1

Having previously used batch files to rename files with success, I thought this would be possible, but I just cannot get it to work for this case.

I have the following files which I'd like to rename so that they are ordered correctly:

  • IMG_9963 - no change
  • IMG_E9963 should be renamed to IMG_9963E

So basically, if there is an E, it needs to be moved from where it is to the end of the file name. The rest of the file name should be unchanged.

There may be thousands of files to run through.

To complicate things, the filenames may already have been prefixed so the E may not always be the same place. The E will always be before the numbers though & will always be an E. So examples could be:

WorkIMG_9963
WorkIMG_E9963
AIMG_9963
AIMG_E9963

I've tried lots of different approaches, but coding is not my strength. I've only previously pre-fixed filenames etc. This is the kind of thing I was trying:

setlocal enabledelayedexpansion
for %%j in (*) do (
set old_filename=%%j
set "new_filenameA=!old_filename:E=!"
set "new_filenameB=!new_filenameA!E"
ren "!old_filename!" "!new_filenameB!"
) 

How can I automate this type of renaming task?

13
  • Welcome to SuperUser! We are a community of people that are interested in computers, not a free coding service. Please share what you already have done so that we can find the error, that way it is less work for us and a better suited answer for you. Apr 24, 2022 at 15:54
  • Welcome to Super User. Please note that superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How to Ask.
    – DavidPostill
    Apr 24, 2022 at 16:01
  • Please provide a basic code and explain. If there is an E after the _ the E should be moved to the end of the filename is that it? Apr 24, 2022 at 17:09
  • 1
    The E may not always appear in the same place. -- Examples? Is IMG_E9963 and IMG_99E63 possible? Is it always an "E"? Could it be another letter? Could there be multiple letters? If so, how would you handle them? Is 12345E and 123E45 possible? Would they need to be kept in order? Please consider editing your question to include these details and any other details that may be relevant. Apr 24, 2022 at 21:01
  • @supermonkey the way you put it almost worked exept it moves the E to end of the extension of the filename and not to the end of the filename. Apr 24, 2022 at 22:10

1 Answer 1

0

Well I'm adding this as answer since the topic got reopened now:

@echo off

setLocal EnableDelayedExpansion

if exist "%~1" (if not exist "%~1\" exit) else (exit)
pushd "%~1"
for /f "delims=" %%a in ('dir /b "%~1"') do (
                                             set Name=%%~na
                                             set Name=!Name:E=!
                                             set "NewName=!Name!E%%~xa"
                                             if not "%%~na"=="!Name!" ren "%%a" "!NewName!"
                                            )
popd
1
  • 1
    Probably safer to replace _E with _ (set Name=!Name:_E=_!) than just removing E (there could be one in the prefix)
    – Stephan
    May 12, 2022 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .