DIR /B switch

  • Hello,

    I am copying files on Server1 to a certain folder on the network share folder every 10 mins. Some of the files a quite big.

    On Server2 I need to rename files which are already copied to that network folder.

    I am using DIR /B command to get the list of files copied to that folder.

    However, it also gives me the list of files being copied. So my renames process on Server2 fails, if, at the time of rename, files are still being copied to that folder.

    So, is there any additional switch I can add to DIR /B to see only copied files in the folder.

    Thanks in advance.

  • Hi,

    I'm sorry, but I think, this is no SQL problem. Maybe you could get more help in this forum:

    http://www.computing.net/forum/dos/1.html

    Kind regards,

    Andreas

  • yes, you are right, but I was using that as part of the sql processing, so I was hoping that someone might know.

  • inHouseDBA (4/25/2016)


    yes, you are right, but I was using that as part of the sql processing, so I was hoping that someone might know.

    Here's an example DOS command that I run from xp_CmdShell (which I love... makes life so easy).

    @ECHO OFF && 2>nul (>>"C:\Temp\Test.csv" (CALL )) && (ECHO File is not locked.) || (ECHO File is locked.)

    You can substitute some other action instead of the ECHO messages.

    I can't take credit for this. Please see the 2nd edit of the most popular answer at the following URL. There's also an explanation there.

    http://stackoverflow.com/questions/10518151/how-to-check-in-command-line-if-a-given-file-or-directory-is-locked-used-by-any/10520609#10520609

    Someone else incorporated his code into a nice DOS Batch Loop at the following URL.

    http://www.dostips.com/forum/viewtopic.php?t=5542

    Of course, if you're using xp_CmdShell, you can avoid the DOS Loop and do some success/fail logging to a table.

    I will warn you though... if you pass it the name of a file that doesn't exist, it will create the file as a zero byte file.

    --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)

  • inHouseDBA (4/25/2016)


    Hello,

    I am copying files on Server1 to a certain folder on the network share folder every 10 mins. Some of the files a quite big.

    On Server2 I need to rename files which are already copied to that network folder.

    I am using DIR /B command to get the list of files copied to that folder.

    However, it also gives me the list of files being copied. So my renames process on Server2 fails, if, at the time of rename, files are still being copied to that folder.

    So, is there any additional switch I can add to DIR /B to see only copied files in the folder.

    Thanks in advance.

    Shifting gears a bit, why not just rename the files while you're doing the copy?

    --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)

  • Heh... one more thought. Why not just use the "&&" and "||" decision notation with the rename command instead of the double redirection with a nul write?

    --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)

  • Thank you Jeff, I cannot rename files on Server1 due to some logic, presented only on Server2

    I am inserting the results of xp_cmdshell 'DIR /B PATH' to the table and then doing some manipulations with the filename in the table and then rename files on Server 2.

  • inHouseDBA (4/25/2016)


    Thank you Jeff, I cannot rename files on Server1 due to some logic, presented only on Server2

    I am inserting the results of xp_cmdshell 'DIR /B PATH' to the table and then doing some manipulations with the filename in the table and then rename files on Server 2.

    No, no. I agree. Not suggesting that you rename the source files on Server1 themselves. I mean use the COPY command to copy to a differently named target file on Server2. For example...

    COPY "\\Server1\SomeFolder\OriginalFileName.txt" "\\Server2\SomeOtherFolder\ADifferentName.txt"

    --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. You did see my post on how to determine if a file was open or not? Does that help?

    --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)

  • you can use DIR PATH /S /OD

  • barsuk (4/27/2016)


    you can use DIR PATH /S /OD

    I'm pretty sure that will also list files that are currently open.

    --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)

  • Thank you Jeff for all your suggestions, but looks like using DIR PATH /S /OD and some additional parsing after that may help me solve the issue in someway.

  • Cool. When you figure it out, let us know what "some way" turned out to be, please. Thanks.

    --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)

  • If you dump results of DIR PATH /S /OD to the table and then query that table, then the first chars for each row is the date and time when file was created.

    Timestamp is a part of my file name. So all I have to do is to parse timestamp from my file name and compare it to the file creation date. If they match - then file has been copied already. But if they don't-then file is being copied at this moment. I didn't write the code for it yet, as I have a bunch of other projects on hands. So that's pretty much it. Let me know if you find any flaws in my logic.

  • inHouseDBA (4/27/2016)


    If you dump results of DIR PATH /S /OD to the table and then query that table, then the first chars for each row is the date and time when file was created.

    Timestamp is a part of my file name. So all I have to do is to parse timestamp from my file name and compare it to the file creation date. If they match - then file has been copied already. But if they don't-then file is being copied at this moment. I didn't write the code for it yet, as I have a bunch of other projects on hands. So that's pretty much it. Let me know if you find any flaws in my logic.

    I'm pretty sure that what you say isn't true. The CREATION DATE (the default for the /OD, IIRC) is the date and time that the file is first created by the current copy. That doesn't change even though the file may be currently in the process of being written to. A better check would be to check the MODIFIED DATE using the /TW check because that will be the same as the CREATION DATE while the copy is in progress and will frequently be less than the CREATION DATE once copied. As you can imagine, that might not be the most reliable method either and is certainly more complex that just checking to see if the copy of the file is still open.

    I'm pretty sure that you're going to still need to check to see if the file is open or not, especially if the files have any heft to them, to do this in an error free manner.

    --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 15 posts - 1 through 15 (of 17 total)

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