I have a folder with 100 invoices & they all need to carry the prefix "INVOICE_" in front. I tried to run a command but it doesn't seem to be working.
I've tried ren * INVOICE_*.PDF
but it doesn't work. Can someone help me figure this out?
-
softwarerecs.stackexchange.com is the preferred place to ask about apps which you could add to do this, should you be using Windows which does not know how to do that.– K7AAYNov 15, 2018 at 0:36
4 Answers
A quick and dirty batch file which will do what you want.
@echo off
::Create a temporary filename.
set _tmpfile=%random%
::Store the prefix you want to use in the rename in a variable
set _prefix=INVOICE_
::List of files to be renamed are held in the _tmpfile
::so be sure that you are in the correct folder/directory.
dir *.pdf /b>%_tmpfile%
::Now, loop through the _tmpfile and rename each file using the _prefix variable
:: Remove the ECHO below in order for this to work.
for /f %%x in (%_tmpfile%) do ECHO ren %%x %_prefix%%%x
::Delete the _tmpfile
if exist %_tmpfile% del %_tmpfile%
Update: the batch file, as is, won't rename the files. It would just show you the command it would run if you remove the "ECHO" string from the "for /f in %%x" line. Then the batch file will rename the files.
Hope this helps!
-
You might want to explain the bit about doing a dry run and then deleting the
ECHO
. (2) Why don’t you just dofor %%x in (*.pdf) do
and skip the tmpfile? Nov 15, 2018 at 20:56 -
@scott: for #1, I thought the comments in the batch file would be enough. I'll update the answer. For #2, during my testing, the batch would add the prefix twice to the first file. Figured safest thing was to redirect to temp file, then loop that.– JSanchezNov 16, 2018 at 0:44
The ren command works differently than you expect; click on its link to learn more about it. A third party file renaming utility will make this much easier. There are a number of Open Source free programs which are available, such as
https://sourceforge.net/projects/renameit/
http://file-folder-ren.sourceforge.net/
https://sourceforge.net/projects/mfrv02/
https://sourceforge.net/projects/multifilerename/
https://sourceforge.net/projects/freefilerenamer/
https://sourceforge.net/projects/personalrenamer/
http://regexrenamer.sourceforge.net/
http://rename.sourceforge.net/
among others.
Can't make a recommendation of a specific app here, but https://softwarerecs.stackexchange.com is the preferred place to ask about apps.
You question title says “in Windows”,
so I’m going to ignore the cmd.exe tag
and give you a PowerShell solution.
Go into PowerShell
(type powershell
at your cmd
prompt, or run it from the menu)
and type
dir *.pdf | rename-item -newname {$_.name -replace "^","INVOICE_"}
Seems pretty self-explanatory: take the name of each PDF file
and rename it to a new name that’s formed
by replacing the beginning of the old name
(denoted by the regular expression ^
) with “INVOICE_
”.
Copied almost verbatim from How to Batch Rename Multiple Files in Windows at How-To Geek.