NT Batch Files and search and replace

NT Batch Files and search and replace

Author
Discussion

Graham

Original Poster:

16,368 posts

284 months

Wednesday 18th January 2006
quotequote all
I have a Directory on an NT box that recieves files in a specific format.

there can be any number of files in this directory from non to many.

What i need to do via a scheduler is run a batch program that will

a) read in each file 1 at a time
b)find and replace a fixed piece of text ( may occur more than once)
c) save the file out in a different location
d) delete the original file
e)repeat for all files in the source directory...

I can do it under unix but havent a clue where to start on NT

Cheers

Graham

aldi

9,243 posts

237 months

Wednesday 18th January 2006
quotequote all
graham.cmd:
for %1 in (*.txt) do call doreplace.cmd %1

doreplace.cmd:
xchangecl %1 "arse" "bum"
move %1 c:destination

this seems like what you need.

I've done it in two files one calling the other because if you do all the replacements in one line, then move *.txt in the next line then someone could save a new .txt inbetween which doesn't get replaced but does get moved. That shouldnt happen doing it this way.

Hope this does the trick!

Edit: It's eaten my backslashes but I'm sure you get the gist...

>> Edited by aldi on Wednesday 18th January 17:49

zaktoo

1,401 posts

240 months

Wednesday 18th January 2006
quotequote all
Share the directory and use a Unix bow to mount it with Samba. Then perl and/or sed plus cron is your ticket ;-)

HTH

Ciao

Zak

Graham

Original Poster:

16,368 posts

284 months

Thursday 19th January 2006
quotequote all
aldi said:
graham.cmd:
for %1 in (*.txt) do call doreplace.cmd %1

doreplace.cmd:
xchangecl %1 "arse" "bum"
move %1 c:destination

this seems like what you need.

I've done it in two files one calling the other because if you do all the replacements in one line, then move *.txt in the next line then someone could save a new .txt inbetween which doesn't get replaced but does get moved. That shouldnt happen doing it this way.

Hope this does the trick!

Edit: It's eaten my backslashes but I'm sure you get the gist...

>> Edited by aldi on Wednesday 18th January 17:49



Cool I'll Give that a whirl tonight...

Einion Yrth

19,575 posts

244 months

Thursday 19th January 2006
quotequote all
zaktoo said:
Share the directory and use a Unix bow to mount it with Samba. Then perl and/or sed plus cron is your ticket ;-)

HTH

Ciao

Zak

Or just install Perl on the winblows box.

Graham

Original Poster:

16,368 posts

284 months

Monday 23rd January 2006
quotequote all
aldi said:
graham.cmd:
for %1 in (*.txt) do call doreplace.cmd %1

doreplace.cmd:
xchangecl %1 "arse" "bum"
move %1 c:destination



I cant get the first program graham.cmd to do anything. it just errors returning the instruction line with banks where the %1 is. is the (*.txt) correct, how does that pick up all the file names ?

cheers

graham

aldi

9,243 posts

237 months

Monday 23rd January 2006
quotequote all
Graham said:
aldi said:
graham.cmd:
for %1 in (*.txt) do call doreplace.cmd %1

doreplace.cmd:
xchangecl %1 "arse" "bum"
move %1 c:destination



I cant get the first program graham.cmd to do anything. it just errors returning the instruction line with banks where the %1 is. is the (*.txt) correct, how does that pick up all the file names ?

cheers

graham


It just does, it calls everything after 'do' once for every flile that matches the pattern in the brackets with %1 as the filename. If you type 'help for' at a command prompt it should give you all the detailed goods. I'm assuming the files in question are just *.txt files?

Edit: This will be the problem, from the help text:
"To use the FOR command in a batch program, specify %%variable instead of %variable. Variable names are case sensitive, so %i is different from %I."

Sorry for the confusion there.

>> Edited by aldi on Monday 23 January 18:09

Graham

Original Poster:

16,368 posts

284 months

Monday 23rd January 2006
quotequote all
Ahh thats better

cheers

G

greenv8s

30,192 posts

284 months

Monday 23rd January 2006
quotequote all
In the FOR command you probably want to name the loop variable something other than '1'. For example

for %f in ( *.txt ) do call doreplace.cmd %f

etc

Graham

Original Poster:

16,368 posts

284 months

Wednesday 25th January 2006
quotequote all
Blimey this Nt stuff is like pulling teeth..

I've got xchangecl working quite nicley on the multiple search and replace.

a bit of a mare as I ended up having to do most of it at ascii code level as its full of extended characters...


The Problem I have now is a file name one.

The files need to end up in the format JOBCRxxxxxxx^xxxxx.xml which is what i
have them at at the moment.

the trouble is the for %%1 in (*.xml) do call fred.cmd %%1 looses the ^ from the file name
giving me JOBCRxxxxxxxxxxxx.xml so then the xchangecl fails as there isnt a file with that name ARGHHHH...

changing the ^ to say a £ and it all works fine. trouble is i need to end up with the ^ in the file name.

is there a way from a command line batch file i can search and replace a character i.e. I can get the the source file to JOBCRxxxxxxx£xxxxx.xml

run it through the changes and save out, but then I'd still need change the filename back to

JOBCRxxxxxxx^xxxxx.xml

any Ideas cheers Guys..