Link to home
Start Free TrialLog in
Avatar of sparker1970
sparker1970Flag for United States of America

asked on

Batch File to Replace Text in a Text File

I have a text file "C:\TextOutput\LJD.txt" where I want to replace "+-" (without the quotes with) "-" (again, without the quotes)

My only option is to do this within a .bat file without installing any 3rd party software.

+-00000000000000 becomes -00000000000000
Avatar of Kimputer
Kimputer

Is VBscript an option (works out of the box since WinXP)? Also, is replaceing the "+- " to "-" without any conditions (any location, any occurrence? or are there restrictions, like has to be before numbers, always the first position etc)
Avatar of sparker1970

ASKER

Anything "out-of-the-box" within Windows is an option. Lots of security lock-downs in the office and there are no conditions. If "+-" exists, replace with "-". Also, I want to save the same file without creating a new one.
Sadly, I can write a VBscript file for this, but it HAS TO BE created as a new file (after that, of course the old file will be deleted and the new file will be renamed to the original). Is that an option?
As long as I finish with the filename the same as when I started, it's all good. There is another script I do not have control of that will look for this file at a specific time and upload it to an FTP site so keeping the same name at the end is critical.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
With a "pure" batch file the issue is the same - we need two files temporarily. And it will remove all empty lines.
@echo off
setlocal EnableDelayedExpansion
pushd C:\TextOutput
ren LJD.txt tmp.txt
(for /F "tokens=*" %%L in ('C:\TextOutput\tmp.txt") do (
   set line=%%L
   echo,!line:+-=-!
) > LJD.txt
if exist LJD.txt del tmp.txt > nul
popd
endlocal

Open in new window

Here's the VBscript option (I like it because program lines are almost human readable, errors are more clear, line numbers etc):

input_file =  "D:\temp\test1.txt"

Set fso=wscript.CreateObject("Scripting.FileSystemObject")
Set objTextFile = fso.OpenTextFile(input_file, ForReading)
Set objTextWrite = fso.CreateTextFile(input_file & ".bak")

Do Until objTextFile.AtEndOfStream
	strNextLine = objTextFile.Readline
	strNextLine = Replace(strNextLine,"+-","-")
	objTextWrite.Writeline(strNextLine)
Loop

objTextFile.Close
objTextWrite.Close

Set objTextFile = Nothing
Set objTextWrite = Nothing

fso.deletefile(input_file)
fso.movefile input_file & ".bak", input_file
Set fso= Nothing

Open in new window

Here is a small VBS approach that can replace the file in place.  It can be run two ways, with one file name, or two file names.  If one is specified it will overwrite the file.  If two are specified (good for testing) it will write the changed version to the second file name.

cscript myscript.vbs file1.txt file2.txt

or

cscript myscript.vbs file1.txt

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

sData = Replace(sData, "+-", "-")

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing
' MsgBox "Conversion done."
Wscript.Quit

Open in new window

~bp
I used the "plain batch version" posted and made some modifications.

move "F:\LJD.txt" "F:\LJD2.txt"
@echo off
setlocal enabledelayedexpansion
set InFile=F:\LJD2.txt
set OutFile=F:\LJD.txt
if exist "%OutFile%" del "%OutFile%"
for /f "tokens=1* delims=[]" %%a in ('type "%InFile%" ^| find.exe /n /v ""') do (
      set Line=%%b
      if defined Line set Line=!Line:-+=-!
      echo.!Line!
      >>"%OutFile%" echo.!Line!
)
del "F:\LJD2.txt"