2

I've been searching a lot about how to convert a string into a integer on windows batch but I just can't get how to do it.

I've written a batch that copies a file from a directory to another and name it after the current date. My code goes something like this:

set FILE=log %date:~6,4%-%date:~3,2%-%date:~0,2% copy C:\log.txt C:\"%FILE%".txt

I need to do the conversion to make FILE be one day previous. For example, if I run the script right now, I'll get a file named "log 2014-07-09", but I want it to be "log 2014-07-08".

I've tried with this few sentences but I get an error about the numeric value is not valid:

set DAY=%date:~0,2% :: this assignation give me no problems
set /A DAY = %DAY%-1 :: this is the one that makes my head breaks

I don't know where the error is because I barely know windows batch sentences. I've read about the SET command syntax at Help command and other sites (like this one) but I can't get where I'm failing. I know this may be very simple (on some programming languages a Parse sentence would be enough), but I'm a total novice on this batch script stuff, so any help will be much appreciated. :)

6
  • 2
    The curse of the leading zero. Commented Jul 9, 2014 at 21:26
  • PowerShell would make all of this a whole lot simpler: "{0:yyyy-MM-dd}.txt" -f (get-date).AddDays(-1). Commented Jul 9, 2014 at 21:28
  • 1
    Sorry, but I don't know what PowerShell is. Commented Jul 9, 2014 at 22:04
  • You copy command is wrong. 1/ Remove the quotes around %file%. 2/ %file% equate to log /07/- 1-Th so your copy command is copy C:\log.txt C:\"log /07/- 1-Th".txt. That does not look right. Commented Jul 9, 2014 at 23:29
  • 1
    Will this be executed the fist day of month? The previous day will fall in the previous month. Commented Jul 10, 2014 at 7:48

4 Answers 4

1

A way to resolve it :

@echo off 
set "$DateNew=%date:~0,2%"
set /a "$DateNew=%$DateNew:0=%-1"
if %$DateNew%==0 set "$DateNew=10"
set "$DateNew=0%$DateNew%"
set "FILE=log %date:~6,4%-%date:~3,2%-%$dateNew:~-2%"

copy C:\log.txt C:\"%FILE%.txt"
Sign up to request clarification or add additional context in comments.

2 Comments

set /a $DateNew=%$DateNew:0=%-1 If today is 10, the final value is 0
Oops ! Corrected Thanks @MC ND
0

subtract one and keep a leading zero:

set a=08
set /a a=11%a%-1001
set a=%a:~1%
echo %a%

of course, 01- 1 = 00...

Comments

0

All of the cmd shell script (batch) string-number parsing pain can be avoided in PowerShell:

copy-item C:\log.txt ("C:\{0:yyyy-MM-dd}.txt" -f (get-date).AddDays(-1))

This also avoids the "last day of the month" problem. For example, the code

"{0:yyyy-MM-dd}" -f (get-date "8/1/2014").AddDays(-1)

outputs 2014-07-31.

Comments

0

This is a robust way to get yesterday's date with a batch file:

:: get yesterdays date
@echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "data=%yyyy%-%mm%-%dd%"

echo Yesterday was "%data%"
pause

8 Comments

Clever idea (create temporary VBScript script file). I still think PowerShell is easier...
@Bill_Stewart I like the idea of Powershell Bill, but it's crippled because every user has to jump through hoops to be able to run a powershell script. if Microsoft had generated a prompt on running the first PS script to ask if they want to allow the script once, the script all the time, or allow all scripts in the future - then it would have been more useful. As it stands the basic users don't have the skills to enable Powershell scripts.
It's not really too difficult. Open an elevated command window and enter the command Set-ExecutionPolicy RemoteSigned. If the local user doesn't have administrator privileges, he/she can enter Set-ExecutionPolicy RemoteSigned CurrentUser. The execution policy can also be set in a GPO.
@Bill_Stewart It's not hard if you know how to do it. :)
Same as your VBScript workaround - easy if you know how. :) I would argue that PowerShell is a more valuable skill to learn from this point forward.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.