james chang
james chang

Reputation: 329

Batch script that copies all contents of subdirectories into a parent directory?

Let's say I have a batch file, script.bat. This batch file is placed into a parent folder.

> Parent folder
    script.bat
    > subdirectory1
        file1.1
        file1.2
        file1.3
    > subdirectory2
        file2.1
        file2.2
        file2.3

How do I write the batch file so that when I double click on it, it copies all the files from (inside the subdirectories) to (the folder that the batch file is located in)?

Upvotes: 1

Views: 1633

Answers (1)

rojo
rojo

Reputation: 24476

The code you're looking for is

for /d %%I in (*) do copy "%%~I\*" .

for performs the command after do on every directory (because of the /d switch) matched by *. copy copies. %%~I is a variable with a value of whatever directory name to which the for loop has progressed. The tilde in %%~I strips surrounding quotation marks, if any. The . at the end is shorthand for the current working directory (the directory containing the batch script). See for /? in a cmd console for more info.

Upvotes: 2

Related Questions