1

I need to get a batch script running that takes a source folder and zips every file into its own zip file recursively through all subfolders and save the zip files to a given destination.

Here is a working variant that just takes the source folder zips it and saves the zip to the destination:

 @echo off


 set year=%date:~-4,4%
 set month=%date:~-10,2%
 set day=%date:~-7,2%
 set hour=%time:~-11,2%
 set hour=%hour: =0%
 set min=%time:~-8,2%

 set zipfilename=%~n1.%year%_%month%_%day%_%hour%_%min%
 set destination=%~dp1

 set source="%~1\*"
 set destpath="%~2"
 set destname="%~3"
 set ziptyp="%~4"
 set dest= "%destpath%\%destname%_%year%_%month%_%day%_%hour%_%min%.%ziptyp%"
 REM "%destination%Backups\%zipfilename%.%desttyp%"

 IF [%1]==[/?] GOTO BLANK
 IF [%1]==[] GOTO BLANK
 IF [%1]==[/h] GOTO BLANK
 IF [%1]==[?] GOTO BLANK

 set AppExePath="%ProgramFiles(x86)%\7-Zip\7z.exe"
 if not exist %AppExePath% set AppExePath="%ProgramFiles%\7-Zip\7z.exe"

 if not exist %AppExePath% goto notInstalled

 echo Backing up %source% to %dest%


 if /I %ziptyp%=="zip" ( 
 %AppExePath% a -rtzip %dest% %source%)

 if /I %ziptyp%=="7z" ( 
 %AppExePath% a -rt7z %dest% %source%)


 echo %source% backed up to %dest% is complete!

 goto end

 :BLANK


 goto end

 :notInstalled

 echo Can not find 7-Zip


 :end

For this one I need to change the following parts:

 if /I %ziptyp%=="zip"

and if /I %ziptyp%=="7z"

I tried the following ways and more:

 (
 cd  %source%
 FORFILES %source% /M *.* /C "%AppExePath% a -rtzip "%%~nG.zip" ".\%%G\*"")

or

 FOR /R %source% %%G in (.) do ( 
 Pushd %%G 
 %AppExePath% a -rtzip "%%~nG.zip" ".\%%G\*"
 Popd )

or

 for /R %%a in (%source%) do (zip -r -p "%%~na.zip" ".\%%a\*")

Does anyone have an idea on how to get this working?

Thanks in advance,

TheVagabond

Thevagabond
  • 323
  • 2
  • 9
  • 34

1 Answers1

1

You were close!

I have just tested around a bit and this seems to be able to process every single file in one directory:

@echo off
cd /D "%~1"
for /r %%i in (*) do (
    REM Process file here!
    echo Full path: %%i
    echo Filenames with extension: %%~nxi
)

This moves to the path you gave it as parameter and then using * instead of . will take every file. You can now process it in your desired way.

To check how to modify the outcome to your desire I recommend reading this great answer about the path parameter modification.

Thanks to aschipfl for reminding me of the /D for cd and the modification of paths!

Community
  • 1
  • 1
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • You should add the `/D` option to the `cd` command line to change the drive in case as well; or remove the `cd` command line and change the `for` loop to: `for /R "%~1" %%i in (*) do`... To get the pure file names, use `%%~nxi` instead of `%%i` in the loop... – aschipfl Nov 23 '16 at 14:51
  • Damnit I am always missing this switch! Thanks for reminding! – geisterfurz007 Nov 23 '16 at 15:00