Batch to add leading zero to date

August 5, 2009 at 08:58:47
Specs: Windows XP
OK I am no programmer by any means but I get along OK most of the time. But I have an issue that is killing me. We receive a delimited text file that we need to update. One of the things I need to update on the file is a date. It is always proceeded by {20134{. The date in the text file is formatted m/yy I need to add the leading zero to it. Example:


Needs to be:

This has been driving me nuts. Currently I am manually doing a find and replace for each month 1 -9 Is there any way to find these and change them all at once with a batch script? Can anyone help me?

See More: Batch to add leading zero to date

Report •

August 5, 2009 at 15:54:00
Hi raremind

A few questions

1, How is the file delimited by comma's ect.
2, Is there any thing else on the date line.
3, Is there only one "/" in the file(s)
4, Is there only "{ " "}" on the date line

Report •

August 5, 2009 at 16:34:52
Thanks for your reply!

The file is delimited with {
The date line is formated as such 000000{000000{M/YY
Yes there is only one /
No every line uses the { delimiter

Thanks again your you help

Report •

August 7, 2009 at 13:15:02
Hi Micheal

Try this on some dummy files first to check that it works ok.
I including a check for a leading zero so files don't get modified twice,

@echo off
SetLocal EnableDelayedExpansion

dir /b Data*.txt>Files.txt

type nul>Tmp.txt
for /f "tokens=*" %%a in (Files.txt) do (
for /f "tokens=*" %%b in ('type "%%a"') do (
for /f "tokens=1-2 delims=/" %%c in ("%%b") do (
if "%%d"=="" (
echo %%b>>Tmp.txt
) else (
for /f "tokens=1-3 delims={" %%e in ("%%c") do (
set Day=%%g
set Zero=!Day:~,1!
if !Zero! NEQ 0 (
if %%g LSS 10 (set Day=0%%g)
echo %%e{%%f{!Day!/%%d >> Tmp.txt
) else (echo %%b>>Tmp.txt)
del "%%a"
ren Tmp.txt "%%a"

del Files.txt

Report •

Related Solutions

Ask Question