So, you want to become more productive? How about learning how to copy files from subfolders to one specific folder. This could possibly be a huge time-saver! Let’s get to it.
Using The Command Prompt To Copy Files – Simplified
Windows 7 provides several great tools that allow you to copy files more effectively. Unfortunately, most of them are only accessible via the command prompt. There are for example: copy, xcopy and robocopy. All three can be used to become more productive.
1. Step Open a command prompt
2. Step Enter cd C:\pathtoyourfolder **replace pathtoyourfolder with your actual path, e.g. C:\Users\username\Pictures
The Actual Code
3. Step Copy files from subfolders to one folder:
for /f "tokens=*" %a in ('dir /b /s /a-d') do @copy "%a" "C:\YourFolder" /y
Code Explanation
What is does is to look into each subfolder (dir /b /s /a-d) in the current folder and then copy each file in the subfolder. It will copy each file to the folder C:\YourFolder
What is the /y doing? I am using this option to NOT get prompted for overwriting files. If you don’t add /y it will always ask if you want to overwrite files with the same name. So, if you have files in the subfolders with the same name you will have to rename them first.
If you’ve done everything correctly, it will then copy all files within the subfolders and copy them to the folder that you specified:
Next up, are some more productivity tips, so stay tuned for more!
This does not work keep getting error *” was unexpected at this time.
code was
for /f “tokens=*” %a in (‘dir /b /s/a-d’) do @copy “%a” “d:\new\rar” /y
I have got this working. with the following
for /f “tokens=*” %a in (‘dir /b /s /a-d “c:\test”‘) do @copy “%a” “d:\new\rar”
It appears you left out the source folder (c:\test) in your example.
working fine for me, many thanks
any further detailes about win 7 cmd tools?
I kept getting the error:*” was unexpected at this time.
Turns out if you just copy and paste the above, the quotes and double quotes come out wrong – you need to make sure that the quotes in the command prompt are straight up like this (” ‘) not slanted like this (“ ” ‘’)
…. aand they were autocorrected to the slanty ones in my comment above :(. basically, if you get the error, try manually typing the command out, not copy-paste.
It works!
But there are a few notes.
The first is already described by Rosy – if you copied the source code from the browser, do not forget to replace quotes and double quotes.
The second one is that if you put the commands in a batch file you must double the percents before the FOR variables. For example, %a on the command line would need to be %%a in a batch file.
And one more useful note (all to be in one place :)).
In case you need to copy all the files by some file name mask, you should use the following format:
for /f “tokens=*” %%a in (‘dir “*.zip” /b /s /a-d’) do
as you can see the file name mask is added right after the “dir” command.