Rename multiple files with batch windows - file-rename

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)

Related

Renaming a list of 'Filename' inside a folder, with Prefix+'(existing)Filename' . For every file. using command promt/batch file

For example : Suppose we have 4 files inside a folder,
1656088200108CONTPE_CONR.txt
1656174600239CONTPE_NTSL.txt
1656261000254CONTPE_PMLL.txt
1656174600114CONTPE_CONR.txt
I wanted to add 'CONTPE_' in the beginning of every file.
Then i want to replace the filename as :
CONTPE_1656088200108CONTPE_CONR.txt
CONTPE_1656174600239CONTPE_NTSL.txt
CONTPE_1656261000254CONTPE_PMLL.txt
CONTPE_1656174600114CONTPE_CONR.txt
But there are incremental files in the folder. So destination code shouldn't be static/pre-defined.
I tried to do various code like:
ren *CONTPE*.* CONTPE_*
ren *CONTPE*.* CONTPE_????*.*
All the above methods, replaces the first characters in filename with CONTPE, thus outputs are like CONTPE_000254CONTPE_PMLL.txt, which is basically trimming the actual filename however I wanted as CONTPE_1656261000254CONTPE_PMLL.txt.
Screenshot will describe the problem.

Batch file to rename files to specific names

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.

7zip cmd line write a file name with underscores

this seems like a simple one but I can't find it anywhere!
I want to zip up a file with this command:
#"a -tzip -mx0 -v500m -mmt -- C:\Greg_Folder\zippedPackage.zip D:\tmp\failedImages";
This basically means that I'd like to zip up the folder "D:\tmp\failedImages" and move it to "C:\Greg_Folder\zippedPackage.zip"
THE PROBLEM: I want to zip to the "Greg_Folder" and not 'Greg Folder"... but underscores seem to be translated to spaces in the cmd line. So I need a way to say that I want the underscores to be treated literally as part of the directory name.
any ideas?
I just compressed some folder on my drive using your command without the # and the quotes.
"C:\Program Files\7-Zip\7z.exe" a -tzip -mx0 -v500m -mmt -- C:\Greg_Folder\zippedPackage.zip K:\devkitPro\devkitARM\arm-eabi
I just did not put the "#" sign which is for some other option (and your bug seems to be a strange side-effect), and it even created the "Greg_Folder" directory and the .zip.001 file inside.

Why file cannot be found to copy

I am writing a batch script which will copy a file from a folder into the C:\ drive:
#ECHO ON
COPY C:\RANDOMFILES\Weekly Reprort_Hew*.xls C:\Weekly Reprort_Hew???????????.xls
The filename in the RANDOMFILES folder is: Weekly Reprort_Hew, 6-29-2014 10-30-00 PM-642.xls (The date and time and the number at the end will always change so I used the * in the filename being copied in the script)
When I run the batch script, I get the following message:
c:\RANDOMFILES>COPY C:\RANDOMFILES\Weekly Reprort_Hewlett*.xls C:\Weekly Reprort
_Hewlett???????????.xls
The system cannot find the file specified.
How can I fix the issue?
You need double quotes to handle spaces etc. Double check the spelling too.
#ECHO ON
COPY "C:\RANDOMFILES\Weekly Reprort_Hew*.xls" "C:\Weekly Reprort_Hew???????????.xls"
Don't know why it is not working - is it hidden? Maybe the spaces in the name?
The following will work though:
FOR %%I in (C:\RANDOMFILES\Weekly Reprort_Hew*.xls) DO COPY "%%I" C:\
The destination file name isn't necessary; if not otherwise specified, it will remain unchanged provided the destination is different. That may be why the plain COPY command isn't working.
Also:
"It is an not-so-well-known fact that the question mark wildcard will match exactly one character only when the wildcard does not appear at the end of a file name. " from http://www.thefriendlycoder.com/2011/11/24/batch-file-gotcha-question-mark-wildcard/

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?

Resources