.bat et 7-zip

Selon un post sur commentcamarche, j'ai créer 2 fichier .bat pour automatiser la tâche d'archivage de répertoires via compression utilisant 7-zip à destination d'une seule archive zip ou bien d'un fichier zip par dossier présent dans la liste des dossiers à sauvegarder.

7z.listfiles.txt

Ce fichier permet d'indiquer les fichiers et dossiers à sauvegarder (1 par ligne)

C:\7zip
C:\apache-tomcat*
C:\Documents and Settings\monutilisateur

7z.backup.bat (version tout à destination d'un fichier zip unique)

@ECHO OFF
 
rem http://www.framasoft.net/article1902.html
rem PATH ;
rem annule les chemins actifs précédemment établis.
rem PATH "C :\Program Files\ORDI\7-Zip"
rem rend actif les fichiers exe du reptr sité, du rep ou l’on se trouvera
rem CD "c :\Mes documents"
rem va ou se trouve l’archive à actualiser
rem ECHO (MISE A JOUR DE L’ARCHIVE1.ZIP GARCE A 7-ZIP et FRAMASOFT)
rem ligne de commande de 7-zip pour actualiser l’archive
rem 7za u -tzip archive1.zip *.txt
rem Met à jour l’archive1.zip ou la crée.
 
rem http://www.commentcamarche.net/forum/affich-4801881-compression-de-fichiers-a-l-aide-d-un-bat
rem 7z a -tzip archive.zip @listfile.txt 
 
rem 7-Zip installé dans :
rem "C:\Program Files\7-Zip\7zFM.exe"
rem 7z.exe
 
rem %PATH%
 
 
rem C:\Documents and Settings\toto>7z
rem 
rem 7-Zip 4.57  Copyright (c) 1999-2007 Igor Pavlov  2007-12-06
rem 
rem Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...]
rem        [<@listfiles...>]
rem 
rem <Commands>
rem   a: Add files to archive
rem   b: Benchmark
rem   d: Delete files from archive
rem   e: Extract files from archive (without using directory names)
rem   l: List contents of archive
rem   t: Test integrity of archive
rem   u: Update files to archive
rem   x: eXtract files with full paths
rem <Switches>
rem   -ai[r[-|0]]{@listfile|!wildcard}: Include archives
rem   -ax[r[-|0]]{@listfile|!wildcard}: eXclude archives
rem   -bd: Disable percentage indicator
rem   -i[r[-|0]]{@listfile|!wildcard}: Include filenames
rem   -m{Parameters}: set compression Method
rem   -o{Directory}: set Output directory
rem   -p{Password}: set Password
rem   -r[-|0]: Recurse subdirectories
rem   -scs{UTF-8 | WIN | DOS}: set charset for list files
rem   -sfx[{name}]: Create SFX archive
rem   -si[{name}]: read data from stdin
rem   -slt: show technical information for l (List) command
rem   -so: write data to stdout
rem   -ssc[-]: set sensitive case mode
rem   -ssw: compress shared files
rem   -t{Type}: Set type of archive
rem   -v{Size}[b|k|m|g]: Create volumes
rem   -u[-][p#][q#][r#][x#][y#][z#][!newArchiveName]: Update options
rem   -w[{path}]: assign Work directory. Empty path means a temporary directory
rem   -x[r[-|0]]]{@listfile|!wildcard}: eXclude filenames
rem   -y: assume Yes on all queries
 
set nomDuFichierArchive=7z.archive
set typeArchive=zip
set nomCompletDufichierArchive=%nomDuFichierArchive%.%typeArchive%
 
echo "Suppression de la précédente archive : %nomCompletDufichierArchive%"
del %nomCompletDufichierArchive%
 
echo "Debut de compression"
 
set PATH=%PATH%;C:\Program Files\7-Zip;
 
7z a -t%typeArchive% %nomCompletDufichierArchive% @7z.listfiles.txt 
 
echo "Fin de compression du fichier : %nomCompletDufichierArchive%"
 
rem pause

