Solved Challenging file renaming batch script

July 31, 2011 at 17:29:44
Specs: Windows XP
Hi guys,
I'm new to batch scripts and desperately need help from experts.
I am trying to write a batch script that will rename all files in a folder in following fashion.
Original files names are 27 letters long, I need the 18th letter replaced by 27th letter and then last 3 letters removed.
For example
xxxxxxxxxxxxxxxxxxxxxxxx_A1
would become
xxxxxxxxxxxxxxxxx1xxxxxx

and
xxxxxxxxxxxxxxxxxxxxxxxx_A5
would become
xxxxxxxxxxxxxxxxx5xxxxxx
etc.

If anyone can guide me in a right direction I would appreciate it.


See More: Challenging file renaming batch script

Report •


✔ Best Answer
August 1, 2011 at 15:18:38
Here's a go based hopefully on the information you have provided re filename length etc.. The script is heavily commented to try to let you know what happens on each line, if you need an explanation please first try entering the command followed by /? at the command prompt (e.g. Set /?) otherwise don't hesitate to post any queries.

Good luck.

W.

@echo off  
cls
setlocal enabledelayedexpansion

pushd C:\files\||echo Pushd failed - job terminated.&&exit /b

for /f "tokens=*" %%1 in ('dir /a-d /b *.drm') do (

    REM get filename only into variable File and extract file extension 
    REM only into var Extn
    set file=%%~n1&set extn=%%~x1

    REM extract first 17 characters of filename into var First and
    REM six characters starting at character number 19 into var Second
    set First=!file:~0,17!&set Second=!file:~18,6!

    REM extract the final character of filename into var Last
    set Last=!file:~-1!

    REM display potential outcome for testing purposes only, files are
    REM NOT yet renamed, see below...
    echo %%1 being renamed !first!!last!!second!!extn!

    REM remove rem on the following line to rename files after testing.
     
    rem ren "%%1" "!first!!last!!second!!extn!"
    echo.
)
popd


Please come back & tell us if your problem is resolved.



#1
August 1, 2011 at 00:59:03
Here's a guide.

Start by creating a directory listing of the bare filenames. Because you haven't shown any extensions I'll assume there are none.

From that listing extract each filename into an environment variable then split that variable into three others containing (a) the first 17 characters, (b) the 6 characters starting at number 19 (c) the last character.

Rename the original file to the new filename contained in (a)(c) and (b) in that order.

Hope this helps, post back if you need further assist.


Please come back & tell us if your problem is resolved.


Report •

#2
August 1, 2011 at 07:31:58
Wahine,
Thanks for your reply.
That's exactly what I was thinking but unfortunately my knowledge is lacking.
I guess I should have said I need a concrete example instead of just guidance :)
How do I set a variable that pulls part of the file name? Do I have to use if-then argument to rename each file or is there a better way to rename all of them at once. Let's use C:/Files as the folder in question and .drm as file extension and let's say there could be random number of files in that folder from 0 to 50.
Thanks,

Report •

#3
August 1, 2011 at 08:36:10
Wahine,
Thanks for your reply.
That's exactly what I was thinking but unfortunately my knowledge is lacking.
I guess I should have said I need a concrete example instead of just guidance :)
How do I set a variable that pulls part of the file name? Do I have to use if-then argument to rename each file or is there a better way to rename all of them at once. Let's use C:/Files as the folder in question and .drm as file extension and let's say there could be random number of files in that folder from 0 to 50.
Thanks,

Report •

Related Solutions

#4
August 1, 2011 at 15:18:38
✔ Best Answer
Here's a go based hopefully on the information you have provided re filename length etc.. The script is heavily commented to try to let you know what happens on each line, if you need an explanation please first try entering the command followed by /? at the command prompt (e.g. Set /?) otherwise don't hesitate to post any queries.

Good luck.

W.

@echo off  
cls
setlocal enabledelayedexpansion

pushd C:\files\||echo Pushd failed - job terminated.&&exit /b

for /f "tokens=*" %%1 in ('dir /a-d /b *.drm') do (

    REM get filename only into variable File and extract file extension 
    REM only into var Extn
    set file=%%~n1&set extn=%%~x1

    REM extract first 17 characters of filename into var First and
    REM six characters starting at character number 19 into var Second
    set First=!file:~0,17!&set Second=!file:~18,6!

    REM extract the final character of filename into var Last
    set Last=!file:~-1!

    REM display potential outcome for testing purposes only, files are
    REM NOT yet renamed, see below...
    echo %%1 being renamed !first!!last!!second!!extn!

    REM remove rem on the following line to rename files after testing.
     
    rem ren "%%1" "!first!!last!!second!!extn!"
    echo.
)
popd


