1

I'm trying to rename the contents of a zip file to match the name of the zip file with a batch file.

This is exactly what I'm trying to do : https://stackoverflow.com/questions/22853824/zip-file-to-unzip-and-rename-files-and-zip-back/25732864#25732864

it works but only if the zip files do not have a space in the file name. Otherwise it creates a bunch of empty folders where a space occurs in the zip file name.

Phil V's answer works well but I think it just need refined a bit :

:: # Core Logic
:: # Looping through all the zips
for %%c in (*.zip) do (
    :: # Make a temporary folder with the same name as zip to house the zip content
    if not exist %%~nc md %%~nc
    :: # Extracting zip content into the temporary folder
    7z e -o%%~nc %%c
    if exist %%~nc (
        :: # Jump into the temporary folder
        pushd %%~nc
        if exist *.* (
            :: Loop through all the files found in the temporary folder and prefix it with the zip's name
            for %%i in (*.*) do (
                ren %%i %%~nc.%%i
            )
            :: # Zip all the files with the zip prefix with orginal zip name but with a number 2 (abc2.zip)
            if exist %%~nc.* (
                7z a -tzip %%~nc2 %%~nc.*
            )
            :: # Move the new zip back out of the tempory folder
            if exist %%~nc2.zip move %%~nc2.zip ..
        )
        :: # Jump out of the temporary folder
        popd
        :: # Showing you the directory listing
        dir
        :: # Showing you the content inside the new zip
        7z l %%~nc2.zip
        :: # Remove the temporary folder (Clean up)
        rd /s/q %%~nc
    )
)

Update : OK got it working with the help of webmarc (see solution below).

It took some trial and error but eventually I figured out where the quotes are needed to get it to work the way I need. Just like webmarc said "put quotes around any other arguments in your program that may have embedded spaces."

  • What is your actual question? How to make the script work with files that have spaces in them? – Adam Sep 10 '14 at 16:00
  • @Adam - yes that is my actual question, sorry. How to make the script work with files that have spaces in them? – DigablePlanets Sep 10 '14 at 16:32
2

Don't forget, the shell doesn't know the difference between multiple arguments and files with a space in them. You can use quotes to tell the shell when a single argument should have a space in it:

For example, ren %%i %%~nc.%%i, change to ren "%%i" "%%~nc.%%i"

And put quotes around any other arguments in your program that may have embedded spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.