Batch file to rename files to specific names - windows

So a quick explanation. We have hundreds of projects and in each of them and in every new project we have a program where we fill in a ton of information. From that we get 20 pdf files that are called File_1, File_2 etc.
What I'd like to do is to rename the files as the example below with a bat file.
File_1 = abc.pdf
File_2 = xyz.pdf
File_3 = qwe.pdf
I want to specific in the bat file what I want file_1 should be called and file_2 etc. The files I get will always be called File_1, File_2 etc and I always want them to be renamed the same. So each time I get those files I just run the bat file. Is there a way to do this? Or is there a better way to do this?
Thanks!

I'm assuming your files will always be called "file_1", "file_2" etc.??
If they are, then you can just write a file with a line for each file. For example
ren file_1 foo.pdf
ren file_2 bar.pdf
ren file_3 foobar.pdf
Note that as you didn't specify extensions for the original filenames, I haven't either. You would want to put the full file name with extension, or use wildcards if appropriate, e.g. ren file_1.*. (Be very careful with wildcards though, or you may end up trying to rename multiple files with the same name!)
You could check if the file exists first, and only run the ren on files that are there, or you could run it and let it error for missing files (though I'm sure other people will have reasons why you shouldn't).
You should also consider whether or not there will already be a file with the name you are trying to rename to, because if there the is then the rename will error and fail.
If your files will be different names daily, you will need to give more information as to what you have and what you need.
EDIT - Response to comment below
Copy (and complete) this code, and save it as 'rename.bat' or whatever you need.
#echo off
ren file_1 abc.pdf
ren file_2 xyz.pdf
ren file_3 qwe.pdf
<<repeat as necessary>>
The #echo off just stops the batch file from displayed the commands. It will still display errors (if the file doesn't exist or if you try to rename a file to an existing filename). You really should be looking at catching the errors and doing something with it, but if you can be 100% certain that the new filenames don't exist it will work.
Also worth pointing out that as I haven't used full file names, the batch file would need to be in the same folder as your file_1, file_2 etc.
You could use move to specify the full path of the original file, and a new path (it will move and rename the files), but you still have potentially the same problems with duplicate filenames etc.
I'm assuming you will end up with a folder containing file_1, file_2 etc. so you can just copy your batch file into the folder, run it, then move all your renamed files to where they need to go. Then next time you need to run the file, your folder would only have in it the new set of file_1, file_2 etc. so you could copy in the batch file again, run it... and so on.
EDIT2 - After thought for filenames including spaces
It just occurred to me that your existing files (file_1, file_2 etc.) don't appear to have any spaces in the name, but your new filenames might (you didn't specify the names exactly). If you have spaces if filenames, make sure to add quotes to the command e.g.
ren file_1 "my new file.pdf"
You can also quote your original filenames too (quoting both wouldn't hurt even if there are no spaces) so you could try
ren "file_1" "abc.pdf"
ren "file_2" "x y z.pdf"
ren "file_3" "qw e.pdf"
etc.

Related

Rename multiple files with batch windows

I need to rename a group of .pdf files
In the \Source folder I have the files:
bbbbbbbbb-56.pdf
vduysdvss-60.pdf
sdvbdsbvv-80.pdf
I have to rename them in the \Destination folder like this:
11111111-bbbbbbbbb-ggg-hhh-56-dddd.pdf
11111111-vduysdvss-ggg-hhh-60-dddd.pdf
11111111-sdvbdsbvv-ggg-hhh-80-dddd.pdf
so I need to insert some fixed parts:
before the name
in the middle of the name
at the end of the name.
Using the command:
cd \Source
copy "\Source" "\Destination"
cd \Destination
ren *.pdf 11111111-?????????-ggg-hhh???-dddd.*
the result is:
11111111--56-ggg-hhh-dddd.pdf
the bbbbbbbbb string disappears
can you help me?
Thanks
By using the following command Copy the files from Source to Destination :
copy "/Source_folder" "/Destination_folder"
Go in /Destination folder
cd "/Destination_folder"
And then ren the file names by the following command :
ren *.pdf ???-new_filename_part.*
(The question mark (?) is also a wildcard, but it represents a character of the original file name. So, in the syntax, we're using three question marks, which means that the output file name will include the first three characters of the original file (which works as a unique identifier to avoid duplication)
(According to your logic you can change the new filenames by using some RegExpressions or some variables)

rename files with changing pattern

i want to rename different files in bash with pattern and found this option:
rename 's/.2007/(2007)/g' *.*
with this pattern I can rename every file with ".2007" in name to "(2007)"
--> this is exactly what i want to do.
Next step:
i want to automate this, because i have files with 1995 - 2017. It is a possibility to do:
rename 's/.2007/(2007)/g' *.*
rename 's/.2008/(2008)/g' *.*
rename 's/.2009/(2009)/g' *.*
etc.
but actually, is there another solution?
my files are named like (they are not the same length...):
FILENAME.ANOTHERFILENAME.2007.jpg
FILENAME.2007.jpg
FILENAME.ANOTHERFILENAME.SOMETIMESONEMORE.2007.jpg
With Perl‘s rename:
rename -n 's/.([1-2][0-9]{3})/($1)/' *.*
This renames all files with 1000 to 2999. If everything looks fine remove -n.
For the use-cases where it's not so much about automation but rather about batch processing of a set of files, I find renameutils and its qmv ("quick move") very useful: it enables you to edit the target filenames in a text editor which may be easier/faster than designing regex's for some.
https://www.nongnu.org/renameutils/ (it's in *buntu repos)
But for applications that need to run w/o human intervention, rename is certainly more suitable.

Copy single file instead of entire directory in windows batch

Let's suppose that I am in some directory with two subdirectories, a and b. a has two files in it: t1.txt and t2.txt. That is, I have the following directory structure:
/.
/a
t1.txt
t2.txt
/b
I want to copy the file t1.txt from the a directory into the b directory.
I tried the following command
copy /b a/t1.txt b/t1.txt
but it copies the entire a directory into the b directory.
Why does this happen, and how can I make it so that only the t1.txt file is copied?
When copying to a new directory, you only need to specify the new directory. So
copy /b a\t1.txt b
should work.
That said, I don't think additionally specifying the file name would cause the error you've described -- the official help text says "Destination can consist of a drive letter and colon, a folder name, a file name, or a combination of these," which to me implies that how you have it is fine.
I've also reversed the slashes -- were you using forward slashes in your batch file or is that a typo in the post? Maybe that was the problem?

Change several windows folder names at once

I have to rename several folders. The old folders were randomly named, and the new names allow for a consistent naming protocol. i have reconciled the existing folders with the list of new names, but there are hundreds of folders to rename and to do it manually will take forever.
the old name and new name are generally very different. for example:
old: john l,smth new: smith_john_04082013
so what i would like to do is place a list of the old names in one part of a program and the list of new names in another part, and then loop down the list of folders renaming them until the last one is done.
for example
john l,smth (to) smith_john_04082013
mary-jones 42nd street (to) jones_mary_03122013
wil-h-davis (to) davis_william_02122012
etc
i know how to use the rename command in dos, but all that 'seems' to do is just change the name of of one directory, ie rename "john l,smth" "smith_john_04082013"
i tried doing something like:
rename "john l,smth" "smith_john_04082013"; "mary-jones 42nd street" "jones_mary_03122013"; "wil-h-davis" "davis_william_02122012"
the concept being maybe using a separator was the trick to a multiple rename, but that didn't work either.
if anyone knows how to do this that would be very helpful.
TIA
Just create a batch file (plain text file with a .bat extension, like rename_folders.bat), and list each operation as a single line:
ren "john l,smth" "smith_john_04082013"
ren "mary-jones 42nd street" "jones_mary_03122013"
ren "wil-h-davis" "davis_william_02122012"
(ren is the short name for rename, which saves a little typing.)
Once you have all the lines in the file, save it in the folder where you want it to run, open a command prompt in that folder, and just run the batch file (the .bat extension is optional when you run it, as it's one of the known executable file extensions):
C:\YourBaseFolder>rename_folders
or
C:\YourBaseFolder>rename_folders.bat

Why does xcopy not copy files when using these parameters?

I have a simple xcopy script that I'm running from the command line that reads a CSV file of directories and file names. I've used a very similar script with no problems before. Here is the script:
Z:\HOME\>for /f "delims=, tokens=1,2,3,4" %i in (Z:\HOME\MissingImages.csv) do
echo f | xcopy "Y:\%j\%k\%l" "C:\Horizon\%j\%k\%l" >> Z:\HOME\MissingImagesLog.txt
However, it is not copying any of the files over
Here is an entry from the log file:
Does C:\Horizon\K\00\6bef500f.IMG specify a file name
or directory name on the target
(F = file, D = directory)? f
0 File(s) copied
It's finding the images because if I change the root directory to something else the script will just populate the log file with 0 File(s) copied for all entries, so the files are there and can be seen...
Also, the Z:\ drive is on a network and not local, but again I have used a very similar script across a network without problems (it just takes longer).
I've tried different options like /i, /s, etc. but I can't seem to get it to copy any files over.
xcopy will also report 0 File(s) copied if you use forward slashes "/" in paths instead of backslashes "\", though ONLY if you've enclosed the path in quotes.
This fails with "0 File(s) copied"
xcopy "pathname1/file" pathname2\file
This fails with "Invalid number of parameters"
xcopy pathname1/file pathname2\file
This works just fine
xcopy pathname1\file pathname2\file
It asks because it doesn't know whether you want to copy to directory (to be created) or you provide the full target pathname.
This will ask:
xcopy pathname1\file.from pathname2\file.to
However, adding slash will tell that you copy to directory:
xcopy pathname1\file.from pathname2\to\
But I haven't found the way to tell explicitly that I want to copy and rename file, except
echo Y | xcopy pathname1\file.from pathname2\file.to
I played a bit with your case (with for, do and xcopy) and found out that even if it asks Does SOMEFILE specify a file name or directory name on the target (F = file, D = directory)? it is provided with f from echo and it's copied successfully. Thus, it's not a problem with file/directory specifying, but with copying through network itself.
Well, that's annoying; I found the issue. It looks like when I generated my CSV file, it put a space at the end of each line, so xcopy was looking for files that had a space after the extension.
The thing that was throwing me off was that it was finding the files, but couldn't copy them, making me think it was a network or xcopy issue.
I just ran a sed script to remove the eol spaces and the xcopy script is now working as expected.

Resources