Question

I am trying to write a batch file to find all zip, rar, tar.gz and re-compress them as a 7zip self executable. I am able to get it to store the files found and scanning trough them but i cannot seem to get it to compress... or everything in the do() to work. I also need it to store the text file so i can check before it starts the process and remove any unnecessary files

@echo off
::Scan for archives and store to file
dir /b /x *.*  |  find "rar" > rfound.txt
dir /b /x *.*  |  find "zip" > zfound.txt
dir /b /x *.*  |  find "tar.gz" > tfound.txt

echo Please check found files then hit enter
pause>nul

::Uncompress and recompress as 7z
for /f "tokens=1 delims=;" %%i in (rfound.txt) do ( 7za.exe e -y -otmp %%i * & pushd tmp & ..\7za.exe a -y -r -t7z ..\%%i * & popd & rmdir /s /q tmp )
for /f "tokens=1 delims=;" %%i in (zfound.txt) do ( 7za.exe e -y -otmp %%i * & pushd tmp & ..\7za.exe a -y -r -t7z ..\%%i * & popd & rmdir /s /q tmp )
for /f "tokens=1 delims=;" %%i in (tfound.txt) do ( 7za.exe e -y -otmp %%i * & pushd tmp & ..\7za.exe a -y -r -t7z ..\%%i * & popd & rmdir /s /q tmp )

pause
Was it helpful?

Solution 2

I ended up extracting all the files by selecting all with extract /*, then compressing all folders

@echo off
for /d %%a in (*) do (7za.exe a %%~na.exe -mmt -mx5 -sfx ".\%%a\*" & RD /s /q %%a)
pause

And by the way i downloaded the 7z cmd line standalone and library for self extract and placed them in the same folder.

I saved over 4GB in space in my development folder. :)

OTHER TIPS

Test this: set the variable to the 7-zip folder

The quotes allow you to support long filenames.

@echo off
::Scan for archives and store to file
dir /b /a-d *.rar *.zip *.tar.gz > found.txt

echo Please check found files then hit enter
pause>nul

set "7z=c:\program files\7-zip\7za.exe"

::Uncompress and recompress as 7z
for /f "delims=" %%a in (found.txt) do (
"%7z%" e -y -otmp "%%a"
   pushd tmp
      "%7z%" a -y -r -t7z "..\%%~na.7z" *
      if errorlevel 1 echo an error occurred & pause
   popd
rmdir /s /q tmp
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top