7z.backup.bat (version tout à destination d'un fichier zip par entrée dans la liste du fichier 7z.listfiles.txt)

Ce programme se lance avec un argument (celui du fichier contenant les entrées à compresser) :

7z.backup.fichier.par.fichier.bat 7z.listfiles.txt
@ECHO OFF
 
set PATH=%PATH%;C:\Program Files\7-Zip;
 
rem FOR /F %%i IN (%1) Do call :LISTER %%i
rem FOR /F %%i IN (%1) Do call :ZIPER %%i
FOR /F "delims=" %%G IN (%1) DO CALL :ZIPER "%%G"
 
:LISTER
set nomDuFichierArchive=%1%
set typeArchive=zip
set nomCompletDufichierArchive=%nomDuFichierArchive%.%typeArchive%
echo %nomCompletDufichierArchive%
GOTO FIN
 
:ZIPER
set nomDuFichierArchive=%1%
set typeArchive=zip
set nomCompletDufichierArchive=%nomDuFichierArchive%.%typeArchive%
 
rem echo "Suppression de la précédente archive : %nomCompletDufichierArchive%"
rem del %nomCompletDufichierArchive%
 
echo "Debut de compression de : %nomCompletDufichierArchive%"
 
7z a -t%typeArchive% %nomCompletDufichierArchive% %nomDuFichierArchive%
 
echo "Fin de compression du fichier : %nomCompletDufichierArchive%"
GOTO FIN
 
: FIN
rem pause

Problème d'inclusion du nom des répertoires et dossiers lors de la création de l'archive

Comment garder l'indication du chemin dans l’archive ? A défaut de trouver comment, j'ai fait un script .bat qui lit un fichier .txt se place à la racine du dossier dont on veut garder l'arborescence, et qui ajoute ces dossiers/fichiers dans le .zip ou .tar.gz :

7z.from.read.file.to.keep.folders.bat

@echo off
 
set rootFolderToSaveZip=C:\miseenproduction\
 
set listeDesFichiersAzipper=%rootFolderToSaveZip%\7z.listfiles.txt
 
set nomDuFichierArchive=7z.archive
rem set typeArchive=zip
set typeArchive=tar
set nomCompletDufichierArchive=%rootFolderToSaveZip%\%nomDuFichierArchive%.%typeArchive%
 
set rootFolderToZip=C:\monprojet\
 
IF EXIST %nomCompletDufichierArchive% (
   echo Suppression de la precedente archive : %nomCompletDufichierArchive%
   del %nomCompletDufichierArchive%
) ELSE (
rem   echo %nomCompletDufichierArchive% non trouve.
)
 
echo Debut de compression
 
set PATH=%PATH%;C:\Program Files\7-Zip;
 
rem 7z.exe a -t%typeArchive% %nomCompletDufichierArchive% @7z.listfiles.txt 
 
cd %rootFolderToZip%
 
rem for /f "delims=" %%i in ('type 7z.listfiles.txt') do echo %%i
for /f "delims=" %%i in ('type %listeDesFichiersAzipper%') do 7z.exe a -t%typeArchive% %nomCompletDufichierArchive% %%i
 
echo "Fin de compression du fichier : %nomCompletDufichierArchive%" 
 
cd %rootFolderToSaveZip%
 
 
rem Ceci ne conserve pas le dossier et place tous les fichiers à la racine du .zip
rem 7z.exe a -tzip 7z.archive.zip @7z.listfiles.txt
rem 7z.exe a -tzip 7z.archive.zip C:\monprojet\admin\fichier.php
 
rem Ceci conserver le dossier
rem cd C:\monprojet\
rem 7z.exe a -tzip C:\miseenproduction\7z.archive.zip admin\fichier.php
 
 
rem 7z.exe : http://www.7-zip.org/
rem 7-Zip [64] 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03
rem 
rem Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...]
rem        [<@listfiles...>]
rem 
rem <Commands>
rem   a: Add files to archive
rem   b: Benchmark
rem   d: Delete files from archive
rem   e: Extract files from archive (without using directory names)
rem   l: List contents of archive
rem   t: Test integrity of archive
rem   u: Update files to archive
rem   x: eXtract files with full paths
rem <Switches>
rem   -ai[r[-|0]]{@listfile|!wildcard}: Include archives
rem   -ax[r[-|0]]{@listfile|!wildcard}: eXclude archives
rem   -bd: Disable percentage indicator
rem   -i[r[-|0]]{@listfile|!wildcard}: Include filenames
rem   -m{Parameters}: set compression Method
rem   -o{Directory}: set Output directory
rem   -p{Password}: set Password
rem   -r[-|0]: Recurse subdirectories
rem   -scs{UTF-8 | WIN | DOS}: set charset for list files
rem   -sfx[{name}]: Create SFX archive
rem   -si[{name}]: read data from stdin
rem   -slt: show technical information for l (List) command
rem   -so: write data to stdout
rem   -ssc[-]: set sensitive case mode
rem   -ssw: compress shared files
rem   -t{Type}: Set type of archive
rem   -v{Size}[b|k|m|g]: Create volumes
rem   -u[-][p#][q#][r#][x#][y#][z#][!newArchiveName]: Update options
rem   -w[{path}]: assign Work directory. Empty path means a temporary directory
rem   -x[r[-|0]]]{@listfile|!wildcard}: eXclude filenames
rem   -y: assume Yes on all queries
 
 
rem IF DOS : http://www.computerhope.com/if.htm
rem 
rem If Command Extensions are enabled IF changes as follows:
rem 
rem IF [/I] string1 compare-op string2 command
rem IF CMDEXTVERSION number command
rem IF DEFINED variable command
rem 
rem where compare-op may be one of:
rem 
rem EQU - equal
rem NEQ - not equal
rem LSS - less than
rem LEQ - less than or equal
rem GTR - greater than
rem GEQ - greater than or equal
rem 
rem and the /I switch, if specified, says to do case insensitive string compares. The /I switch can also be used on the string1==string2 form of IF. These comparisons are generic, in that if both string1 and string2 are both comprised of all numeric digits, then the strings are converted to numbers and a numeric comparison is performed.
 
 
rem pour quoter le > dans le echo 
rem Quoting Windows HELP:
rem 
rem Conditional processing symbols
rem Conditional processing symbols give you control over the execution of commands.
rem 
rem Processing Commands Conditionally
rem 
rem You use conditional processing symbols to issue multiple commands from the same prompt and to act based on the results of a command.
rem 
rem The ampersand (&) separates multiple commands on one command line.
rem The parentheses groups multiple commands.
rem The semicolon or comma (; ,) separate command parameters.
rem The caret (^) cancels a subsequent command symbol's special meaning so you can use a command symbol as text.
rem The double ampersand (&&) causes the command following this symbol to run only if the command preceding the symbol is successful.
rem The double pipe (||) causes the command following this symbol to run only if the command preceding the symbol fails.
 
 
rem Seconde passe pour une conversion .tar => .tar.gz
IF /I %typeArchive% EQU tar goto tar2targz
 
 
:tar2targz
rem Creating .tar.gz Archive Easily in Windows : http://www.tmsnetwork.org/blog/creating-targz-archive-easily-windows Creating .tar.gz Archive Easily in Windows
echo Il s'agit d'un .tar =^> conversion en .tar.gz
 
7z.exe a -tgzip 7z.archive.tar.gz %nomCompletDufichierArchive%

7z.listfiles.txt

fichier1.alaracine.txt
*.php
admin\fichier.php
user\fichier.php

Ressources