Computing.Net > Forums > Programming > Batch to add leading zero to date

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch to add leading zero to date

Reply to Message Icon

Name: raremind
Date: August 5, 2009 at 08:58:47 Pacific
OS: Windows XP
Subcategory: Batch
Tags: Leading Zero, Date Format
Comment:

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:

111{20134{7/09

Needs to be:
111{20134{07/09

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?



Sponsored Link
Ads by Google

Response Number 1
Name: dtech10
Date: August 5, 2009 at 15:54:00 Pacific
Reply:

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


0

Response Number 2
Name: raremind
Date: August 5, 2009 at 16:34:52 Pacific
Reply:

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


0

Response Number 3
Name: dtech10
Date: August 7, 2009 at 13:15:02 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch to add leading zero to date

Batch to add the column in a txt file www.computing.net/answers/programming/batch-to-add-the-column-in-a-txt-file/19625.html

Remove leading zeros from a string www.computing.net/answers/programming/remove-leading-zeros-from-a-string/19553.html

Date Routines in Batch Files www.computing.net/answers/programming/date-routines-in-batch-files/15590.html