How do I copy recent folder/files from a network drive using a batch file?

9,031

Solution 1

%DATE% will give you the format MM/DD/YYYY instead of the required MM-DD-YYYY

Try this instead to get dashes instead of slashes:

UK/Europe:  /D:%DATE:~3,2%-%DATE:~0,2%-%DATE:~6,4%
USA:        /D:%DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4%

As a side note, if your using Windows 7 have a look at using the 'Robocopy' /MINAGE /MAXAGE command instead, as it is usually far more reliable than xcopy, and provides a lot more options.

Solution 2

have you considered something like the following? (because it's what I did for my same problem)

set folder=%DATE% 
mkdir "%folder%" 
cd "%folder%" 
powershell.exe "copy-item -Path \\server\directory1\*.* -Destination ."

Now keep in mind, this solution will only work on Windows 7 machines, although it can work on WinXP as well if you install "powershell"

What's nice about this method is it doesn't require messing with any shares, the only downside is needing either Win7 on the machine it's running from, or installing powershell.

I just know for me this worked like a charm, good luck to you!

(also note that I'm lazy and just changed the current directory to the date folder, so when I tell it to copy, I just copy to the current location which is why the destination is a period, you could just as easily change the period to your local path)

Share:
9,031

Related videos on Youtube

user158353
Author by

user158353

Updated on September 18, 2022

Comments

  • user158353
    user158353 over 1 year

    My question may have answered someway or other but unfortunately I did not get the exact answer that I was looking for. Here is what I'm trying to do-

    1. create a folder on pc1, c:\temp1
    2. map a network drive that contains source folder/files - \\server1\directory1folder01...10000 ( each day system creates either just single or multiple folders but with unique time stamp
    3. copy the latest folder that was created in the PC1 i.e. c:\temp1

    This is what I have written in the batch file:

    +++++++++++++++++++++++++++++++++

    @echo off
    mkdir c:\temp1
    
    echo mapping drive...
    
    net use Y:\\server\directory1 /user:myusername mypassword
    
    echo copying files/folders into c:\temp1....
    
    xcopy Y:\ c:\temp1 /s/e/d:"%DATE%"
    

    +++++++++++++++++++++++++++++++++

    It creates the c:\temp1 and maps the drive but can't copy.
    Can anyone please help me here?

    Kam

    • user5249203
      user5249203 over 11 years
      Which question are you referring to? Did you already ask a similar question?
    • Julian
      Julian over 11 years
      Do you get any error from xcopy?
    • Isaac Rabinovitch
      Isaac Rabinovitch over 11 years
      Note that you don't need /s if you have /e. Does no harm, though.
    • Keltari
      Keltari over 11 years
      have you tested the xcopy statement by itself to see if it works?
    • CharlieRB
      CharlieRB over 11 years
      Can you give us the exact folder name in order to help format the command correctly?