1.

Solve : Batch code problem; prevmonth=-1????

Answer»

Hey guys,

Me and my friend have a little problem. We are trainees and we have to create a batch file that does the following:

The first day of a new month it has to put all the .xml files in one .rar file with the year and the month number. Example: 2008_01.rar = January 2008

My friend completed most of it but he isn't doing something right. This is the code right now:


Code: [Select]@echo off
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)

cd c:\"Program Files"\Winrar

set /a prevmonth=month-1

rar a -tb%year%-%month%-01 C:\%year%_%prevmonth%.rar "C:\auto archive\*.xml"


del "C:\auto archive\*.xml"

pause


The problem is when a new month begins, it does -1. So Februari is 02, in the .rar file it becomes 01 (January)
But when you create a file that has been made in December. And you set the clock again to January and we run the batch file. It makes a .rar file but with this name: 2008_0, but it should be 2007_12
Another problem occurred with the month July and August. Then it becomes 2008_-1!

Would SOMEBODY please help us with this ?
Best way would be to add year-end logic:

Code: [Select]@echo off
For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do (
Set Month=%%A
Set Day=%%B
Set Year=%%C
)

set /a month=%month%-1
if %month% equ 0 (
set month=12
set year=%year%-1
)

cd c:\"Program Files"\Winrar
rar a -tb%year%-%month%-01 C:\%year%_%prevmonth%.rar "C:\auto archive\*.xml"
del "C:\auto archive\*.xml"
pause

I cannot find the error for July & August. You don't have a special logic for those months. Can you post how the error occurs for those months?

I've made a screenshot of the DOS box while using your code:


I've mad a .xml file but he only deleted it It didn't make a .rar file in the C: directory...1. I don't know why people have to chop up the date string using a FOR loop and tokens when it is simpler just to slice %date%.

2. The date string is different in different locales. Here in EUROPE, my date string today is

Code: [Select]07/04/2008
I am presuming that both the OP and Sidewinder live in locales where the US type date format applies, viz:

Code: [Select]Day MM/DD/YYYY
So today would be

Code: [Select]Mon 04/07/2008
Is that correct?

In that case, why not just do this

Code: [Select]set /a month=%date:~4,2%
set /a year=%date:~10,4%
3. Beware, because set /a has some hidden TRAPS. For one thing, it interprets numbers with a leading zero as being octal numbers. That is why the problem happens in August, because as you know, 08 (and 09 as well) don't exist in octal.

Therefore, for months with a leading zero, it is best to test if the first character is a zero, and if so, lose it.

4. This won't work

Code: [Select]rar a -tb%year%-%month%-01 C:\%year%_%prevmonth%.rar "C:\auto archive\*.xml"
Because RAR says this

Code: [Select]<switches>
tb<date> Process files modified before <date> in YYYYMMDDHHMMSS format





The company is in Holland yes, but everything is set for the US. So also the dates have to be in that way. I believe then it would be like this:

month/day/year

So i replace the tokens part with this code:

Code: [Select]set /a month=%date:~4,2%
set /a year=%date:~10,4%Try this - assuming %date% format is mm/dd/yyyy

Code: [Select]set /a month=1%date:~,2%-100 %this removes the leading zero if value <10%

set /a year=%date:~-4%
Good luckI have no IDEA what i'm doing wrong

I've created some .xml files in the right directory, i've planted your code in the batch file but still it refuse to created a .rar file in de C: directory... All it does is deleting the .xml files...

This is the code right now:

Code: [Select]@echo off
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)

cd c:\"Program Files"\Winrar

set /a month=1%date:~,2%-100

set /a year=%date:~-4%

rar a -tb%year%-%month%-01 C:\%year%_%prevmonth%.rar "C:\auto archive\*.xml"

del "C:\auto archive\*.xml"

pause

-tb%year%-%month%-01

read what I wrote before
use a language that does date arithmetic better than batch. here's a vbscript
Code: [Select]strMonth=DatePart("m",DateAdd("m",-1,Date))
If Len(strMonth) <= 1 Then
strMonth = "0"&strMonth
End If
strYear = Year(Date)
If strMonth = "12" Then
strYear=strYear-1
End If
strCmd = "rar a -tb " & strYear & "-" & strMonth & ".rar " & """" & "C:\auto archive\*.xml" & """"
WScript.Echo strCmd
Set WshShell = CREATEOBJECT("WScript.Shell")
Set oExec = WshShell.Exec(strCmd)
Do While oExec.Status = 0
WScript.Sleep 100
Loop
WScript.Echo oExec.Status
save the above as script.vbs and on command line;
Code: [Select]c:\test> cscript /nologo script.vbs
I posted this some while ago (and repeated it)

This won't work:

rar a -tb%year%-%month%-01 C:\%year%_%prevmonth%.rar "C:\auto archive\*.xml"

Because RAR /? says this

<switches>
tb<date> Process files modified before <date> in YYYYMMDDHHMMSS format




Discussion

No Comment Found