0

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

user2443476
  • 1,935
  • 9
  • 37
  • 66
  • works fine for me, try maybe to use `"` in the rar file name: `"%year%_%month%.rar"` – Elisha Dec 04 '14 at 15:34
  • Ok, because with this exe (7za), adding the double quotes changes nothing. Thanks anyway – user2443476 Dec 04 '14 at 15:37
  • I didn't understand. do you still have the problem? – Elisha Dec 04 '14 at 15:40
  • Yes I also tried with 7z.exe, same result : one folder called "_2" containing ".rar" containing my 2 files. It's like the name in my batch file is not well interpreted, but I don't understand why it's working for you. – user2443476 Dec 04 '14 at 15:42
  • 1
    can you add the line `echo %year%_%month%.rar 1.txt 2.txt` and add the output to the question? – Elisha Dec 04 '14 at 15:49
  • It sounds like you're getting a `/` in `%year%` or `%month%` that is being interpreted as a path separator. `echo %year%_%month%` to see what your variables are being assigned. – Ken White Dec 04 '14 at 16:13
  • year variable is empty and month variable is equal to _2/ – user2443476 Dec 04 '14 at 16:15
  • but I don't know how to get year and date, and then remove the backslash in batch scripting. – user2443476 Dec 04 '14 at 16:15
  • Perhaps use `wmic` to get the date. Win32_Localtime is locale agnostic anyway. See [Method 2 on this page](http://ss64.com/nt/syntax-getdate.html) for a solution. Or if you simply want to remove a slash from a variable value, [see this page](http://www.dostips.com/DtTipsStringManipulation.php) for string manipulations in batch scripting. (Hint: `%var:/=%` will remove slashes from `%var%` at the time of evaluation.) I apologize for picking nits, but `/` is a forward slash, not a backslash. `/` leans forward. `\ ` leans backward, you see. – rojo Dec 04 '14 at 17:04

2 Answers2

0

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)

ths
  • 2,858
  • 1
  • 16
  • 21
0

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
UnhandledExcepSean
  • 12,504
  • 2
  • 35
  • 51
  • the positive thing with `wmic` is, that the output is independent of regional settings. (see [here](https://stackoverflow.com/a/18024049/2152082) for a simpler `wmic` query to get the same result) – Stephan Dec 23 '17 at 13:40