Link to home
Create AccountLog in
Avatar of rye004
rye004Flag for United States of America

asked on

Windows batch file and supporting spaces in file path.

I have written the following Windows batch file, which searches for log files and makes individual zip files.  The batch file works well, however it does not when there are spaces in the file path.   It fails if the location or the files within have spaces.  Does anyone have suggestions on updating this to support spaces in the path?


@echo off

setlocal enabledelayedexpansion


echo ---------------------------------------------------------------------------------------------------------

echo Usage - zipdata

echo This BAT file compresses all LOG files in the specified location into ZIP files and encrypts them with a password.

echo ---------------------------------------------------------------------------------------------------------


:start

set "location="

set "password="

set "success=true"

set "compressed_count=0"

set "error_count=0"


set /p location=Enter the location of the LOG files (folder path):

if not exist "%location%" (

    echo The specified location does not exist.

    goto start

)


set /p password=Enter the password to use for encryption (8 characters or more, including punctuation and numeric chars):

if "%password%"=="" (

    echo Password cannot be empty.

    goto start

)


for /r "%location%" %%F in (*.LOG) do (

    set "LOGfile=%%~nxF"

    set "zipfile=%%~dpnF.zip"

    echo Compressing "!LOGfile!" to "!zipfile!" ...

    "C:\Program Files\WinZip\winzip64.exe" -a -s"!password!" "!zipfile!" "%%F" >nul

    if exist "!zipfile!" (

        echo !LOGfile! has been compressed successfully.

        set /A compressed_count+=1

    ) else (

        echo Error compressing !LOGfile!

        set /A error_count+=1

        set "success=false"

    )

)


if "%success%"=="true" (

    echo ---------------------------------------------------------------------------------------------------------

    echo All LOG files in the location have been compressed and encrypted with the password.

    echo Total LOG files compressed: %compressed_count%

    echo ---------------------------------------------------------------------------------------------------------

) else (

    echo ---------------------------------------------------------------------------------------------------------

    echo Compression and encryption process encountered errors. Please check the output for details.

    echo Total LOG files successfully compressed: %compressed_count%

    echo Total LOG files with errors: %error_count%

    echo ---------------------------------------------------------------------------------------------------------

)



Avatar of Qlemo
Qlemo
Flag of Germany image

I cannot see a reason why the script should fail, as you have enclosed all paths and file names in double quotes. Do you know which part exactly fails?

Avatar of ☠ MASQ ☠
☠ MASQ ☠

Should this line: or /r "%location%" %%F in (*.LOG) do ( 

read: or /r "%location%" "%%F" in (*.LOG) do (   

???

Avatar of rye004

ASKER

Qlemo, When I echo the final command being run, the trailing and proceeding double quote does not appear to be there.  


MASQ, when I make that change, I get the error "%F" was unexpected at this time.”.


Thank you both for your help.


I agree that it's not clear why this script is failing.  Are you sure it's because of spaces and not special characters like &, ^, <, or > ?

set /p tmploc=Enter the location of the LOG files (folder path):
set location="tmploc"
if not exist %location% (
    echo The specified location does not exist.
    goto start
)

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of ☠ MASQ ☠
☠ MASQ ☠

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account