Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
xterm11
- Posts: 2
- Joined: 26 May 2009 00:15
#1
Post
by xterm11 » 26 May 2009 00:23
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.
-
avery_larry
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
#2
Post
by avery_larry » 26 May 2009 10:43
Do you want it copied into every subfolder?
Code: Select all
cd /d "%~dp0"
for /d %%a in (*) do copy example.txt "%%a"
-
xterm11
- Posts: 2
- Joined: 26 May 2009 00:15
#3
Post
by xterm11 » 10 Jul 2009 11:40
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?
-
avery_larry
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
#4
Post
by avery_larry » 10 Jul 2009 12:03
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.