Page 1 of 1
Copy
Posted: 26 May 2009 00:23
by xterm11
How can I copy a file into a subfolder, if I don't know the name of the subfolder? Whenever I use * it says I'm copying over the file, and it doesn't like the syntax of *\*.
Code: Select all
@echo off
Copy example.txt "%~dp0*\*"
pause
Something like that.
Posted: 26 May 2009 10:43
by avery_larry
Do you want it copied into every subfolder?
Code: Select all
cd /d "%~dp0"
for /d %%a in (*) do copy example.txt "%%a"
Posted: 10 Jul 2009 11:40
by xterm11
avery_larry wrote:Do you want it copied into every subfolder?
Code: Select all
cd /d "%~dp0"
for /d %%a in (*) do copy example.txt "%%a"
THANK YOU! Just what I needed. Could you explain it to me?
Posted: 10 Jul 2009 12:03
by avery_larry
for /d
That tells the for loop to match directories when using wildcards.
So if you CD into the parent directory first, you can use:
for /d %%a in (*) do
to assign %%a to every subdirectory of the current directory (one at a time), and then execute the do portion on each subdirectory. You can actually specify the path inside the parentheses instead of CD into it like this:
for /d %%a in (c:\tmp\*) do
but it will choke if the path contains spaces, and you can't use double quotes inside the parentheses when using for /d.
In short, the for /d loop allows us to use a wildcard for the subdirectory(ies) to match, where the simple copy command will not allow that.