views:

6549

answers:

5

I want to create a batch while which finds specific lines in a batch file and are able to edit these lines.

Example:

//TXT FILE//

ex1
ex2
ex3
ex4

i want to let the batch file find 'ex3' and edit this to 'ex5' to let it look like this:

ex1
ex2
ex5
ex4
+3  A: 

This is the kind of stuff sed was made for (of course, you need sed on your system for that).

sed 's/ex3/ex5/g' input.txt > output.txt

You will either need a Unix system or a Windows Cygwin kind of platform for this.
There is also GnuWin32 for sed. (GnuWin32 installation and usage).

nik
this is too many stuff that i need. I want to set this public; so it should be done easily.
YourComputerHelpZ
+1  A: 

on a native Windows install, you can either use batch(cmd.exe) or vbscript without the need to get external tools. here's an example in vbscript

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    If InStr(strLine,"ex3")> 0 Then
     strLine = Replace(strLine,"ex3","ex5")
    End If 
    WScript.Echo strLine
Loop

save as myreplace.vbs and on the command line

c:\test> cscript /nologo myreplace.vbs  > newfile
c:\test> ren newfile file.txt
ghostdog74
Thanks a lot, Helps me!
YourComputerHelpZ
A: 

There is no search and replace function or stream editing at the command line in XP or 2k3 (dont know about vista or beyond). So, you'll need to use a script like the one Ghostdog posted, or a search and replace capable tool like sed.

There is more than one way to do it, as this script shows:

  @echo off
        SETLOCAL=ENABLEDELAYEDEXPANSION

        rename text.file text.tmp
        for /f %%a in (text.tmp) do (
            set foo=%%a
            if !foo!==ex3 set foo=ex5
            echo !foo! >> text.file) 
    del text.tmp
RobW
A: 

An alternate script. Assume file is at C:\file.

# Script ex35.txt
# Read file in.
var str content ; cat "C:\file" > $content
# Replace "ex3" with "ex5".
sal "^ex3^" "ex5" $content > null
# Write file back.
echo $content > "C:\file"

Script is in biterscripting ( http://www.biterscripting.com ) . To try, save the script as C:\Scripts\ex35.txt, start biterscripting, enter the following command.

script "ex35.txt"

Make sure you change C:\file to the correct file path. Enclose all file names and paths in double quotes.

P M
i assume this is bash. i asked for BATCH, Windows.
YourComputerHelpZ
A: 

Hello all,

Using ghostdogs example, is there a way l can pass variables from a .bat file to a VB script, or vise-verse.

Maybe using prompts in the VB or SET / P comments in a batch.

I am currently using a batch file to rename files before it calls on a number of batch files to run file merges and install services in a windows environment. Currently I am using the batch file to set the variables with SET /P questions but I would love to be able to automate the process so it can open a .ini file or .txt (easy to change it to a .txt) change common text within the file before calling on the other batch files etc.

This would allow me to use a base set of file with the batch/VB without me having to open the file and manually change it each time...can anyone help.

Don't know much about VB, but know a little about dos commands....willing to go the other way e.g. use the VB script to pass the variables to the batch file, but not sure how to do that either....

I did try the first example, however, when the file writes back it does not write the full line, it does not write anything after a space in the line, l will try the other 2 example and see if l get the same result...with most .ini files the string sometimes have spaces.