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
sparker1970Asked:
Who is Participating?
 
oBdACommented:
That's a one-liner in Powershell:
(Get-Content C:\Temp\test.txt) | % {$_.Replace("+-", "-")} | Set-Content C:\Temp\test.txt

Open in new window

And for the fun of it, here's a plain batch version; whether it works depends in the exact contents of the input file.
It currently writes to a second file; if it does the job, it could be changed to overwrite the original.
@echo off
setlocal enabledelayedexpansion
set InFile=C:\Temp\test.txt
set OutFile=C:\Temp\test2.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!
)

Open in new window

0
 
KimputerCommented:
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)
0
 
sparker1970Author Commented:
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.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
KimputerCommented:
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?
0
 
sparker1970Author Commented:
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.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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

0
 
KimputerCommented:
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

0
 
Bill PrewCommented:
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
0
 
sparker1970Author Commented:
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"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.