Rename Epub files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Maulz
Posts: 3
Joined: 26 Mar 2013 13:00

Rename Epub files

#1 Post by Maulz » 26 Mar 2013 13:13

Hi!
I have a lot of *.epub-files (5000) with*.opf-file inside each epub. The names of files are not correct (a001, a002...). Inside each -*.opf is info about book in such format:

<*"aut"*>author</dc:creator>
<dc:title*>title</dc:title>

How can i create a *.bat-file, that renames all the archives in format author-title (Paulo Coelho-Achhemist)? Thanks for help.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: Create a bat-file

#2 Post by mfm4aa » 26 Mar 2013 13:33

How do you open *epub to see *opf & inside *opf the info?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Create a bat-file

#3 Post by foxidrive » 26 Mar 2013 13:42

This uses 7-Zip in the default location.

It creates renbooks.bat.txt with the rename commands - examine it in notepad to see if it is what you want.

It writes *.opf files to, and deletes them from, the current folder - if you have *.opf files there then move them.


Code: Select all

@echo off
for /f "delims=" %%a in ('dir *.epub /b /s /a-d') do call :next "%%a"
cls
echo done
echo check renbooks.bat.txt in notepad, and rename to .bat and run if ok.
pause
goto :EOF

:next
set "auth="
set "title="
del *.opf 2>nul
"c:\Program Files\7-Zip\7z.exe" e "%~1" -r *.opf
for %%b in (*.opf) do (
for /f "tokens=3,4 delims=<>" %%c in ('type "%%b"') do (
if /i "%%d"=="/dc:creator" set "auth=%%c"
if /i "%%d"=="/dc:title" set "title=%%c"
)
)
del *.opf 2>nul
if defined auth if defined title (
>>renbooks.bat.txt echo ren "%~1" "%auth% - %title%%~x1"
) else (
>>renbooks.bat.txt echo :: "%~1" has no author - title
)

Maulz
Posts: 3
Joined: 26 Mar 2013 13:00

Re: Rename Epub files

#4 Post by Maulz » 27 Mar 2013 14:12

Thank You!!!
And how do i use REN command to rename many files (about 2000) to get result like

z00o1.zip, z0002.zip ("serialize" file names)

Thanks again!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename Epub files

#5 Post by foxidrive » 27 Mar 2013 16:33

Here's a tool to help:


Code: Select all

   ::Renumber.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::
   :: 2004-03-10 - Idea from foxidrive in a.m.b.nt
   :: 2004-03-10 - Enhanced by Matthias Tacke
   :: 2004-03-11 - Rejigged again by foxidrive
   @echo off
   setlocal enableextensions
   if [%1]==[] goto :Usage
   :: the following two lines disable the persistent Offset/RenInc
   set /A Offset=1
   set /A RenInc=1
   if not [%2]==[] set    Prefix=%~2
   if not [%3]==[] set /A NumLen=%3
   if not [%4]==[] set /A Offset=%4
   if not [%5]==[] set /A RenInc=%5
                   set    RealExt=%~x1
                   set    TempExt=%RealExt:.=.tmp_%
                   set    DrvPath=%~dp1
   if NOT defined Offset set /A Offset=1
   if NOT defined RenInc set /A RenInc=1

   :: Check for valid files to rename
   for /f %%A in ('dir %1 /a-d /b 2^>nul^|find /c /v ""') do set RenCnt=%%A
   if %RenCnt%==0 echo There are no "%1" files to rename. & goto :EOF
   set /A RenMax=(Offset+(RenCnt*RenInc))-RenInc

   :: Check if previous UNDO file exists
   set ask=
   if exist "%DrvPath%UndoRenumb.cmd" (
     echo The last UNDO file "%DrvPath%UndoRenumb.cmd" is still usable,
     set /p ask="do you wish to overwrite it? (Y/N) :"
   )
   if exist "%DrvPath%UndoRenumb.cmd" (
     if /i [%ask%]==[Y] (del "%DrvPath%UndoRenumb.cmd") else (goto :EOF))


   :: Routine to check the highest number used
   :: and increase NumLen if required
   set ask=
   set count=0
   :findlength
   set /a count+=1
   call set RenMaxLen=%%RenMax:~0,%count%%%
   if not %RenMaxLen%==%RenMax% goto :findlength
   if NOT defined NumLen set /a NumLen=%count%
   if %NumLen% LSS %count% (
     echo The Numeral Length has been changed from %NumLen% to %count%
     echo to accomodate the highest numeral needed for the rename - %RenMax%
     set /p ask="Continue? [Y/N] :"
     set /a NumLen=count
   )
     if /i [%ask%]==[N] goto :EOF


   :: Body of routine to pass each filename to the Renumb routine
   :: then rename all files with a temp extension to the real extension
   :start
   for /F "tokens=*" %%A in ('dir %1 /b /on /a-d') do Call :Renumb "%%A"
   Ren "%DrvPath%*%TempExt%" *%RealExt%
   endlocal & set /A Offset=%Offset%& set /A RenInc=%RenInc%
   goto :EOF


   :: routine to do the renumbering and renaming
   :Renumb
   set RenNew=00000000000000000000%Offset%
   call set RenNew=%%RenNew:~-%NumLen%%%
   Ren %1 "%Prefix%%RenNew%%TempExt%"
   >>"%DrvPath%UndoRenumb.cmd" echo Ren "%~dp1%Prefix%%RenNew%%RealExt%" %1
   set /a Offset=Offset+RenInc
   goto :EOF

   :Usage
   echo.Usage:  %0 file.ext ["Pre fix"] [#NumLength] [#Begin] [#Increment]
   echo.
   echo.To rename files to a new name, consisting of a prefix
   echo.followed by an incrementing numeral (and the original extension)
   echo.
   echo."Pre fix"  - is the prefix for your files
   echo.             (use "" if no prefix is desired and other options needed)
   echo.#NumLength - EG: using 3 adds 001 as the numeral
   echo.
   echo.#Begin     - Number to start the renaming at
   echo.
   echo.#Increment - Number to add to successive elements
   echo.
   echo.Example: %~f0 *.jpg "Model Planes " 3
   echo.         will rename all JPG files to "Model Planes nnn.JPG"
   echo.         where nnn is a number from 001 to 999
   echo.
   echo.An UNDO batch file "UndoRenumb.cmd" is generated in the file directory
   ::echo.The Offset and Increment values are preserved between successive runs
   echo.Arguments in brackets are optional but the order is important:
   echo.I.E. If you need increment then all other arguments must be included.
   ::Renumber.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::


Post Reply