Copy and paste a file 200 times with new names
I have a folder with a file named: 'template.xlsx', and hundreds of files named:
old_2301 Item 1.xlsxold_2014 Item 2.xlsxold_5321 Item 3.xlsx- ...
old_3212 Item 200.xlsx
I want to copy the file template.xksx and create 200 new files:
2301 Item 1.xlsx2014 Item 2.xlsx5321 Item 3.xlsx3212 Item 200.xlsx
I've tried something like:
In the folder, type Shift+Right shift: select "open command-window here"
for %a in (*.*) do copy "template.xlsx" ... ren ...?I want to rename the copied template.xlsx files to the same name as the other files, but with old_ trimmed away.
Is there a simple way to do this?
53 Answers
I want to copy template.xlsx to the name of the other files, with old_ removed
Use the following batch file:
@echo off
setlocal enabledelayedexpansion
rem get list of file names
for /f "usebackq tokens=*" %%i in (`dir /b old_*.xlsx`) do ( rem save the filename set _name=%%i rem remove old_ from the name set _name=!_name:old_=! rem do the copy to the modified name copy "template.xlsx" "!_name!" )
endlocalFurther Reading
- An A-Z Index of the Windows CMD command line
- A categorized list of Windows CMD commands
- dir - Display a list of files and subfolders.
- enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
- for /f - Loop command against the results of another command.
- variable edit/replace - Edit and replace the characters assigned to a string variable.
You have to use the files named old_* as your name source and split at the underscore.
@Echo off
For /f "tokens=1* delims=_" %%A in ( 'Dir /B /A-D "old_* Item *.xlsx"'
) Do Echo Copy template.xlsx "%%B"If the output looks OK remove the echo in front of copy.
Explaining the parsing for /f options:
filename old_2301 Item 1.xlsx delims _ tokens 1 _ * (rest) for var %%A %%B
A variant for the command line without batch:
For /f "tokens=1* delims=_" %A in ('Dir /B /A-D "old_*.xlsx"') Do Copy template.xlsx "%B" 2 Unless I actually wanted a program that I could run over and over, if this is just a one-off or a few-off, I would use Notepad++ or any other text editor with block copy and paste. Open a command prompt in the relevant directory and type dir/b > 1.txt, which results in e.g.:
D:\MiscJunk\1>type 1.txt
1.txt
old_2014 Item 2.txt
old_2301 Item 1.txt
old_5321 Item 3.txt
template.txtEdit 1.txt in Notepad++ and add as many of these lines as needed:
copy template.txt ""
copy template.txt ""
copy template.txt ""Block copy (Alt Shift arrow keys) the text:
2014 Item 2.txt
2301 Item 1.txt
5321 Item 3.txtand paste it between the quotes:
copy template.txt "2014 Item 2.txt"
copy template.txt "2301 Item 1.txt"
copy template.txt "5321 Item 3.txt"Then after checking the commands are correct and adjusting accordingly, just copy&paste those commands back into the command prompt, resulting in:
D:\MiscJunk\1>dir/b
2014 Item 2.txt
2301 Item 1.txt
5321 Item 3.txt
...This method is ultra-simple with no debugging needed, and there's very little that can go wrong. Knocking up blocks of commands in Notepad++ is something I do fairly regularly.
1