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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Paul TomasiCommented:
Oops! Line 7 should be:
for /r %source% %%a in (*.*) do (

Open in new window

not 'course' !!
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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
Paul TomasiCommented:
Oops! In addition to the previous 3 comments, please remove the PAUSE in Line 47.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.