0

I've seen plenty of posts on similar requests but I can't quite find one that suits what I am trying to do. It is fairly simple but I cannot seem to get it.

ren FILE??.txt FILE%Year%%Month%%Day%??.txt copy FILE%Year%%Month%%Day%??.txt C:\Users\me\Desktop\Batch\renamed\achod%Year%%Month%%Day%??.txt 

I cannot get the script to keep the '??' which represents random characters the first file may have.

Any help is appreciated.

2
  • Are the ?? characters always in the same position (the 5th and 6th) in the file name and always exactly two characters? Commented Jan 6, 2015 at 19:56
  • yes, it will always be the 5th and 6th Commented Jan 6, 2015 at 20:06

1 Answer 1

1

You won't be able to rename files directly using a wildcard character. Instead you need to locate all the applicable files and then rename each.

The script below works under the assumptions of your question/comments:

  • File name is 6 chars long.
  • Only the last 2 chars are interchangeable.

Of course, the script could be very easily adapted to accomodate other settings but this does just as you requested.

SETLOCAL EnableDelayedExpansion REM Set your Year, Month, Day variable values here. REM They will be used for file renaming. ... CD "C:\Path\To\Files" FOR /F "usebackq tokens=* delims=" %%A IN (`DIR "File??.txt" /B /A:-D`) DO ( REM Extract the last 2 chars of the file name. SET FileName=%%~nA SET First4=!FileName:~0,4! SET Last2=!FileName:~-2! REM Rename the file, inserting the new data. RENAME "%%A" "!First4!%Year%%Month%%Day%!Last2!%%~xA" ) ENDLOCAL 
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to work well. I was mistaken and the file name is actually 7 characters, 5 and then the last 2. I changed the SET FIRST4 to FIRST5 and 0,5 and it seems to work fine. Thanks for your help!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.