Link to home
Start Free TrialLog in
Avatar of City of Newark
City of Newark

asked on

Robocopy Help

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?
Avatar of Bill Prew
Bill Prew

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
Avatar of City of Newark

ASKER

How do you copy from the subfolder (dated ones) if the date changes everyday?
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
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
@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
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.