Does it have to be a Batch file? This is something that would be relatively simple using a VB script.......if VBS is OK, let me know some more details and I will post something.
Greg
Main Topics
Browse All TopicsCould anyone help me make a Btch file that will find and replace text IN a text file?
thx you,
support@ticketstogo.com
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
This is not possible. The batch processor and command line tools of Windows are too limited. There is no way to perform a saerch&replace.
To accomplish this you either need an additional (command line) utility for search&replace that you can call from within your batch OR you have to forget about .bat files and use the Windows Script Host (scripts written in VBScript or JScript). These are .vbs or .js script files that can be launched exactly like the .bat files in the latest versions of windows without additional software and supporting much wider functionalities than batches.
here is the script. You can run it with
C:> cscript <scriptname>
or just typyng <scriptname> but in this case the messages may displayed in windows message boxes and you might prefer to remove them (since they wait for the user too click on "OK").
---------
var srcFolder = "C:\\Web";
var dstFolder = "C:\\CWeb";
var fso =new ActiveXObject("Scripting.F
if(!fso.FolderExists(srcFo
WScript.Echo("Source folder (" + srcFolder + ") doesn't exist!");
WScript.Quit(1);
}
if(!fso.FolderExists(dstFo
WScript.Echo("Destination folder (" + dstFolder + ") doesn't exist!");
WScript.Quit(1);
}
var fc = new Enumerator(fso.GetFolder(s
while(!fc.atEnd()) { //for each file in srcFolder
var fname = fc.item().name;
if(fname.match(/\.txt$/)) { //only consider .txt files
WScript.Echo("Processing " + fname + "...");
var srcfile = fc.item().OpenAsTextStream
var srctext = srcfile.AtEndOfStream ? "" : srctext=srcfile.ReadAll();
srcfile.close();
var dstfile = fso.CreateTextFile(fso.Bui
dstfile.write(srctext.repl
dstfile.close();
}
fc.moveNext();
}
Cscript is an external command ususally located in C:\Windows\System32 (if you have win2k or xp).
For the *ticket thing just replace the line "var dstfile=..." with:
var dstfname= fname == "My Tickets.txt" ? "TTTickets.txt" : fname;
var dstfile = fso.CreateTextFile(fso.Bui
BTW, note that this script doesn't modify the files in C:\Web. It only reads tesxt files from C:\Web, do the search&replace in memory and save the results in C:\CWeb\something.txt. Hope this is what you wanted.
Mmh, I dont understand. What do you mean with "It is removing the space" ?. My script doesn;t change the name of the file "My Tickets.txt" in C:\Web. It just creates a file named "TTTickets.txt" in C:\CWeb with the result of the search&replace of My Tickets.txt.
Anyway if you want the file C:\Web\My Tickets.txt renamed, change the script like this
-----------
var srcFolder = "C:\\Web";
var dstFolder = "C:\\CWeb";
var fso =new ActiveXObject("Scripting.F
if(!fso.FolderExists(srcFo
WScript.Echo("Source folder (" + srcFolder + ") doesn't exist!");
WScript.Quit(1);
}
if(!fso.FolderExists(dstFo
WScript.Echo("Destination folder (" + dstFolder + ") doesn't exist!");
WScript.Quit(1);
}
var fc = new Enumerator(fso.GetFolder(s
while(!fc.atEnd()) { //for each file in srcFolder
var fname = fc.item().name;
if(fname.match(/\.txt$/)) { //only consider .txt files
WScript.Echo("Processing " + fname + "...");
if(fname == "My Tickets.txt") fc.item().move(fname="TTTi
var srcfile = fc.item().OpenAsTextStream
var srctext = srcfile.AtEndOfStream ? "" : srctext=srcfile.ReadAll();
srcfile.close();
var dstfile = fso.CreateTextFile(fso.Bui
dstfile.write(srctext.repl
dstfile.close();
}
fc.moveNext();
}
------
Note that this script just renames the file "My Tickets.txt" in c:\web but doesn't change its content. Only the file TTTickets in C:\CWeb will be changed. If this is not what you wanted, please try to be more precise in your request.
PERFECT, I'm very sorry I wasn't more specific. My first time using Expert Exchange. I only hope that I can sit down and write a javascript like that , so quickly oneday. I love javascript,I always have. any tips on how to learn it more quickly and completely?
Also, one last question about the script! Can it load a program called c:\cweb\openfeed.exe??? which uploads the text file?
Thank you so much!
Caiapfas
support@ticketstogo.com
Yes, you can find full Windows Script Host documentation and an introduction to jscript at www.microsoft.com/scriptin
For the execution of openfeed, follow these links (again from the same page mentioned above): Windows Script Host Documentation -> Reference ->Methods -> Run Method. It also has two examples at the end. The equivalent Jscript code (for example 1) is:
var WshShell = WScript.CreateObject("WScr
WshShell.Run "%windir%\notepad " + WScript.ScriptFullName;
ok, i don't understand . I when to the above site , but only found VBscript?
how can i make this
var WshShell = WScript.CreateObject("WScr
WshShell.Run "%windir%\notepad " + WScript.ScriptFullName;
run c:\cweb\opfeed.exe?
var WshShell = WScript.CreateObject("WScr
WshShell.Run "C:\\cweb\openfeed.exe " + WScript.ScriptFullName;
and if that is it , when do i add it to the script?
Almost:
var WshShell = WScript.CreateObject("WScr
WshShell.Run "C:\\cweb\\openfeed.exe";
Regarding the position where to insert this piece in the orginal script, it depends on when you want it executed. If you want it executed at the end,after the search&replace thing, just add it at the very end of the file.
These are the links that I see at www.microsoft.com/scriptin
Script Debugger Documentation
Remote Scripting Documentation
JScript Documentation
VBScript Documentation
Windows Script Host Documentation
Windows Script Components Documentation
Microsoft Windows Script 5.6 Documentation for Download
Microsoft Windows Script 5.6 Download
The third is javascript.
I know its due to my stupidity, but i'm getting a error. problay due to where I'm putting the run command.
var srcFolder = "Z:\\web";
var dstFolder = "C:\\cweb";
var fso =new ActiveXObject("Scripting.F
if(!fso.FolderExists(srcFo
WScript.Echo("Source folder (" + srcFolder + ") doesn't exist!");
WScript.Quit(1);
}
if(!fso.FolderExists(dstFo
WScript.Echo("Destination folder (" + dstFolder + ") doesn't exist!");
WScript.Quit(1);
}
var fc = new Enumerator(fso.GetFolder(s
while(!fc.atEnd()) { //for each file in srcFolder
var fname = fc.item().name;
if(fname.match(/\.txt$/)) { //only consider .txt files
WScript.Echo("Processing " + fname + "...");
if(fname == "MyTickets.txt") fc.item().move(fname="TTTi
var srcfile = fc.item().OpenAsTextStream
var srctext = srcfile.AtEndOfStream ? "" : srctext=srcfile.ReadAll();
srcfile.close();
var dstfile = fso.CreateTextFile(fso.Bui
dstfile.write(srctext.repl
dstfile.close();
}
fc.moveNext();
var WshShell = WScript.CreateObject("WScr
WshShell.Run "C:\\cweb\\openfeed.exe";
I'm very sorry, I know i'm driving you nuts
But I'm still getting the error?
var srcFolder = "Z:\\web";
var dstFolder = "C:\\cweb";
var fso =new ActiveXObject("Scripting.F
if(!fso.FolderExists(srcFo
WScript.Echo("Source folder (" + srcFolder + ") doesn't exist!");
WScript.Quit(1);
}
if(!fso.FolderExists(dstFo
WScript.Echo("Destination folder (" + dstFolder + ") doesn't exist!");
WScript.Quit(1);
}
var fc = new Enumerator(fso.GetFolder(s
while(!fc.atEnd()) { //for each file in srcFolder
var fname = fc.item().name;
if(fname.match(/\.txt$/)) { //only consider .txt files
WScript.Echo("Processing " + fname + "...");
if(fname == "MyTickets.txt") fc.item().move(fname="TTTi
var srcfile = fc.item().OpenAsTextStream
var srctext = srcfile.AtEndOfStream ? "" : srctext=srcfile.ReadAll();
srcfile.close();
var dstfile = fso.CreateTextFile(fso.Bui
dstfile.write(srctext.repl
dstfile.close();
}
fc.moveNext();
}
var WshShell = WScript.CreateObject("WScr
WshShell.Run "C:\\cweb\\openfeed.exe";
openfeed is a .exe
this is cut n pasted from explorer - openfeed.exe
I even tried to make the script use a batch file to open the .exe, same error as above?
var srcFolder = "Z:\\";
var dstFolder = "C:\\Program Files\\Openfield Technologies\\OpenFeed\\";
var fso =new ActiveXObject("Scripting.F
if(!fso.FolderExists(srcFo
WScript.Echo("Source folder (" + srcFolder + ") doesn't exist!");
WScript.Quit(1);
}
if(!fso.FolderExists(dstFo
WScript.Echo("Destination folder (" + dstFolder + ") doesn't exist!");
WScript.Quit(1);
}
var fc = new Enumerator(fso.GetFolder(s
while(!fc.atEnd()) { //for each file in srcFolder
var fname = fc.item().name;
if(fname.match(/\.txt$/)) { //only consider .txt files
WScript.Echo("Processing " + fname + "...");
if(fname == "MyTickets.txt") fc.item().move(fname="TTTi
var srcfile = fc.item().OpenAsTextStream
var srctext = srcfile.AtEndOfStream ? "" : srctext=srcfile.ReadAll();
srcfile.close();
var dstfile = fso.CreateTextFile(fso.Bui
dstfile.write(srctext.repl
dstfile.close();
}
fc.moveNext();
}
var WshShell = WScript.CreateObject("WScr
WshShell.Run("C:\\Program Files\\Openfield Technologies\\OpenFeed\\op
Try replacing the last line with:
WshShell.Run("C:\\Windows\
If at the end of the script you see notepad starting without errors then the script is fine and the issue is with your openfeed application.
In any case:
- you probably should open a new request since this is not related to search&replace anymore
- maybe openfeed is not supposed to be used in batch processing but only interactively
- if you still have issues, report exactly the error you are receiving and which program is giving the error. I believe that you are not getting the error from the script but from openfeed. Also try to start openfeed manually from the command prompt and see what happens.
I've been tied up with several projects, so I haven't had a chance to give any input. It looks like lbertacco has got you pretty well covered, but if for future reference here's a Perl solution. This assumes that the script is being run in the same directory as the source files; if it's not, then we'll need to make a minor change.
#!perl -w
{local $^I = '.orig';
@ARGV = glob("*.txt");
while (<>) { s/\bwas\b/is/ig; print; } }
@orig = glob("*.orig");
unlink @orig;
system "move Tickets.txt c:/cweb/TTTickets.txt" if (-e 'Tickets.txt');
I love the power of Perl!
Business Accounts
Answer for Membership
by: FishMongerPosted on 2004-03-09 at 17:12:25ID: 10556665
The answer is Yes, BUT we need more info before we can help you.