batch-filearchive7zip

Batch script to unarchive multiple archives using 7zip


I'm trying to unarchive a lot of archives in a folder. I'm trying to archive them just outside, without making a folder per archive.

I'm using this code

@echo off
setlocal

rem Set the path to the 7-Zip executable
set "7z_path=C:\Program Files\7-Zip\7z.exe"

rem Set the path to the folder containing the archives
set "source_folder=C:\Users\blabla"

rem Echo the paths for debugging
echo 7z_path is set to: "%7z_path%"
echo source_folder is set to: "%source_folder%"

rem Change to the source folder
cd /d "%source_folder%"

rem Extract each archive file in the folder
for %%i in (*.zip *.rar *.7z) do (
    "%7z_path%" x "%%i" -aoa -o"%source_folder%"
)

endlocal
pause

But it doesn't work with the following error '"z_pathsource_folder"' is not recognized as an internal or external command, operable program or batch file.

When debugging I notice the 7z_path is set to: "z_path" which is not correct obviously, but I don't understand what I did wrong. This problem might be causing the error above.

Any help is appreciated, ty.


Solution

  • Arguments to batch scripts are called with the syntax %1, %2, %3, etc. These variables are expanded to their values before all other variables in the script are. Because you're calling your variable %7z_path%, that's being interpreted as %7 followed by the string z_path%.

    Change your variable name to something that does not start with a number, like %sevenzip_path%.