dos for loop

a useful thing if you want to copy a multitude of files from all subdirectories in a certain directory to the root of this directory would be to run the following compound command from a DOS shell (it certainly works on Windows XP SP3):

for /d %i in (*) do @copy /Y %i\*.* .

certainly an improvement, as you wince each time you see how little your common DOS prompt offers you in terms of scriptability, comparing to your common UNIX/Linux shell.

/d switch invokes FOR on directories instead of files
* could be any wildcard that you would use with a dir command
@ does something which i can't really explain - as far as i could see it runs the copy command smoothly without returning to command prompt at the end of each for loop
/Y switch forces overwrite (suppresses user prompt), in case of files with identical names

note that in batch files all %i should be written as %%i as pointed out (among other things) here

now, a nice quick n dirty deployment scheme for deploying your new builds to multiple workstations on the same domain would be:
- to have a directory on c that would contain only the files to be copied
- to have a batch file that would run the following:


for %%i in (*.exe *.dll *.xml) do @copy /Y %%i "\\workstation1\c$\workdir"
for %%i in (*.exe *.dll *.xml) do @copy /Y %%i "\\workstation2\c$\workdir"
for %%i in (*.exe *.dll *.xml) do @copy /Y %%i "\\workstation3\c$\workdir"
...


edit: another take on copying files from subdirectories:

for /F %i in ('dir /a:-D /B /S *') do copy /Y %i .


/F lets you use a shell command in parentheses
/a:-D excludes files with directory attribute
/b is DIR bare format (just filenames)
/s is telling DIR to go into subdirectories

No comments:

Post a Comment