Robocopy Help

City of Newark
City of Newark used Ask the Experts™
on
Robocopy question

Currently I have a .bat file that uses robocopy to copy specific files from one folder to another.  Easy and it works just fine.

Issue now is the files are coming to us in multiple folders with date and timestamp.  What we need to is just copy the files within those folders to another location without copying the actual folders.

Current:

@Echo off

ROBOCOPY
D:\file1\file2\file3\
D:\destination\
*.XML
/MAXAGE:1

Again files were saved within folder "file3".  However, now there are multiple folders within "file3" and we need to pull just the files from within those subfolders but all the folders have different date and timestamps

Need:

@Echo off

ROBOCOPY
D:\file1\file2\file3\Folder_test_072519 (where date changes everyday)
D:\destination\
*.XML

Is there anyway to do this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
ROBOCOPY doesn't have the ability to do that in one execution, you would have to do it for each subfolder (the dated ones now).  Here's a small BAT that may help, give it a test there...

@echo off
setlocal

set BaseDir=D:\file1\file2\file3
set DestDir=D:\destination

for /d %%D in ("%BaseDir%\*.*") do (
    robocopy "%%~D\" "%DestDir%\" *.XML /MAXAGE:1
)

Open in new window


»bp

Author

Commented:
How do you copy from the subfolder (dated ones) if the date changes everyday?
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
How do you copy from the subfolder (dated ones) if the date changes everyday?
That's what the FOR /D loop does it, gets all the subfolders under the BaseDir and copies each to the DestDir.

Or do you only want to get todays single folder?


»bp
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
If you just want to copy the files from the one folder that has todays date (in MMDDYY format) then this should do that.  This assumes the files will look like:

D:\file1\file2\file3\072519
D:\file1\file2\file3\072619
D:\file1\file2\file3\072719


If there is some constant text before the date then that will need to be added...

@echo off
setlocal

REM Define folders to work with
set BaseDir=D:\file1\file2\file3
set DestDir=D:\destination

REM Get current date/time in default YYYYMMDDhhmmss format
set Today=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
    if not defined Today (
        set Today=%%A
    )
)
REM Format current date/time as MMDDYY
set Today=%Today:~4,2%%Today:~6,2%%Today:~2,2%

REM Copy the files for todays folder
if exist "%BaseDir%\%Today%\" (
    robocopy "%BaseDir%\%Today%\" "%DestDir%\" *.XML /MAXAGE:1
)

Open in new window


»bp
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
@City of Newark,

Are you all set with this now, or do you need more help?  If all set, could you please close it out now.  If you need help with the question close process take a look at:



»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial