Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: .bat file making search and replace in text file  (Read 55592 times)

0 Members and 1 Guest are viewing this topic.

Wassberg

  • Guest
.bat file making search and replace in text file
« on: February 23, 2006, 03:04:51 AM »
Does anyone know how to write a .bat that looks for a string in a text file and replaces it with something else?

carlos

  • Guest
Re: .bat file making search and replace in text fi
« Reply #1 on: February 23, 2006, 04:51:24 AM »
You need to read the help for command SET:

Quote
SET /?
...
Environment variable substitution has been enhanced as follows:

%VAR:str1=str2%

would expand the VAR environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1.
...
« Last Edit: February 23, 2006, 09:33:06 AM by carlos »

Wassberg

  • Guest
Re: .bat file making search and replace in text fi
« Reply #2 on: February 24, 2006, 01:57:26 AM »
Thanks!

I have tried this but it does not run as I expected.

I have the file test.txt containing the single word "Charlie".

the bat script looks as follows:

set var=
for /f "tokens=*" %%A in ('type test.txt') do set myVar=%%A

echo %myvar%

set str1=Charlie
set str2=Bob

%myVar:str1=str2%

echo %myvar%


_______________________________________ ______________________
Now when I run this it returns/echos:

C:\Transfer>set var=

C:\Transfer>for /F "tokens=*" %A in ('type test.txt') do set myVar=%A

C:\Transfer>set myVar=Charlie

C:\Transfer>echo Charlie
Charlie

C:\Transfer>set str1=Charlie

C:\Transfer>set str2=Bob

C:\Transfer>Charlie
'Charlie' is not recognized as an internal or external command,
operable program or batch file.

C:\Transfer>echo Charlie
Charlie
_______________________________________ ___

so the %myVar:str1=str2% try to execute the content of str1??????

What did I miss???

carlos

  • Guest
Re: .bat file making search and replace in text fi
« Reply #3 on: February 24, 2006, 02:27:25 AM »
The sentence

       %myVar:str1=str2%

return a new variable where it's been replaced the word "str1" for "str2".
You would need:

         set myVar=%myVar:str1=str2%

But in 'myVar' there isn't the word "str1". I have tried to put:

         set myVar=%myVar:%str1%=%str2%%

but it seems that it doesn't admit variables within variables.

If you run a subshell cmd /v: on, the next work:

Code: [Select]
@echo off
for /f "tokens=*" %%A in (test.txt) do set myVar=%%A

echo %myvar%

set str1=Charlie
set str2=Bob

set myvar=!myVar:%str1%=%str2%!
 
echo [ %myvar% ]
« Last Edit: February 24, 2006, 02:30:22 AM by carlos »

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: .bat file making search and replace in text fi
« Reply #4 on: February 24, 2006, 05:19:26 AM »
Batch coding was never meant to do much more than run programs sequentially. Using batch as a programming language is like using a spoon to dig the Panama Canal. Yes, it can be done, but it's not the best tool for the job.

Code: [Select]
Const ForReading = 1
Const ForWriting = 2
Const FileIn = "c:\test.txt"
Const FileOut = "c:\test.txt"  '<== Change to prevent overwrite of original file

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(FileIn, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Charlie ", "Bob ")

Set objFile = objFSO.OpenTextFile(FileOut, ForWriting)
objFile.WriteLine strNewText
objFile.Close

Save the script with a vbs extension and run from the command line as: cscript scriptname.vbs. As written, the script will overwrite the original file. You can modify this by changing the FileOut parameter.

Just my 2˘ worth.  8-)
« Last Edit: February 24, 2006, 05:39:15 AM by Sidewinder »
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Wassberg

  • Guest
Re: .bat file making search and replace in text fi
« Reply #5 on: February 24, 2006, 05:58:13 AM »
Thanks Carlos! You are a Star!!!
That works briliant!


This really helped me out.

Wassberg

  • Guest
Re: .bat file making search and replace in text fi
« Reply #6 on: February 24, 2006, 06:10:44 AM »
Ah Yes.

I have thought of looking in to vbs just have not had time yet.
I might just do that in a hurry now though.

Thanks.

DosItHelp



    Intermediate
    Re: .bat file making search and replace in text fi
    « Reply #7 on: March 12, 2006, 01:20:05 AM »
    Since this is DOS forum here is a possible DOS solution  :D...

    At http://dostips.cmdtips.com/DtCodeBatchFiles.php you can find a section about [highlight]String Substitution[/highlight].  It shows a batch file that replaces text in a text file.

    The main task is being done by a few lines of code similar to this:

    Code: [Select]
    SETLOCAL ENABLEEXTENSIONS
    SETLOCAL ENABLEDELAYEDEXPANSION

    REM -- Parse each line
    REM -- Trick, add line numbers using find command, to preserve empty lines

    set src=<source file>
    set str1=<old string>
    set str2=<new string>

    for /f "tokens=1,* delims=]" %%a in ('"find /n /v "@#$PreserveEmptyLines$#@" "%src%""') do (
        if "%%b"=="" (
            echo.
        ) else (
            set line=%%b
            call echo.!line:%str1%=%str2%!
        )
    )

    Due to the nature of the FOR command and Delayed Expansion, some restrictions for the content of the text file apply.

    Hope this information is useful for somebody ;)


    « Last Edit: March 25, 2006, 10:42:12 PM by DosItHelp »

    DosItHelp



      Intermediate
      Re: .bat file making search and replace in text fi
      « Reply #8 on: March 21, 2006, 06:34:27 PM »
      I just figured out that find /v "" filename.txt returns all lines of a file, so the funny @#$PreserveEmptyLines$#@ is not needed.  The for command can be written like this:

      for /f "tokens=1,* delims=]" %%a in ('"find /n /v "" "%src%""') do (...