views:

218

answers:

1

I want to make a .bat to copy & rename a file multiple times. I want to have a list of names, and an original file, then I want to copy that file and rename it for each name on the list.

How I can do this using a .bat file?

Also is it possible to run winrar fromthe .bat to .rar or .zip every file after copying/renaming?

Example:

$file = "file.tmpl";
$names = "name1, name2, name3, nameetc";
foreach( $names as $name) {
    copy $file; //to avoid deleting the original
    rename $file to $name;
    zip $name; //I dont really need this but if its easy to do i will like to use it
}

So I start with a file.tmpl and I end up with 4 more files (which are a duplicate of file.tmpl) called name1, name2, name3, nameetc.

The example is not a real coding lang, I used a sort of php sintax because is the language I know more.

A: 

Do the file names need to be in a string list?

If you can name them in a separate file, like so,

name-one.pdf
name-two.pdf
name-three.pdf

then this batch file will work

SET source_file=%1
SET name_list_file=%2

FOR /F "usebackq delims=," %%G IN (`TYPE %name_list_file%`) DO (
    COPY %source_file% %%G
)

You would call it like this

batch-file-name source-file-name name-list-file

In other words, I called the batch file make-copies.bat, and the name file filenames.txt, and I used it to copy a file called mla-play.pdf.

make-copies mla-play.pdf filenames.txt

This also allows you to change the target name list without modifying the batch file.

Hope this helps.

harpo
Thank you, getting the names from a txt is even better.
flyout