DOS command to Copy a file from parent dir to specific folder within many child directories
I'm trying to teach myself some simple DOS commands and have used relatively simple commands to copy or move files, however this specific request is presenting a challenge for me and would appreciate some expertise from this forum.
C:\Parent\library.eds (location of my source file)
Any time I update library.eds in the parent directory, I would like to copy that开发者_运维知识库 file into every Child directory that contains a folder named "LIB". I have standardized the Child directories to the following:
C:\Parent\Child1\INPUT
C:\Parent\Child1\OUTPUT
C:\Parent\Child1\LIB {paste library.eds here}
C:\Parent\Child2\INPUT
C:\Parent\Child2\OUTPUT
C:\Parent\Child2\LIB {paste library.eds here}
and loop through until all children with LIB directories contain the updated file "library.eds"
Thank you for your help! Mark
Here's a command that can get you started:
FOR /F "delims=" %%D IN ('DIR /b /a:D /s C:\Parent\LIB') DO @ECHO COPY "C:\Parent\library.eds" "%%~D"
Once you get it working the way you want, remove the @ECHO
part to actually do the copy:
FOR /F "delims=" %%D IN ('DIR /b /a:D /s C:\Parent\LIB') DO COPY "C:\Parent\library.eds" "%%~D"
Extra help for these commands
HELP FOR
HELP DIR
How this works
FOR /F ... %variable IN ('command') DO otherCommand %variable...
This lets you execute command
, and loop over its output. Each line will be stuffed into %variable
, and can be expanded out in otherCommand
as many times as you like, wherever you like. %variable
in actual use can only have a single-letter name, e.g. %V
.
"delims="
This lets you ignore any whitespace output by 'command'
, so it properly handles directories that have spaces in their names.
DIR /b /a:D /s C:\Parent\LIB
This will search for all files under C:\Parent
that have the name LIB
. It will recursively go through subdirectories because of /s
. It will only find directories (because of /a:D
). It will output them in a way that is useful for looping, because of /b
.
%%D instead of %D
This is required in batch files. If you did this on the command prompt, you would use %P instead.
if you know all the child directories before hand the simplest solution is batch files tutorial on batch files
I dont know how loops work in ms dos but in linux bash shell loops are simple to program..
Any ways batch files are the simplest option
if you can download cygwin then you can use the following bash script to do the job without hardcoding it
The code
P.S you will need to change the name of the folder from tmp to LIB and the filename from LOL to ur file name .. The script is pretty self explanatory.
Unix shells are better than MS-DOS so it might be a good idea to get cygwin anyways
精彩评论