Computing.Net > Forums > Programming > Batch Program

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 Program

Reply to Message Icon

Name: Advent85
Date: April 9, 2009 at 03:02:50 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Please forgive any errors as this is my first post.

I have a text file that is used to import job details.
each record in the file contains a line in the following format (the * can be any number or letter):
******HSG*********20**************

I need a batch program that will find each instance of this line and replace the characters after the "20"
so I end up with the following:

******HSG*********20SCDC1234

The characters where I have used the "*" must remain the same.

Can anyone help me as its driving me mad



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: April 9, 2009 at 03:37:50 Pacific
Reply:

:: chg lines w HSG and 20

@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%a in (myfile) do (
set str=%%a
echo !str! | find "HSG" | find "20" > nul
if !errorlevel! equ 0 (set str=!str:20=20SCDC1234!)
echo !str! >> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: Advent85
Date: April 9, 2009 at 04:11:15 Pacific
Reply:

Hi M2
Thank you so much for your prompt reply, working through you file I think I almost have it, but one problem is that "20" could appear in any of the spaces I have marked with a "*".

If it helps "HSG" will always be the 7,8,9 character in the line and the "20" I am looking for will always be at positions 19 & 20

Sorry I should have made this clearer from the start


0

Response Number 3
Name: lee123abc
Date: April 9, 2009 at 04:48:53 Pacific
Reply:

That script is good M2


0

Response Number 4
Name: Mechanix2Go
Date: April 9, 2009 at 04:51:15 Pacific
Reply:

:: chg lines w HSG and 20

@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%a in (myfile) do (
set str=%%a
::echo !str:~6^,2!
if !str:~6^,3! equ HSG if !str:~18^,2! equ 20 (
set str=!str:20=20SCDC1234!
)
echo !str! >> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 5
Name: Advent85
Date: April 9, 2009 at 05:36:30 Pacific
Reply:

Hi M2, you are so near on this I can taste it.

I have run the batch and what is happening is the 20SCDC1234 is being put in place of the existing 20 so I am getting:

******HSG*********20SCDC1234**************

What I need is for the SCDC1234 to replace whatever is after the 20 to get

******HSG*********20SCDC1234

Also I had a line where there was 20 between the HSG and 20 and it inserted the 20SCDC1234 twice.

i.e.
000017HSG03232200020RR010001118999

became

000017HSG0323220SCDC12340020SCDC1234RR010001118999

When I need it to be

000017HSG03232200020SCDC1234

I tried to amend your script slightly to achieve this but you really don't want to know what result I got.

Thanks for your help it really is apreciated.



0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: April 9, 2009 at 05:46:47 Pacific
Reply:

Post a few lines of your actual file so we can get this right.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 7
Name: Advent85
Date: April 9, 2009 at 06:15:22 Pacific
Reply:

000244HSG0323552000104442
000245HSG032355200020280500480
000246HSG03235520003999 BLAKELAND HILL
000247HSG03235520004DUXFORD
000248HSG03235520005CAMBS
000249HSG03235520006CB25 8RB
000250HSG03235520007Mr J BLOGGS
000251HSG0323552000801223 555666
000252HSG03235520011B028
000253HSG03235520014FIT BANNISTER AND GRAB RAILS
000254HSG03235520015N
000255HSG03235520016JOBR
000256HSG03235520017Y
000257HSG03235520019 215.26
000258HSG03235520020RK050007060999
000259HSG03235520021BOB BUILDER
000260HSG0323552002401223 555666
000261HSG03235520025YYYYYNNYYYYYNNNNNNNNN
000262HSG03235520026MARGARET THATCHER
000263HSG0323552002804/04/2007
000264HSG0323552002902/05/2007 00:00:00
000265HSG03235520031YC116 2.00
000266HSG03235520047AS SHEET IN BAG 03.04.07
000267HSG03235520031YC158 2.00
000268HSG03235520031YC112 1.00
000269HSG03235520047BATHROOM
000270HSG03235520031YC110 1.00
000271HSG03235520047BATHROOM
000272HSG03235520070
000273HSG03235600000HSG
000274HSG0323560000104162
000275HSG032356000020801400530
000276HSG03235600003999 CHASTON ROAD
000277HSG03235600004GREAT SHELFORD
000278HSG03235600005CAMBS
000279HSG03235600006CB28 9AP
000280HSG03235600007Mr D BECKAM
000281HSG03235600008
000282HSG03235600011B080
000283HSG03235600014Supply & fit straight grab rail up to 500mm in
000284HSG03235600014length.
000285HSG03235600015N
000286HSG03235600016JOBR
000287HSG03235600017Y
000288HSG03235600019 93.11
000289HSG03235600020RK050007060999
000290HSG03235600021BOB BUILDER
000291HSG03235600025YYYYYNNYYYYYNNNNNNNNN
000292HSG03235600026POSH SPICE
000293HSG0323560002804/04/2007
000294HSG0323560002902/05/2007 00:00:00
000295HSG03235600031YC110 1.00
000296HSG03235600047ALL WORK AS SHEETS IN BAG 03.04.07
000297HSG03235600031YC110 1.00
000298HSG03235600047OPP W/C
000299HSG03235600031YC112 1.00
000300HSG03235600070

Sorry it took so long I had to change names /addresses .
The above consists of two job records.


0

Response Number 8
Name: Mechanix2Go
Date: April 9, 2009 at 09:01:08 Pacific
Reply:

Call me a stickler for detail but it looks like EVERY line has HSG in positions 7-9. Which makes it pointless to test for it.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 9
Name: ghostdog
Date: April 9, 2009 at 19:21:13 Pacific
Reply:

if you can download stuffs, here's a good tool for string manipulation, gawk for windows(see my sig)

C:\test>gawk "substr($0,19,2)==20{print substr($0,0,20) \"SCDC1234\";next}1" file.txt
000244HSG0323552000104442
000245HSG032355200020280500480
000246HSG03235520003999 BLAKELAND HILL
000247HSG03235520004DUXFORD
000248HSG03235520005CAMBS
000249HSG03235520006CB25 8RB
000250HSG03235520007Mr J BLOGGS
000251HSG0323552000801223 555666
000252HSG03235520011B028
000253HSG03235520014FIT BANNISTER AND GRAB RAILS
000254HSG03235520015N
000255HSG03235520016JOBR
000256HSG03235520017Y
000257HSG03235520019 215.26
000258HSG03235520020SCDC1234
000259HSG03235520021BOB BUILDER
000260HSG0323552002401223 555666
000261HSG03235520025YYYYYNNYYYYYNNNNNNNNN
000262HSG03235520026MARGARET THATCHER
000263HSG0323552002804/04/2007
000264HSG0323552002902/05/2007 00:00:00
000265HSG03235520031YC116 2.00
000266HSG03235520047AS SHEET IN BAG 03.04.07
000267HSG03235520031YC158 2.00
000268HSG03235520031YC112 1.00
000269HSG03235520047BATHROOM
000270HSG03235520031YC110 1.00
000271HSG03235520047BATHROOM
000272HSG03235520070
000273HSG03235600000HSG
000274HSG0323560000104162
000275HSG032356000020801400530
000276HSG03235600003999 CHASTON ROAD
000277HSG03235600004GREAT SHELFORD
000278HSG03235600005CAMBS
000279HSG03235600006CB28 9AP
000280HSG03235600007Mr D BECKAM
000281HSG03235600008
000282HSG03235600011B080
000283HSG03235600014Supply & fit straight grab rail up to 500mm in
000284HSG03235600014length.
000285HSG03235600015N
000286HSG03235600016JOBR
000287HSG03235600017Y
000288HSG03235600019 93.11
000289HSG03235600020SCDC1234
000290HSG03235600021BOB BUILDER
000291HSG03235600025YYYYYNNYYYYYNNNNNNNNN
000292HSG03235600026POSH SPICE
000293HSG0323560002804/04/2007
000294HSG0323560002902/05/2007 00:00:00
000295HSG03235600031YC110 1.00
000296HSG03235600047ALL WORK AS SHEETS IN BAG 03.04.07
000297HSG03235600031YC110 1.00
000298HSG03235600047OPP W/C
000299HSG03235600031YC112 1.00
000300HSG03235600070

Unix Win32 tools | Gawk for Windows


0

Response Number 10
Name: Advent85
Date: April 13, 2009 at 23:23:35 Pacific
Reply:

Hi M2,
Sorry for the delay in replying I have been away over the weekend, (hope you had a good easter).
yes, you are right every line does have the HSG on it, I suppose I was just concentrating on the constant parts of the line, when really the "20" at characters 19 & 20 is the one I should concentrate on.
Ghostdog - Thanks for the suggestion of Gawk which seems a simple solution, unfortunately I don't think I would be able to install third party software on the server I am hoping to run the batch on


0

Response Number 11
Name: BatchFreak
Date: April 14, 2009 at 01:00:59 Pacific
Reply:

heh, i hope this isnt a windows server -_-;

but anyway: just open up your text editor and replace.

replace: 20**************
with: 20SCDC1234


i dont know how notepad handles wildcards, but in sure you coul use a different editor if need be.

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 12
Name: Advent85
Date: April 14, 2009 at 01:09:27 Pacific
Reply:

BatchFreak, I had already tried this but there could be more than one "20" on a line so I really need to be able to specify that the 20 I am replacing is at position 19 & 20.


0

Response Number 13
Name: Mechanix2Go
Date: April 14, 2009 at 04:19:20 Pacific
Reply:

So for lines with 20 at pos 19-20 you need to replace 'the rest of the line' with a given string, is that it?


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 14
Name: Advent85
Date: April 14, 2009 at 04:53:57 Pacific
Reply:

Hi M2,
Yes, that's correct, sorry I wasn't clearer at the begining


0

Response Number 15
Name: Mechanix2Go
Date: April 14, 2009 at 06:15:07 Pacific
Reply:

I'm headed out the door but I think we're getting traction now.

===============================
:: 14 April 2009 19:48
:: chg lines w 20 at pos 19-20

@echo off > newfile & setLocal EnableDelayedExpansion

set /a chgs=0

for /f "tokens=* delims=" %%a in (myfile) do (
set str=%%a
set head=!str:~0^,18!
if !str:~18^,2! equ 20 (
set new=!head!20SCDC1234
echo !new! >> newfile
set /a chgs+=1
) else (
echo !str! >> newfile
)
)
echo !chgs! lines changed


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 16
Name: Advent85
Date: April 14, 2009 at 06:33:04 Pacific
Reply:

M2,
I owe you a drink or three, it works perfectly, I have put it in the test system to run this week and will migrate to live next week.
I cant tell you how much this has helped me.
Many, many thanks


0

Response Number 17
Name: Advent85
Date: April 21, 2009 at 06:03:32 Pacific
Reply:

Just a follow up note to say that this has worked fine all credit to Machanix2Go


0

Response Number 18
Name: Mechanix2Go
Date: April 22, 2009 at 06:29:45 Pacific
Reply:

Thanks for letting us know.


=====================================
If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






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 Program

Advanced batch program www.computing.net/answers/programming/advanced-batch-program/14439.html

Batch Programming www.computing.net/answers/programming/batch-programming/3213.html

Batch Programming www.computing.net/answers/programming/batch-programming/15718.html