Find Folder & Copy

May 7, 2010 at 05:09:59
Specs: Windows XP
Im trying to create a batch script which will find a string within a folder name and return the full path to robocopy to copy it to another destination. The script that I have does work but it is extremely slow. I think the dir command is doing a full search of the directory for each loop (This folder has a huge number of subdirectories!). Is there a better way this can be done for sake of speed? See script below

@echo OFF & SetLocal EnableDelayedExpansion

:: ASK USER FOR DESTINATION FOLDER & STARTING DIRECTORY

set _DEST=H:\Scheme Folder
set _SEARCH=H:\Sanky-data\C Programme Delivery\001 Schemes\Roads\

:: LOOP TO FIND FOLDER THEN COPY TO SPECIFIED DESTINATION

for /f "delims=" %%i in (schemelist.txt) do (
dir /b /s /ad "!_SEARCH!*%%i*" > dir.txt
set /p FOLDER=<dir.txt
IF EXIST "!FOLDER!\Project Information\A. Health Safety and Environment\" (
REM Xcopy /E/I/Q/H/K/O/Y/C "!FOLDER!\A. Health Safety and Environment" "!_DEST!\%%i\Project Information\A. Health Safety and Environment\"
ROBOCOPY "!FOLDER!\Project Information\A. Health Safety and Environment" "!_DEST!\%%i\A. Health Safety and Environment" /COPYALL /s /E /V /ZB /W:2 /R:2
ECHO %%i,Successful,%time%,%date% >> schemereport.csv
) ELSE (
ECHO %%i,Not Found,%time%,%date% >> schemereport.csv
)
)

:: DELETE TEMP FILE

del dir.txt


See More: Find Folder & Copy

Report •

#1
May 7, 2010 at 06:47:11
maybe something like this, using findstr:
for /f "tokens=*" %%a in ('dir /b /s ^|findstr /i /g:schemelist.txt') do (
:: robocopy stuff here
echo %%a
)

Report •

#2
May 7, 2010 at 13:46:46
I'm still a bit limited in my knowledge of batch so thanks for pointing out this alternative! I'll give it a go and let you know how it goes on Monday.

Report •

#3
May 9, 2010 at 09:26:26
But I see that you have come to the correct conclusion yourself : if you use too many nested IF and FOR statements, you dont understand your own code anymore, let alone, somebody else.

Try using either labels, or seperate scripts (as functions are not supported in DOS/Windows). Labels provoke spaghetti-code, which is bad, bet it is still better than nested code.

And 1 more advice, if the line of code does not fit any line, make it short.


Report •
Related Solutions


Ask Question