Please come back & tell us if your problem is resolved.


Report •

#5
August 1, 2011 at 20:37:53
Wahine,
You rock! This is absolutely awesome, I'm glad I asked for help because it would have taken me forever to figure it out, the comments help a lot too. I've tried this script and it worked perfectly (i.e. exactly what I asked for).
There two other elements of complication that I was just made aware of :( I tried to figure it out on my own but I'm lost again.
1. The file name can be 27 or 26 characters long. In either case I need 18th character replaced with last one and last three characters removed. So second variable would have to be 6 characters if original file name is 27 character long and 5 if it's 26, I have no clue on how to do that.

2. The folder may contain files that have been renamed already. Obviously they will be either 24 or 23 characters long. The current script still renames those files, I need it to ignore them.

Thanks again for your help :)


Report •

#6
August 1, 2011 at 22:40:09
1. The file name can be 27 or 26 characters long. Hey, that's not correct, your original post definitely stated Original files names are 27 letters long, I need the 18th letter replaced by 27th letter and then last 3 letters removed.

2. The folder may contain files that have been renamed already. Obviously they will be either 24 or 23 characters long. Where the heck did they come from? Did you not test and confirm the script I posted? The discrepancies would have shown in the testing stage.

Before I get down to re-writing the script please review the information you have supplied and advise of any other differences in filenames which you want to be considered. Last chance to alter the specifications. Last chance with me anyways, someone else may pick up the cudgel.


Please come back & tell us if your problem is resolved.


Report •

#7
August 3, 2011 at 05:00:43
Wahine,
As I mentioned in my previous post I've tried you script and it worked exactly as it was supposed to. At the time of my original post I wasn't aware that some file names could be 26 characters long so I did state that they would be 27. Anyways, I went over all of possible scenarios and this are my final specs :)
1. The original file name are going to be 26 or 27 characters long. I need 18th character replaced by last one and last 3 characters removed.
2. After the new (one that will work for 26 and 27 characters) script is ran the folder will be full renamed files which will be 23 or 24 letters long. If we drop new files in the folder (26&27 char long) and run the script again to rename them, the script will also rename files that have been renamed already, i.e. replace 18 char by 23 or 24th, thus misnaming them. Is there a way to make the script ignore those files.
I hope this clarifies it and appreciate your time and patience with me :)

Report •

#8
August 3, 2011 at 15:56:10
Okay - the specs I now understand are:

1. The folder may contain files with names of various lengths.
2. All filenames to be processed will have the .drm extension.
3. Only filenames which are 26 or 27 characters in length are to be processed, all others to remain as-is.
4. Character #18 is to be changed to the final character of the filename.
5. The final 3 characters of the filename are to be removed making the new filenames 23 or 24 characters in length.
6. The original file extension is to be retained.

Test this, please confirm that the script is correct before removing the rem command. Here's hoping for success.

Good luck.

@echo off  
cls
setlocal enabledelayedexpansion

pushd C:\Files\||echo Pushd failed - job terminated.&&exit /b

for /f "tokens=*" %%1 in ('dir /a-d /b *.drm') do (
    set First=
    set file=%%~n1&set extn=%%~x1

    set cnt=0
    for /l %%1 in (0,1,30) do (
        set chars=!file:~%%1!
        if defined chars set /a cnt+=1
    )

    if !cnt! gtr 25 if !cnt! lss 28 (
        
    set First=!file:~0,17!
    if !cnt! equ 26 (set Second=!file:~18,5!
       ) else (
       set Second=!file:~18,6!
       )

    set Last=!file:~-1!

    if defined First echo %%1 being renamed !first!!last!!second!!extn!&&echo.

    rem ren "%%1" "!first!!last!!second!!extn!"

  )
)

popd


Please come back & tell us if your problem is resolved.


Report •

#9
August 3, 2011 at 19:36:11
Wahine,
You nailed every request, worked perfectly! :)

Hey, I know you can't do much with just a thank you so can you PM me, I would like to PayPal you $25 so you can have a dinner on me, and thanks again!


Report •

#10
August 3, 2011 at 19:56:44
Thank you for the very generous offer. If you have got that much to spare please consider donating it to your local Breast Cancer Support Group or to the Breast Cancer Foundation where it will do more good than adding to my waistline!! LOL

(The moderator may remove the above as it could be considered as soliciting for donations).

Thank you for coming back so promptly to report your success.

Kind regards.

Wahine.


Please come back & tell us if your problem is resolved.


Report •


Ask Question