Move File Cmds

  • Hello,

    Ina Stored Procedure I am using the move command to transport files. Is there a way to check if the file already exists and if it does rename the new file with a version number?

    Here is my code:

    SET @cmd = 'MOVE /Y ' + @Folder_Dir + @FilesInDir + ' ' + @Error_Dir

    EXEC sys.xp_cmdshell @cmd;

    PRINT @cmd

    Is there a cmd or simple way to check if the file is already there and if it is rename the new file to say filenamev2?

  • https://technet.microsoft.com/en-us/library/bb490920.aspx?f=255&MSPPError=-2147217396, 3rd example

    if [not] exist FileName command [else expression]

    and

    https://technet.microsoft.com/en-us/library/bb490987.aspx, 2nd example:

    ren [Drive:][Path] filename1 filename2

  • Meatloaf (12/11/2015)


    Hello,

    Ina Stored Procedure I am using the move command to transport files. Is there a way to check if the file already exists and if it does rename the new file with a version number?

    Here is my code:

    SET @cmd = 'MOVE /Y ' + @Folder_Dir + @FilesInDir + ' ' + @Error_Dir

    EXEC sys.xp_cmdshell @cmd;

    PRINT @cmd

    Is there a cmd or simple way to check if the file is already there and if it is rename the new file to say filenamev2?

    No. You'll need to read the file names from both directories, make the comparison, and change the move command to move just one file at a time so that the destination file name can be changed.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • p.s. I recommend that you use ROBOCOPY (comes with DOS/Windows) instead. It has auto restart if a file fails part way through, logging, and a whole bunch of other features that simply aren't available in the older DOS file commands.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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