Recursive Copy then script_One folder at a time

I have this attached batch file which copies recursively all files from folders.  If a file has the same name in two different folders, then it is renamed.  It works very well and I got help on it from EE a year or more ago.  

However, now I am dealing with dual layer DVDs, etc with thousands of folders.  How can I have it copy all of the contents of a folder, run a script, then copy next folder, then run the script.  Basically the same sort of looping fashion, but run a script in between each download of folder contents?  
#------copy files from disc to destination while renaming files if duplicates exist is subfolders---
set source=%MYVAR18%:\
set destination=c:\RadiologyTechnology\CD_Import
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('dir /a-d /b /s "%source%"') do (
   set filename=%%~nxa
   if exist "%destination%\!filename!" call :GetNewFN "!filename!"
   copy "%%a" "%destination%\!filename!"
)

goto proceed2XML
exit /b

:GetNewFN
set count=0
:loop
   set filename=%RANDOM%%RANDOM%%RANDOM%
if exist "%destination%\%filename%" goto loop
exit /b

Open in new window

weklicaAsked:
Who is Participating?
 
Paul TomasiCommented:
Oops! In addition to the previous 3 comments, please remove the PAUSE in Line 47.
0
 
Paul TomasiCommented:
For a start off, you should consider using 'FOR /D' or 'FOR /R' instead of the horrible 'FOR /F...(DIR /S' construct. Also, the random name generator for duplicate names is rather mish-mash and could be written incorporating better logic.
0
 
Paul TomasiCommented:
Here's a quick mock-up however, I will improve the random filename generator in a mo...

Every time the foldername changes, the batch file calls the routine PROCESS. You can place your own code in that routine.

set source=%MYVAR18%:\
set destination=c:\RadiologyTechnology\CD_Import
setlocal enabledelayedexpansion

set drvpath=

for /r %cource% %%a in (*.*) do (
  if %%~dpa neq !drvpath! call :process
  set drvpath=%%~dpa

  set filename=%%~nxa
  if exist "%destination%\!filename!" call :GetNewFN "!filename!"
  copy "%%a" "%destination%\!filename!" >nul
)

goto proceed2XML
exit /b


:GetNewFN
  set count=0
  :loop
    set rnd=%RANDOM%
  if exist "%destination%\%~n1%rnd%.%~x1" goto loop
  set filename=%~n1%rnd%.%~x1
goto :eof


:process
  rem
  rem Place your code here
  rem
goto :eof

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Paul TomasiCommented:
Oops! Line 7 should be:
for /r %source% %%a in (*.*) do (

Open in new window

not 'course' !!
0
 
Paul TomasiCommented:
Apologies.... There were lots of bugs in my previous post... Here's a working version!

set source=%MYVAR18%:\
set destination=c:\RadiologyTechnology\CD_Import
setlocal enabledelayedexpansion

set dp=%source%

for /r "%source%" %%a in (*.*) do (
  if not "%%~dpa"=="!dp!" call :process

  set dp=%%~dpa
  set filename=%%~nxa

  if exist "%destination%\!filename!" call :GetNewFN "!filename!"

  copy "%%a" "%destination%\!filename!"
)

call :process

goto proceed2XML
exit /b


:GetNewFN
  set count=0
  :loop
    set /a count+=1
  if exist "%destination%\%~n1(!count!)%~x1" goto loop
  set filename=%~n1(!count!)%~x1
goto :eof


:process
  rem
  rem Place your code here
  rem
goto :eof

Open in new window


Please note the filename generator: if for example, ABC.TXT exists, then it will generate ABC(1).TXT and then ABC(2).TXT etc... Just as DOS does!
 
0
 
Paul TomasiCommented:
Apologies once again...

Here's an improved version
set source=%MYVAR18%:\
set destination=c:\RadiologyTechnology\CD_Import
setlocal enabledelayedexpansion

set source=d:\

set dp=%source%

for /r "%source%" %%a in (*.*) do (

  if not "%%~dpa"=="!dp!" (
    set dp=%%~dpa
    call :process
  )

  set filename=%%~nxa

  if exist "%destination%\%%~nxa" (
    call :GetNewFN "%%~nxa"
  ) else (
    set filename=%%~nxa
  )

  title %%a --^> !filename!
  copy "%%a" "%destination%\!filename!" >nul
)

call :process

goto proceed2XML
exit /b


:GetNewFN
  set count=0
  :loop
    set /a count+=1
  if exist "%destination%\%~n1(!count!)%~x1" goto loop
  set filename=%~n1(!count!)%~x1
goto :eof


:process
  rem
  rem Place your code here
  rem
  pause
goto :eof

Open in new window


Notice in line 24, I display a running progress in DOS's window's titlebar. You may remove this line if this feature is not required.

Any problems, please give us a shout!
0
 
Paul TomasiCommented:
Oops! Please remove Line 5 (this was added while testing the code at my end).
0
 
Paul TomasiCommented:
You shall be pleased to note the filename generator in the code above is a big improvement on the random number generation from your original code. If for example, ABC.TXT already exists, then it will generate ABC(1).TXT, and then ABC(2).TXT etc... Just as DOS does!

If you need to produce a logfile of errors (should any occur), or a logfile of filenames that are renamed, or any other type of logfile then let us know.

The call :process on Line 28 is necessary if you need to do some processing after files are copied from the last folder. The reason why this is added here is because call :process is normally executed only when a folder-change takes place. Let us know if this needs to be tweaked for pre-processing or post-processing of files.
0
 
weklicaAuthor Commented:
Holy smokes.  Sorry for delay.  I dropped off the map for a bit.  

Thanks so much for your help!
0
 
Paul TomasiCommented:
No probs! Thank you too! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.