1.

Solve : Beginner needs help, in renaming a lot of files.?

Answer»

Hello,

I could use some help.
I would like to rename a whole bunch of files.

Now they are named like:              A story about frogs - Mister Greenleg.epub
They should be renamed to:          Mister Greenleg - A story about frogs.epub

How do i do that with a bat file?

Thanks in Advance,
GreenlegThere's probably a 1 line line solution, but I can't find a way of trimming the whitespace WITHOUT a second line.

The below switches the two halves of the file name AROUND the hyphen (and does so for all files in the directory and its subdirectories). It will fail on any files that do not have a hyphen in their name, with error 'The syntax of the command is incorrect.', but will rename all other files before and after the hypenless file:

Code: [Select]For /f "tokens=1,2 delims=-" %%a in ('dir/b /s') do (set p2=%%~nb
call ren "%%~na - %%p2:~1%%%%~xb" "%%p2:~1%% - %%~na%%~xb")Hello Sirim,

Thank you for helping me out.
I tried the code you wrote, but it doenst work.
It reads EVERY file, bit says that it can't find the given file.

C:\Documents and Settings\Jan\Bureaublad\rename-ebooks>(
set p2= Nachtlamp
 call ren "rename - %p2:~1%" "%p2:~1% - rename"
)
Het systeem kan het opgegeven bestand niet vinden.


This is the message i get. (I'm using a dutch XP OS)
The directory where i run the bat file is "rename-ebooks"
The batfile itself is called "rename"
And this message come from the file  "Nachtlamp - Jack Vance.epub"
The last line in dutch says that the system can't find the file given.

Any idea what i'm doing wrong?

GREETINGS, GreenlegMy code wasn't expecting a hypen in the folder name. The below does (this ACTUALLY requires there to be exactly 1 hyphen in the folder path):

Code: [Select]For /f "tokens=2,3 delims=-" %%a in ('dir/b /s') do (set p2=%%~nb
call ren "%%~na - %%p2:~1%%%%~xb" "%%p2:~1%% - %%~na%%~xb")this one works.
Thanks for helping me out.
Saves me a lot of workYou're welcome, glad I could help.Here is a much longer piece of code that crops the name down to the text string and then sets them up the way you show. It is definitely more involved than the previously posted code, but it should provide also prevent some errors from occuring if different spaced names are encountered.

Code: [Select]echo off
setlocal enabledelayedexpansion

for /f "tokens=1,2,3 delims=-" %%A in ('dir /s /b') do (
  set oldfile=%%A-%%B-%%C
  set ext=%%C
  call :skimext
  set P1=%%B
  call :skimP1
  set P2=%%C
  call :skimP2
  set newfile=!P2! - !P1!!ext!
  echo ren !oldfile! !newfile!
  pause
)
goto :eof


:skimext
:extloop
echo %ext% | find "." >nul
if errorlevel 1 goto setext
set ext=%ext:~1%
goto extloop
:setext
set ext=.%ext%
goto :eof

:skimP1
:P1loop1
set P1LC=%P1:~-1%
if not "%P1LC%"==" " goto P1loop2
set P1=%P1:~0,-1%
goto P1loop1
:P1loop2
echo %P1% | find "\" >nul
if errorlevel 1 goto :eof
set P1=%P1:~1%
goto P1loop2

:skimP2
:P2loop1
set P2C1=%P2:~0,1%
if not "%P2C1%"==" " goto P2loop2
set P2=%P2:~1%
goto P2loop1
:P2loop2
echo %P2% | find "." >nul
if errorlevel 1 goto :eof
set P2=%P2:~0,-1%
goto P2loop2
Now this code may seem much longer and much more convoluted, but it should also accomplish what you are wanting to accomplish, with a little error compensation involved as well. When you are satisfied that the code is showing you the correct command that you are wanting to see (you should see the ren !oldfile! !newfile! expanded with correct names) then take the echo out of the second-to-last line and remove the pause on the last line and run the code.



Discussion

No Comment Found