Jump to content

Renaming files with a batch file?


Recommended Posts

Basically, I'm trying to make a .bat file that players can use to rename a bunch of mp3s that they place in a folder to file names that my esp will recognize.

 

I don't know anything about non-beth scripting (sad, sad), so I expanded on a .bat file that comes with the new vegas radio extender and wound up with this:

 

 

 @Echo Off

pause

setLocal EnableDelayedExpansion
set extension=mp3
set index=1

for %%f IN (*.%extension%) do (
set /A index+=1
set index=!index!
ren "%%f" "SOFOLong!index!.%extension%"
)

@echo [Done!]

pause

:end [/Code]

 

 

which replaces the names with "SOFOLong" + number. So far so good.

2 problems though:

* The first 9 will display as SOFOLong1/2/3/etc.mp3 but what I'd really want is SOFOLong01/02/03/etc.mp3. Anybody know how to do this, or if it can be done at all? (If not, that's ok, it's just a format preference. I can still redirect the file paths in my esp.)

 

* if any of the source files' names starts with a number, the renaming process seems to skip a count and start with SOFOLong2.mp3 instead of SOFOLong1.mp3. I'd like to avoid this, make it foolproof.

 

Any insight appreciated, even if it's saying "you're SOL".

Link to comment

Well, I really can't help too much other than to post a couple of bat files that were written by the Japanese guys who made lovers with PK. They changed formats for one of the PK versions and issued these bat files for folks to convert their sounds to the new format layout. It is for oblivion so it might or might not help.

 

 

I pulled the previous version and included another that changes all the gibberish to a machine translation - should be easier to understand now.

Link to comment

DoctaSax, I could create a bash script under Linux to do that easy enough, but since we need Windows for for most games, you might try a variation of the following. The command executes a for loop, initializing the counter i with the first argument (0), steps the counter with the second argument (1) and stops when the counter exceeds argument number 3 (9). The % sign in front of i shows it is a variable.

This works only for 0 to 9, you will need to adjust slightly, if you go from 10 to 99, etc. that is each time an extra digit changes in the zero padding: for /l %i in (0, 1, 9) do rename test-tmp-0000%i.* test-tmp-%i.*

Link to comment

Thanks Slammer. I do fear I need it spelled out a bit more - hate to pull the whole damsel-in-distress act, but I don't know the syntax, the functions or anything. The only bit in that code that was mine was adding the prefix to the output :blush:

 

the initial "set index=1" should be 0 (mistyped), but the problem with the count starting with 2 instead of 1 persists when there is any number in any of the file names already. That's the main thing that bugs me: mp3s tend to have numbers.

(The leading zeroes for numbers under 10 is more something I'd like to have so I don't need to reset file paths for 72 audio entries - doable, but mindnumbingly repetitive.)

 

Recap: let's say there can be anywhere between 15 and 40 randomly named mp3s in a given folder (8 folders), then how I get a neat rename going that replaces the names with a given prefix (SOFOLong, SOFOLove etc) and a correct count starting at 1 or 01?

Like going from left to right here:

 

Link to comment

If that's the case DoctaSax you might try it this way:

@echo off

 

REM Meant to be run in a directory that you choose. But DO NOT run this in the Windows directory!

 

REM Prefix to add to the filename

set strPrefix=Green -

 

REM Set this to be the files that you want to prefix with the "strPrefix" above "*.*" means all the files

REM set fname=*.*

set fname=tst.bat

 

REM List the files to be renamed

for %%f in (%fname%) DO echo Rename file "%%f" to "%strPrefix%%%f"

 

REM Rename the files

REM for %%f in (%fname%) DO ren "%%f" "%strPrefix%%%f"

Link to comment

Ah, well, in the mean time I shamelessly adapted something from elsewhere & it seems to stop the misnumbering:

 


@Echo Off

SETLOCAL ENABLEDELAYEDEXPANSION
SET Count=1
FOR /F "tokens=*" %%A IN ('DIR /OD /B *.mp3') DO (
REN "%%~A" SOFOLong!Count!.###
REN *.### *.mp3
SET /A Count = !Count! + 1
)
ENDLOCAL [/Code]

 

Doesn't seem to play nice with whatever I try to do to add leading zeroes, but at least the count is right. And it'll probably be faster for me to change the filepaths for the 'under-ten' files than spending more time on getting those leading zeroes right. That was just a convenience/preference thing, and I need to get back to work on story & effects.

Thanks anyhow :)

Link to comment

Why don't you write it using Windows Script Host? Be a lot simpler and more powerful. Can even use the SDK's Digital Certificate tool to create a signed certificate to avoid issues with users who have WSH set to only run trusted certificate scripts using WSH. It's optional for Win95/NT and guaranteed to be on all other versions of Windows since Win98 up to Win7.

Link to comment

Why don't you write it using Windows Script Host? Be a lot simpler and more powerful. Can even use the SDK's Digital Certificate tool to create a signed certificate to avoid issues with users who have WSH set to only run trusted certificate scripts using WSH. It's optional for Win95/NT and guaranteed to be on all other versions of Windows since Win98 up to Win7.

 

Well, what I have right now is functioning now that I've corrected a few filepaths - took less time than I feared - so I'll leave it at that. Just needed to have something in place so that users wouldn't have to manually rename their tunes and wind up not doing it.

Link to comment

Why don't you write it using Windows Script Host? Be a lot simpler and more powerful. Can even use the SDK's Digital Certificate tool to create a signed certificate to avoid issues with users who have WSH set to only run trusted certificate scripts using WSH. It's optional for Win95/NT and guaranteed to be on all other versions of Windows since Win98 up to Win7.

 

Well' date=' what I have right now is functioning now that I've corrected a few filepaths - took less time than I feared - so I'll leave it at that. Just needed to have something in place so that users wouldn't have to manually rename their tunes and wind up not doing it.

[/quote']

 

Yeah, it was more for future reference... figured you'd already solved your issue.

Link to comment
  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. For more information, see our Privacy Policy & Terms of Use