Rename file names in bulk

October 19, 2012 at 13:14:15
Specs: MS Windows 7 Ultimate 64-bit SP1, AMD Athlon II X3 435 4 gigs
I would like to rename many file names from "Title - Author.epub" to "Author - Title.epub". All the book file names reside in one directory (folder).

See More: Rename file names in bulk

Report •


#1
October 19, 2012 at 17:46:10
::==== begin script
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1,3 delims=-." %%a in ('dir /b *-*.epub') do (
set xx=%%b-%%a
set xx=!xx:~1,-1!
echo ren %%a-%%b.epub !xx!.epub
)

::===== end script

Although I'm about sure this will be prone to failure unless your naming conventions somehow eliminate "-" from book's titles (and author's names, for that matter, since people do have hyphenated names. Then, if a hyphenated author writes a hyphenated title, well, "houston, we have a problem"). I'll work on smoothing this path-to-destruction after feedback (and maybe a healthy sample of the directory listing).


Report •

#2
October 20, 2012 at 06:37:03
If I change " - " to "@@@", what will be the revised code? Thanks

Report •

#3
October 20, 2012 at 06:44:07
The code will turn to:
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1,3 delims=@@@." %%a in ('dir /b *@@@*.epub') do (
set xx=%%b@@@%%a
set xx=!xx:~1,-1!
echo ren %%a-%%b.epub !xx!.epub
)


Report •

Related Solutions

#4
October 20, 2012 at 11:44:43
I have tried the revised code and still not works. In one folder (O:\TMP), I only have the following file (Title@@@Author.epub) and the batch file (Rename2.bat), which is your code. I expect that the file name will be changed to: Author@@@Title.epub

Report •

#5
October 20, 2012 at 16:21:07
delims clause will only support 1-byte arguments, so @@@ would not work as a delimiter, only @ will. Just about any character will work as a delims argument, but some may need escaped. I don't know which ones, i always forget and have to trial-error till i get it to work. If you could sacrifice the spaces surrounding the hyphen (or whichever other) delimiter, you could nail it with a one-liner:

@for /f "tokens=1,3 delims=-." %%a in ('dir /b *-*.epub') do echo ren %%a-%%b.epub %%b-%%a.epub

If you want to pick a delim that is less likely to appear in a title, you might try the tilde (~) or caret (^) or underscore (_), but of course, nothing guarantees that a title won't come along with one of those in it (freedom of speech and all that). If this is just a one-time deal, then you know what you're dealing with, otherwise, the field is open (in which case, you would need some tests in your code, but it could still work).


Report •

Ask Question