1.

Solve : ren command with wild cards?

Answer»

I am trying to rename multiple files in a folder, APPENDING a text to the end of each filename. for example, I am using the following ren command to rename 4 files, but it only renames 2 files correctly, says :"A duplicate file name exists" error for the 1 file:

command: ren *.* *_new.*

SalesDigest_Natl_Trad.pdf
SalesDigest_Natl.pdf
SalesDigest_Atlanta_Reg.pdf
SalesDigest_Atlanta.pdfFile names in the folder: ->


File names after executing the command:->
SalesDigest_new.pdf
SalesDigest_Natl_new.pdf
SalesDigest_Natl.pdf
SalesDigest_Atlanta_new.pdf


As you can see, the results are not as expected. I know there are issues with using ren with wild cards. Any suggestions?

My ultimate goal is to append a timestamp to the end of each file in the folder, for which I am using the following command:
ren *.* *_%date:~7,2%%date:~10,4%.*Code: [Select]@echo off
SET var=_%date:~7,2%%date:~10,4%
for /f "delims=" %%a in ('dir *.pdf /b') do ren "%%a" "%%~na%var%%%~xa"Thank you foxidrive!

This is excatly what I was looking for.

Thanks,
MaxxJust out of curiosity, why does the single line rename command (in my first post) mess up the file names? Thanks!

Btw, below is my final batch file which will rename all the files under a folder except the sub DIRECTORIES.

==============================================================
@echo off
REM This batch file appends the files under the Monthly_Sales folder with date in YYYYMM format
cd Monthly_Sales
if %errorlevel% neq 0 exit /b %errorlevel%
set var=_%date:~10,4%%date:~4,2%
for /f "delims=" %%a in ('dir *.* /b/A:-D') do ren "%%a" "%%~na%var%%%~xa"
cd ..
==============================================================
Quote from: maxxcontractor on September 25, 2012, 03:00:02 PM

Just out of curiosity, why does the single line rename command (in my first post) mess up the file names? Thanks!

The wildcard you used results in two files getting the same new name, so the second one causes an error, and is skipped.

Study these and you will see how the wildcard works

1. SalesDigest_Atlanta.pdf becomes SalesDigest_new.pdf
2. SalesDigest_Atlanta_Reg.pdf becomes SalesDigest_Atlanta_new.pdf
3. SalesDigest_Natl.pdf becomes SalesDigest_new.pdf (this already exists because it was used at step 1)
4. SalesDigest_Natl_Trad.pdf becomes SalesDigest_Natl_new.pdf

The syntax is ren [Drive:][PATH] filename1 filename2. You can use wildcards (* and ?) in either file name parameter. If you use wildcards in filename2, the characters represented by the wildcards will be identical to the corresponding characters in filename1.





Expanding slightly, the syntax is ren file1 file2.

In your ren command:

file1 is *.* which means 'process every file'.

file2 is *_new.* which is the specification for the new name and is interpreted as "everything in the original name up to the last underscore, followed by _new. followed by everything after the last dot in the original name"

This means that these two original file names would get the same new name.

SalesDigest_Atlanta.pdf - > SalesDigest_new.pdf
SalesDigest_Natl.pdf -> SalesDigest_new.pdf

The original name that sorts earliest is processed first, and gets the new name. The second one causes a name collision and is skipped with this error message sent to the console:

C:\>ren *.* *_new.*
A duplicate file name exists, or the file
cannot be found.

If anyone is interested, there is a good summary on Superuser

http://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards


Discussion

No Comment Found