# Solved: Batch File/Script



## computerman29642 (Dec 4, 2007)

Does anyone know if it is possible to create a batch file or script that can automate a replace in a text file?


----------



## DoubleHelix (Dec 10, 2004)

Other than the built-in Find/Replace features in text editors? Maybe you should explain what you're looking for in a bit more detail.


----------



## computerman29642 (Dec 4, 2007)

I am trying to automate opening a text file (notepad), and performing a find and replace.


----------



## DoubleHelix (Dec 10, 2004)

So you want to find and replace the same word/phrase in multiple text files? I'd suggest looking at AutoIt


----------



## Squashman (Apr 4, 2003)

I would batch or shell script this versus trying to do it with a GUI. In the Unix world you would use SED to do this. You can get a version of SED for the Windows cmd line. You could also script it using SFK. Is has a filter option to do find and replace.

http://www.stahlforce.com/dev/index.php?tool=filter


----------



## Squashman (Apr 4, 2003)

This might work for you as well.
http://www.handyfiletool.com/index.html


----------



## ghostdog74 (Dec 7, 2005)

as usual, there's no need to download anything. You can do it with batch, or vbscript

```
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "C:\test\test.txt"
Set objFile = objFS.OpenTextFile(strFile,1)
Do Until objFile.AtEndOfStream
	strLine = objFile.ReadLine
	strLine = Replace(strLine,"pattern","changed")
	WScript.Echo strLine
Loop
```
save the above as script.vbs and in command line

```
c:\test> cscript /nologo script.vbs
```


----------



## Squashman (Apr 4, 2003)

Sure would be great to know how to do it natively with a batch file. That seems to be alot of code for a Vbscript that can be done with one line using SED or SFK.

Just from experience I have had at work (And I do data processing for a living) if you are working with very large files a Vbscript seems to run considerably slower then a batch file or a shell script using SFK or SED. I don't know why it is just what we have tested at work.


----------



## ghostdog74 (Dec 7, 2005)

Squashman said:


> Sure would be great to know how to do it natively with a batch file.
> 
> 
> > The standard way applies using batch (and batch commands). The venerable for loop, coupled with delims and tokens.
> ...


Its verbose and understandable, best for people maintaining code. I would rather read vbscript code than SED with complex reg expressions.



> Just from experience I have had at work (And I do data processing for a living) if you are working with very large files a Vbscript seems to run considerably slower then a batch file or a shell script using SFK or SED. I don't know why it is just what we have tested at work.


that's because the internal logic of SED is already optimized to process files. That's what it does, a very specific job. vbscript is different. It needs an interpreter and its bigger monster than SED.


----------



## Squashman (Apr 4, 2003)

Regular expressions are apart of everyday programming. I was just reading an article about how they do this kind of stuff in the PowerShell and regular expressions are even used then.

I actually switched to using that 3rd party utility SFK because people do tend to understand that more then SED but quick one liners when I have to process a million records, I will usually use SED.

I personally couldn't even tell you what your VBscript does from reading the code. But I come from the old school of programming. I am sure it is understandable to people who program in VB or even C++ but when you have spent most of your life programming Fortran, PL1 and unix shell scripting, what you posted above is like a Foreign Language to me.


----------



## ghostdog74 (Dec 7, 2005)

I am sure you know what the code means, you are just being humble.
1) OpenTextFile , its intuitive enough that it means open a text file for processing.
2) Do until ... Loop. By looking at this i am sure you know its a loop going over the file
3) ReadLine and Replace, i am also sure you know what they are?

Now, that's not difficult , is it? 

I am also sure that before you even know SED, you don't understand its usage and syntaxes too. Its all a matter of reading up and trying to understand how your tools work.


----------



## computerman29642 (Dec 4, 2007)

ghostdog74 said:


> as usual, there's no need to download anything. You can do it with batch, or vbscript
> 
> ```
> Set objFS = CreateObject("Scripting.FileSystemObject")
> ...


I tried to use the code provided, but I am unable to get it to work. When I run the code, nothing in the test.txt file changes.

What is specific word is it looking for, and what word is suppose replace that word?


----------



## Squashman (Apr 4, 2003)

You put what you are searching for where it says pattern and what you want to change it to where it says changed. Replace those two words.

The script also points to the file being in C:\test
Change it to wherever the file is.


----------



## ghostdog74 (Dec 7, 2005)

the script just echoes the lines involved to your terminal. use the output redirection operator to generate a new file

```
cscript /nologo script.vbs  > newfile
move newfile original_file
```


----------



## computerman29642 (Dec 4, 2007)

I was able to get it to work by using this code:


```
Const ForReading = 1
    Const ForWriting = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\test.txt", ForReading)

    strText = objFile.ReadAll
    objFile.Close
    strNewText = Replace(strText, "pattern", "changed")

    Set objFile = objFSO.OpenTextFile("C:\test.text", ForWriting)
    objFile.WriteLine strNewText
    objFile.Close
```
Could someone help me to break down the code? Also, is there a way to add the current date to the filename when closing?


----------



## devil_himself (Apr 7, 2007)

Squashman said:


> Sure would be great to know how to do it natively with a batch file.


Replace All Instances Of "search" With "replace"

@echo off
setLocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in (mytest.txt) do (
set tl=%%a
Set tl=!tl:search=replace!
echo !tl!>>tmp.txt )
del mytest.txt & ren tmp.txt mytest.txt


----------



## devil_himself (Apr 7, 2007)

gamecockfan said:


> I was able to get it to work by using this code:
> 
> 
> ```
> ...


VBScript Date Functions
http://www.tizag.com/vbscriptTutorial/vbscriptdate.php

How Can I Find and Replace Text in a Text File?
http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx


----------

