Looking to create a batch file capable of editing a line (or word) in a text file
See if this helps. I had this from when I needed to perform a search and replace on an XML file. The strings, as long as you enclose them in double-quotes, can have special characters such as greater than and less than (used as redirection by scripts, ignored here). If you don't like the usage of variables to define the files you want to modify, you could make them into arguments which you could pass to the script. But then you would have to change the logic to shift the parameters unless you explicitly say "parameter 1 is the file to be changed, parameter 2 is the string to be found, etc..."
Hope this helps someone. Windows XP or higher is required.
::Find and Replace script allows the user to
::define a file path, file name and a string
::to find and replace so as to create a new file.
::
::Original file is backed up with an added extension of *.bak, in case
::the user finds the need to go back to the original.
@echo off
::Use the path from whence the script was executed as
::the Current Working Directory
set CWD=%~dp0
::***BEGIN MODIFY BLOCK***
::The variables below should be modified to the
::files to be changed and the strings to find/replace
::Include trailing backslash in _FilePath
set _FilePath=Path\To\File\
set _FileName=FileNameToModify
::_WrkFile is the file on which the script will make
::modifications.
set _WrkFile=BackupFileName
set OldStr="The string to be found and replaced, enclosed in double-quotes"
set NewStr="The new string to replace the value of OldStr, enclosed in double-quotes"
::***END MODIFY BLOCK***
::Set a variable which is used by the
::search and replace section to let us
::know if the string to be modified was
::found or not.
set _Found=Not found
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
if not exist "%_FilePath%%_FileName%" goto :NotFound
::If a backup file exists, delete it
if exist "%_FilePath%%_WrkFile%" (
echo Deleting "%_FilePath%%_WrkFile%"
del "%_FilePath%%_WrkFile%" >nul 2>&1
)
echo.
echo Backing up "%_FilePath%%_FileName%"...
copy "%_FilePath%%_FileName%" "%_FilePath%%_WrkFile%" /v
::Delete the original file. No worries, we got a backup.
if exist "%_FilePath%%_FileName%" del "%_FilePath%%_FileName%"
echo.
echo Searching for %OldStr% string...
echo.
for /f "usebackq tokens=*" %%a in ("%_FilePath%%_WrkFile%") do (
set _LineChk=%%a
if "!_LineChk!"==%OldStr% (
SET _Found=Found
SET NewStr=!NewStr:^"=!
echo !NewStr!
) else (echo %%a)
)>>"%_FilePath%%_FileName%" 2>&1
::If we didn't find the string, rename the backup file to the original file name
::Otherwise, delete the _WorkFile as we re-created the original file when the
::string was found and replaced.
if /i "!_Found!"=="Not found" (echo !_Found! && del "%_FilePath%%_FileName%" && ren "%_FilePath%%_WrkFile%" %_FileName%) else (echo !_Found! && del "%_FilePath%%_WrkFile%")
goto :exit
:NotFound
echo.
echo File "%_FilePath%%_FileName%" missing.
echo Cannot continue...
echo.
:: Pause script for approx. 10 seconds...
PING 127.0.0.1 -n 11 > NUL 2>&1
goto :Exit
:Exit
exit /b
Related videos on Youtube
Steve
Updated on September 18, 2022Comments
-
Steve over 1 year
I have a text file (...\config.txt) containing 2 lines which looks like:
Preamp: -4 dB Include: surround.txt
I need to change "surround" to "headphones" in that config file. I really need to make 2 batch files. One for "headphones" and one for "surround".
The config file in question is a file related to EqualizerAPO that calls a separate file containing EQ settings for a particular listening environment. In the example above, "surround". However, there can be many others. That config file, when saved, automatically triggers the EQ settings it's calling under the "include" line. The reason I need this is because, if I need to change the listening environment to something else, I need to browse to the directory, open the config.txt file, edit the "Include" section to my needs, then save. It becomes tedious when I have to change it when the need arises. I really need a one-click solution. Any help would be greatly appreciated. Thank you!
EDIT:
The directory (containing that "config.txt" file referred to earlier):
C:\Program Files\EqualizerAPO\config\
contains 2 types of text files: EQ settings files (e.g. headphones, surround, theater, etc. Basically the files that users create and export in TXT format via software called Room EQ Wizard. We'll call them "reference" files.) The other is the actual single config file that EqualizerAPO uses to determine which "reference" file it needs to use and adjust it's EQ settings accordingly. EqualizerAPO attaches itself to the system effect infrastructure as an Audio Processing Object. All I need is a simple solution that would allow me to change the line "Include: surround.txt" to whatever other EQ file I need to use at that time. I am constantly switching between EQs, hence my coming here. If possible, I'd like to be presented with a list of choices.
For example:
Select from the following equalizers: 1. Headphones 2. Surround 3. Theater 4. *** 5. *** Enter the number:_
And then exit when the number is chosen.
Ideally, it'd be nice to have a very minimal application of some sort running in the taskbar that allows a one-click solution, removing the use of batch files entirely. However, that is not a necessity. Batch files are fine for the time being.
-
user2196728 over 10 years
I really need to make 2 batch files. One for "headphones" and one for "surround"
What do you mean ? A batch file to changesurround
toheadphones
and another batch file to changeheadphones
tosurround
? And finally, is batch mandatory or Powershell is an available option ? -
cybernard over 10 yearsWhy not just make 2 copies of the file config.txt and config-2.txt and just rename them as needed?
-
user2196728 over 10 years+1 to cybernard ! This way your batch files will just use
rename
:) easy ! -
dbenham over 10 yearsRegarding cybernard suggestion - it requires at least 2 RENAMEs per change. I suggest using a single COPY instead. So you could have headphones.txt and surround.txt, and simply copy either one to config.txt as needed.
-
Wally over 10 yearsI've used this method for many applications, on both Unix and Windows environments. Just be sure you have the correct permissions for writing, as needed. In other words, be sure your script "runs as admin" if the files are protected.
-
-
barlop over 8 yearsYou should state at the beginning of your question that the code that follows is not a batch file. apart from 7 lines at the end