Archiving text files in SQL Script

  • I need to pass a file name to a command in T-SQL and then have it move the file to another directory, is this possible?? 

    Alternatively if there is a process whereby I can move the entire contents of a directory to another location it would be workable but I would prefer the first one.

    If anyone has any ideas I would really appreciate it.

    Thanks

    Andrew

  • Hi Andrew,

    You can happily use the xp_command shell e.g:

    EXEC Master..xp_cmdShell 'MOVE "\\Server\Drive\Dir\*.xls" "\\Server\Drive\Dir\Archive\"'

    EXEC Master..xp_cmdShell 'DEL "\\Server\Drive\Dir\*.xls"'

    Have fun

    Steve

    We need men who can dream of things that never were.

  • Don't forget to check dos errors using the return from cmdshell (assuming the platform suppoorts it).

    declare @err INT

    EXEC @err = master..xp_cmdshell ...

    IF @err <> 0 PRINT "oops, so much for trying"

    just because your doing a kluge doesn't mean you have to be sloppy about it...

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply