NT Batch Files and search and replace

NT Batch Files and search and replace

Author
Discussion

Graham

Original Poster:

16,368 posts

285 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

Graham

Original Poster:

16,368 posts

285 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...

Graham

Original Poster:

16,368 posts

285 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

Graham

Original Poster:

16,368 posts

285 months

Monday 23rd January 2006
quotequote all
Ahh thats better

cheers

G

Graham

Original Poster:

16,368 posts

285 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..