Batch Rename contents of ZIP file to ZIP file name

2019-06-02 08:32发布

问题:

My question is very similar to:

Rename extracted file based on zip file in Batch

I have 470 .zip files which each contain two files (.mp3 & .cdg) - they are karaoke files.

The ZIP files are cleanly named (Abba - Money Money Money.zip), however the contents, of the ZIP file need to be renamed (e.g FIK001ABBA_02_-ABBA-_MONEY_MONEY_MONEY.mp3).

So, I would like the batch file to do the following:

  • Extract the files
  • Rename the contents, of the zip file to the zip file name.
  • e.g:
  • FIK001ABBA_02_-ABBA-_MONEY_MONEY_MONEY.mp3
  • FIK001ABBA_02_-ABBA-_MONEY_MONEY_MONEY.cdg
  • renamed to
  • Abba - Money Money Money.mp3
  • Abba - Money Money Money.cdg
  • The files are then added back into the zip file

I've looked over the similar posting above, however, it talks about unzipping *.7z my files are *.Zip. Please explain :-)

Here is the code:

md textfiles
for %%f in (*.zip) do (
winzip -v "%%f"
move *.txt textfiles\%%~nf.txt
)
xcopy textfiles\*.txt originalfolder
rd textfiles /s /q

回答1:

@ECHO OFF
SETLOCAL 
(SET workdir=.\textfiles)
IF EXIST "%workdir%\." ECHO Choose a directory name that doesn't exist&GOTO :EOF 
FOR %%f IN (*.zip) DO CALL :process %%f
GOTO :eof

::
::  
::
:process
MD "%workdir%"
wzunzip %1 "%workdir%" >nul
PUSHD "%workdir%"
FOR /f "tokens=1*delims=-" %%i IN ('dir /b /a-d') DO CALL :procren %%i %%j
POPD
:: Not sure whether you want to ADD (as you say) 
:: or REPLACE (seems more logical)
:: 
DEL %1
wzzip -m %1 "%workdir%\*.*" >nul
IF EXIST "%workdir%\." RD "%workdir%" /S /Q
GOTO :eof

:procren %%i %%j
SET filename=%1-%2
SET newname=%2
SET newname=%newname:_= %
SET newname=%newname:-= -%
REN %filename% "%newname%"
GOTO :eof

Just a few notes:

  • I'm not sure whether you want to ADD or REPLACE the content of your .ZIP files. The code will REPLACE. If you truly want to ADD then remove the DEL %1 line.
  • WINZIP and 7ZIP are different, but similar products
  • I use a relatively old version of WINZIP, which has a pair of command-line utilities available (WZZIP and WZUNZIP) aimed at batch use
  • There appears to be a bug with my WZZIP version (v3.2, build 8668) where the -m (move) option will delete the directory if "dirname\*.* is MOVED into the zip. I'll chase this up - but that's why the RD of the work directory is gated and placed with the MD within the :process routine, not in the main routine.