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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)
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.
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?
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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.
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:+-=-!
	>>"%OutFile%" echo.!Line!

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", 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
) > LJD.txt
if exist LJD.txt del tmp.txt > nul

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,"+-","-")


Set objTextFile = Nothing
Set objTextWrite = Nothing

fso.movefile input_file & ".bak", input_file
Set fso= Nothing

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
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


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)
  WScript.Echo "No filename specified."
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
  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
Set oInfile = Nothing

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

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

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

Open in new window

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:-+=-!
      >>"%OutFile%" echo.!Line!
del "F:\LJD2.txt"
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.