# Solved: Numbers to text in Batch file



## cobb (Aug 19, 2006)

I am doing a daily backup of a particular set of folders into one folder called BackUp. As part of the auto start of the backup, I am setting the file BackUp to Backup(todays date - 1) which could relate to Backup20110416. All was working well, but now for some reason any days of the week from 1 to 9 end up as Backup2011058 instead of Backup20110508 (note the zero missing/included in the day). I do want the zero included, and I have tried to manipulate the date within the batch file using:

set tt= %date%
set var1=1
call set substring=%%tt:~8,2%%
set /A newvar=%substring%-%var1%

setlocal
set "myString=%newvar%"
call :strlen result mystring
goto :fini

:strlen <resultVar> <mystringVar>
( 
setlocal EnableDelayedExpansion
set "s=!%~2!#"
set "len=0"
for %%P in (2 1) do (
if "!s:~%%P,1!" NEQ "" ( 
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
( 
endlocal
set "%~1=%len%"
exit /b
)
:fini
if %result% == !1! set "%newvar% = 0%newvar%"
echo %newvar%
pause

But alas this does not work either and I think it is because I manipulate the newvar (forth line) and it is now a number. I can not seem to get it back to characters rather than number. Any suggestions would be greatly appreciated.
Thanks
Jerry


----------



## Squashman (Apr 4, 2003)

Can you show me the output of 

```
echo %date%
```


----------



## Squashman (Apr 4, 2003)

You may just want to change your batch file completely and have it call a VBscript. Your batch file will not subtract back to the previous month if you are running it on the first day of the month.

The following script will get the previous date with the correct month if it is the first of the month.

```
@Echo Off
REM Subtract x days from current date.

Echo wsh.echo "olddate="^&date-5 > %temp%\tmp.vbs
For /f "delims=" %%a in ('cscript.exe //nologo %temp%\tmp.vbs') do set %%a

REM Informational only
Type %temp%\tmp.vbs

REM Delete temp file
Del  %temp%\tmp.vbs /Q

Echo %olddate%
```
The only glitch with this just like your batch file is it removes the leading zeros. But those can easily be added back in by parsing the date back out and use a substring replacement to add the leading zero back in. You just take the day of the week variable and a leading zero in front of it. Then use another set statement to pull the last two characters from the string.


----------



## cobb (Aug 19, 2006)

This is the output of echo %date%

C:\>echo %date%
Thu 05/05/2011

However, that being said, I have a work around that solves the problem. Instead of changing the file name at the begining of the batch file (which changes the date the next day), I will change the date at the end of the batch file (which would be done on the same day as the backup, and thus eliminate the need for changing the date at all.

Thanks very much for your tip. You folks really do a great job.
Best to you
Jerry


----------



## Squashman (Apr 4, 2003)

cobb said:


> This is the output of echo %date%
> 
> C:\>echo %date%
> Thu 05/05/2011


Then your code is incorrect.

```
call set substring=%%tt:~8,2%%
```
Remember that batch counts from 0. It doesn't start at 1. So the eighth & ninth positions of the date variable is "5/".


----------



## cobb (Aug 19, 2006)

That was what I thought too, but found out the hard way that this:

set tt= %date%
set var1=1
call set substring=%%tt:~8,2%%
echo %substring%
pause


Will yield this:

C:\>set tt= Thu 05/05/2011
C:\>set var1=1
C:\>call set substring=%tt:~8,2%
C:\>echo 05
05
C:\>pause
Press any key to continue . . .

But good point because I was taught what you say is the way it works.


----------

