views:

240

answers:

2

I am working within a batch file and need to pad a single digit with a leading 0 if under 10. I have the values in environmental variables. They are month and day, I need to pad to match file structure I am working against. I am using vbscript to return a date that comes back in the following format "7/16/2009". Need it to look like "07/16/2009" and most inportantly need each item in separate EVs.

VBscript:

WScript.Echo DateAdd("d", Date, -36)

Batch:

for /F "tokens=1-3 delims=/" %%x in ('cscript //nologo get36thday.vbs') do (
   SET YYYY=%%z
   SET MM=%%x
   SET DD=%%y)
+2  A: 

VBScript:

dteOldDate = Now()
strNewDate = Right("00" & Month(dteOldDate), 2) & "/" & Right("00" & Day(dteOldDate), 2) & "/" & Year(dteOldDate)
EBGreen
You also need to echo the date: `WScript.Echo strNewDate`, because the batch file reads it from the script's output.
Helen
+3  A: 

For the batch script, I don't know the exact syntax but a batch script can return a specified number of characters from the right side of a string.

So, append the month after a "0" character and take the 2 right-most digits. It would probably look something similar to this:

SET MM=0%%x
SET MM=%MM:~-2%

1 become 01
5 becomes 05
10 stays 10

Robert Cartaino
+1. Nice trick. I would have used a subroutine and an `if` statement. But this is much more elegant.
Joey
Awesome, Very clever.