NT Batch Files and search and replace
Discussion
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
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.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
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
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...
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 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
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..
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..
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff