I need to make a condition for unzipping a file using batch.
The method is as follows:
If dummy=1, the batch will unzip an xlsx file to a particular folder, else , it will copy another xlsx file directly to that folder.
my code is as follows:
set dummy=1
If %dummy% EQU 1 (
SET CHEMIN=C:\file_to_unzip
cd %CHEMIN%
for /f %%j in ('dir /b %CHEMIN%\*.zip') do (
C:\PROGRA~1\WinZip\Winzip32 -min -e -o -j %CHEMIN%\%%j %CHEMIN%
)
COPY "C:\file_to_unzip\*.xlsx" "C:\destination"
) else (
COPY "C:\file_to_copy\*.xlsx" "C:\destination"
)
The problem is that without the IF condition, the unzip command will work properly. But once the IF condition is included, the cmd will say that the zip file cannot be found. I am not sure what causes this issue and how to solve this.
Any help appreciated.
Do you have delayed expansion enabled?
setlocal enabledelayedexpansionWith your example code above, the failure will occur due to the CHEMIN variable being set within the if statement. If a variable is set within a parentheses scope, the new value set for that variable will not be available until the all the sub scopes have ended. Run the following script to see what I mean:
If delayed expansion is disabled (which it is by default), the results will be
if enabled, add
setlocal enabledelayedexpansionat the beginningTry your script as follows. To use
Another solution would be to just move the
set CHEMIN=C:\file_to_unzipline outside of the if statement.