Hiya!
Let me explain what I want to do...
I am trying to make a DOS-only batch file that will edit a variable in a text file , the variable depends on the location of the .bat file...
so for example if I have:
c:\test\varedit.bat
it would open a file , look for a predefined string like %EDITME% and change it to C:\TEST\
I know I can get to a dos variable the current .bat file location with %~dp0 , but how to make %~dp0 replace the %EDITME% string in my file?
any pointers would be highly appreciated!
How to make a BAT to edit a string inside a test file?
Moderator: DosItHelp
Re: How to make a BAT to edit a string inside a test file?
Code: Select all
@echo off &setlocal
set file=test.txt
set replacevar=%%editme%%
type nul>"temp.txt"
for /f "delims=: tokens=1*" %%a in ('findstr /n /r /c:"^" "%file%"') do set "line=%%b" &call :process
move "temp.txt" "%file%"
goto :eof
:: end of main
:process
if not defined line (
>>"temp.txt" echo.
goto :eof
)
set "line=%line:^=^^%"
set "line=%line:&=^&%"
set "line=%line:<=^<%"
set "line=%line:>=^>%"
set "line=%line:|=^|%"
setlocal enabledelayedexpansion
set "line=!line:%replacevar%=%~dp0!"
endlocal &>>"temp.txt" echo %line%
goto :eof
Note that you have to use %%EDITME%% instead of %EDITME%.
Regards
aGerman
Re: How to make a BAT to edit a string inside a test file?
thanks but it did not work... I should have pointed that I am using a DOS-only enviroment from win98se....
I get a "Syntax error" after: for /f "delims=: tokens=1*" %%a in ('findstr /n /r /c:"^" "%file%"') do set "line=%%b" &call :process
and the resulting test.txt is a 0 byte file (previous it only had some random line with %%EDITME%% in the end) is there a trick to do it in such an old DOS version?
I get a "Syntax error" after: for /f "delims=: tokens=1*" %%a in ('findstr /n /r /c:"^" "%file%"') do set "line=%%b" &call :process
and the resulting test.txt is a 0 byte file (previous it only had some random line with %%EDITME%% in the end) is there a trick to do it in such an old DOS version?
Re: How to make a BAT to edit a string inside a test file?
You could try beginning your code with
also for your other thread.
But surely you need cmd.exe and afaik it's available on Win NT and higher versions.
Regards
aGerman
Code: Select all
@echo off &setlocal ENABLEEXTENSIONS
also for your other thread.
But surely you need cmd.exe and afaik it's available on Win NT and higher versions.
Regards
aGerman