I am looking for a Bat Files that changes a line in a file.
Let me give you an example to make it clear.
I have a file test.properties
The file looks like this:
hostname=<hostname>
port=0
rpcqueue=0
defaultlocale=en
defaulttimeout.normal=0
defaulttimeout.long=0
defaulttimeout.xlong=0
name=admin
passwd=406abbc8192eda42cc3261235959e23a8e223f6ab7f10d8cdf2e22fca
So I want to replace the value behind passwd but my problem is I don't know this value I only know the value with which i want to replace the old one.
It should also be possible to enter the file name, the attribute value (passwd) and the new value over command line so that I could use this file for other files to.
Sorry if this question is already somewhere but I couldn't find it.
My knowledge about bat is not superior and I am learning.
Thanks!
Use the script below in command line as folowing:
script.bat
eg> script.bat test.txt passwd someval
script will parse your file looking for "varName=", if it finds it, it replaces value with newVarValue.
All output is redrected to a temp file and after that temp file replaces initial one.
@echo off
set "filepath=%~1"
set "pname=%~2"
set "value=%~3"
(
for /F "tokens=*" %%a in ('type "%filepath%"') do (
echo "%%a" | findstr /C:"%pname%=" > nul 2>nul
if "!errorlevel!"=="0" (
echo %pname%=%value%
) else (
echo %%a
)
)
)>"%filepath%.tmp"
move /Y "%filepath%.tmp" "%filepath%"
Launch this batch file like this: Mybatch newpassword
This uses a helper batch file called repl.bat
- download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat
in the same folder as the batch file or in a folder that is on the path.
@echo off
type "test.properties" |repl "(passwd=).*" "$1%1" >"newfile.properties"