'use batch scripting and 7-zip to compress files
I have a folder composed of files :
"1.txt"
"2.txt"
I need to compress them in a zip thanks to 7-zip via a batch file. Everything is working well with this script :
7za a my_zip.rar 1.txt 2.txt
I get a my_zip.rar containing the two files.
The problem is that I need to name the zip file with the date at the time the batch file is executed. So I tried this script :
set year=%date:~10,4%
set month=%date:~4,2%
7za a %year%_%month%.rar 1.txt 2.txt
I am getting a folder called "_2" containing a ".rar" containing my 2 files. I would like to have a "2014_12.rar" file containing my 2 files.
EDIT : Script output :
D:\Users>zip.bat
D:\Users>set year=
D:\Users>set month=2/
D:\Users>echo _2/.rar 1.txt 2.txt
_2/.rar 1.txt 2.txt
D:\Users>7za a _2/.rar 1.txt 2.txt
7-Zip (A) 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Scanning
Updating archive _2/.rar
Compressing 1.txt
Compressing 2.txt
Everything is Ok
My zip.bat used :
set year=%date:~10,4%
set month=%date:~4,2%
echo %year%_%month%.rar 1.txt 2.txt
7za a %year%_%month%.rar 1.txt 2.txt
Thanks in advance for your help
Solution 1:[1]
You are cutting the year and month parts incorrectly from your DATE
. As i don't know how the date is formatted in your locale, i just can suggest you echo %DATE%
on the command line and count the characters correctly, remembering that you have to start with 0. (if you have dd/mm/yyyy, that would be 6,4 and 3,2)
Solution 2:[2]
Here is a stock script I use to get date values. This is built with the preface that your localization is US English; you may need to alter it if that is not your case.
@echo off
setlocal EnableDelayedExpansion
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
if "%%B" NEQ "" (
SET /A FDATE=%%F*10000+%%D*100+%%A
SET /A FTIME=%%B*10000+%%C*100+%%E
)
)
SET DatePartYear=%FDATE:~0,4%
SET DatePartMonth=%FDATE:~4,2%
SET DatePartDay=%FDATE:~6,2%
C:
cd\
cd "Program Files\7-Zip"
7z.exe a D:\%DatePartYear%_%DatePartMonth%.rar D:\1.txt D:\2.txt
pause
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ths |
Solution 2 | UnhandledExcepSean |