Link to home
Start Free TrialLog in
Avatar of jwiang4u
jwiang4uFlag for United States of America

asked on

Replicate Directories Based on Directory Name

I am trying to replicate a set of folders from one directory to another once per day.  The problem is that i only want to replicate folders that have a certain number in the directory name.  I would like to find a way/tool that will replicate folders that contain the number 1300 in the folder name.  I have used several tools like robocopy, xcopy, and sync toy in the past but i am unable to figure out how to copy folders based on the folder name and not the file names.
Avatar of oBdA
oBdA

Try the following batch script. Just set the "SourceFolder" and "TargetFolder" variables to their respective values (the "1300" folders will be added automatically), and set the robocopy options to what you actually need.
The script is currently in test mode and will only display the robocopy commands it would normally run. To run it for real, remove the "ECHO" in front of robocopy.exe.
@echo off
setlocal enabledelayedexpansion
set SourceFolder=S:\Source
set TargetFolder=T:\arget
set RCOptions=/e
set FindString=1300

set FolderList=
for /f "delims=" %%a in ('dir /a:d /b "%SourceFolder%" ^| find "%FindString%"') do (
  set FolderList=!FolderList! "%%~a"
)
if not defined FolderList (
  echo No folders matching '%FindString%' found.
  goto :eof
)
echo Replicating folders: %FolderList%
for %%a in (%FolderList%) do (
  ECHO robocopy.exe "%SourceFolder%\%%~a" "%TargetFolder%\%%~a" %RCOptions%
)

Open in new window

Avatar of jwiang4u

ASKER

@oBda

I created the batch file and put in the source and target folder information as you said. When i run the batch file the command window pops up and closes so fast that i can't see what is in it.  It looks like it opens and then exits right away. Thanks in advance for your help.
Especially when testing scripts, don't start them by double-clicking in Explorer, start them from an open command prompt.
@oBdA

I ran the batch from the command prompt and the output is "No folders matching '1300' found."  When I search the source directory there are about 60 folder that contain 1300 in the folder name. None of them are in the root of the source directory but are instead nested within other folders. Could this be the problem?  Is it looking for an exact match of a folder named 1300?
It's currently only looking directly in the root folder.
If you want to look for nested folder names, please provide some more information about the structure.
Can the "1300" folders appear anywhere in the folder tree, or just at a certain level?
For example like
C:\Root\A\abc1300
C:\Root\B\abc1300
C:\Root\C\abc1300
Does the "1300" always appear at the same position (maybe it only appears at the beginning or the end of the folder name, or has always 3 characters before/after it), or can it be anywhere in the folder name?
@oBdA

The folders can appear anywhere in the folder tree.  The folder name always starts like this with 4 alpha-numeric values followed by dash and 1300 another dash and then a variable text string.

????-1300-(some text string)

Thanks in advance for all the help.
Try this then; as before, it's in test mode and will only copy anything once the ECHO in front of robocopy.exe is removed.
@echo off
setlocal enabledelayedexpansion
set SourceFolder=D:\Temp
set TargetFolder=T:\arget
set FindString=-1300-
set RCOptions=/e /copyall
for /f "delims=" %%a in ('dir /a:d /b /s "%SourceFolder%"') do (
  echo %%~nxa| find /i "%FindString%" >NUL
  if not errorlevel 1 (
    echo Replicating folder: %%~nxa ...
    set FullPath=%%~a
    set RelPath=!FullPath:%SourceFolder%\=!
    ECHO robocopy.exe "%%~a" "%TargetFolder%\!RelPath!" %RCOptions% 
  )
)
goto :eof

Open in new window

@oBdA

It think we are getting really close. The batch file found all the folders and it looks like it is going to copy them fine but it seems like there may be something a little strange in the script because it is trying to execute a lot of strings in the folders/files as commands in the command prompt. Please see the screenshot.

User generated image
Do you have folders in there with names that contain any one or more of these characters: ^ & %?
Try if it help to just delete the following line (line 10 in the script above):
echo Replicating folder: %%~nxa ...

Open in new window

@oBdA

I did find & symbols in some of the directory names which looks to be the problem. I removed line 10 as you advised and it didn't change anything.

I tried removing the echo from in front of Robocopy to test the functionality and I received the following error on all of the matching directories.  It looks like the problem is that robocopy isn't getting passed the entire destination directory path because of the space in it.  Here is the script that you gave me with my information put in followed by the error i am receiving. In the error you can see that the destination passed to robocopy stops short at the space.

@echo off
setlocal enabledelayedexpansion
set SourceFolder="z:\ECR-completed"
set TargetFolder="z:\Kapstone\1003 - Spine Trilogy\Released\ECRtest"
set FindString=-1003-
set RCOptions=/e /copyall
for /f "delims=" %%a in ('dir /a:d /b /s "%SourceFolder%"') do (
  echo %%~nxa| find /i "%FindString%" >NUL
  if not errorlevel 1 (
    echo Replicating folder: %%~nxa ...
    set FullPath=%%~a
    set RelPath=!FullPath:%SourceFolder%\=!
    robocopy.exe "%%~a" "%TargetFolder%\!RelPath!" %RCOptions% 
  )
)
goto :eof

Open in new window

User generated image
This version should be able to handle & in the folder names as well (works at least here under Win 7).
in addition, I've added some logging. The log file will be in the script's folder, with the script's name and the extension .log. The log file will be cleared with each new run of the script. Successfully copied files and folders will NOT be logged (only in robocopy's summary), only errors will be explicitly listed.
@echo off
setlocal enabledelayedexpansion
set SourceFolder=D:\Temp
set TargetFolder=T:\arget
set FindString=-1300-
REM *** %~dpn0 will expand to Drive, Path, Name of the script:
set LogFile=%~dpn0.log
set RCOptions=/e /copyall /tee /nfl /ndl /np /log+:"%LogFile%"

if exist "%LogFile%" del "%LogFile%"
for /f "delims=" %%a in ('dir /a:d /b /s "%SourceFolder%"') do (
  echo "%%~nxa"| find /i "%FindString%" >NUL
  if not errorlevel 1 (
    echo Replicating folder: "%%~nxa" ...
    set FullPath=%%~a
    set RelPath=!FullPath:%SourceFolder%\=!
    >>"%LogFile%" echo ==== Begin ====== "%%~a" ==========
    ECHO robocopy.exe "%%~a" "%TargetFolder%\!RelPath!" %RCOptions% 
    >>"%LogFile%" echo ==== End ====== "%%~a" ==========
  )
)

Open in new window

@oBdA

I used your new script and it seemed to have less problems with folder names being run as commands but when i run it without the echo in front of robocopy, i am still having the robocopy errors and it looks like the destination folder is still being cut off at where the space in the folder name occurs. I have attached the log when I ran it exactly as your script is laid out with the echo in front of Robocopy.
Log.txt
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think the key difference between what you are running and I am running is that my destination target folder contains a space and that seems like what is causing problem. If you look in my previous screenshots you will see that it truncates the destination to where the space occurs. The robocopy error confirms this because when i look at error #3 online it refers to folder destinations with a space in them.  Can you tell me how to put quotes somewhere so that this space will be accepted?
Then the error is that you already put quotes around the destination when defining the variable(s).
Do not do this; all quotes will be added in the script where and when required.
@oBdA

I removed the quotes like you said and I changed the /COPYALL to /COPY:DATS (permissions problem) and now the script seems to be working great. I am going to test over the next 2 days but i think you got it. The problem was my quotes. Thanks for all of your help.