Can anyone help me to replace a field value in property file using batch.
I have this field in my application.properties file: accessKey=AKIAJ2Q and I want to replace it with accessKey=XXXXX
I tried with sed command as recommanded here but it didn't work. The send command is not recognized by windows 10.
I tried also the following code:
SET key="XXXXX"
FOR /F %i IN (application.properties) DO SET accessKey=%key%
pause
Any help would be appreciated.
Thanks
There is surely a way to replace a string inside a txt/properties file using pure batch.
Taking above example to replace accessKey in application.properties
file.
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\<Add directory path of application.properties>"
(
FOR /f "usebackqdelims=" %%a IN ("%sourcedir%\application.properties") DO (
FOR /f "tokens=1*delims==" %%g IN ("%%a") DO (
IF /i "%%g"=="accessKey" (
ECHO(%%g=xxxx
) ELSE (
ECHO(%%a)
)
)
)>application_new.properties
:: This new file now contains a modified version.
rem ECHO(MOVE /y application_new.properties "%sourcedir%\application.properties"
GOTO :EOF
There is no way to "replace" something inside a text file with pure batch. You could use a 3rd party tool like Find'n'Replace or even some PowerShell commands. But what you can do is to "copy" the text file into a temp file while replacing the token you are looking for. Afterwards you can delete the original file and replace it with the temporary one. Here is the code:
#echo off
setlocal enabledelayedexpansion
set sourcefile=something.txt
set tempfile=tempfile.txt
set oldtoken=AKIAJ2Q
set newtoken=XXXXX
type nul>%tempfile%
for /f "tokens=*" %%l in (%sourcefile%) do (
set line=%%l
set line=!line:%oldtoken%=%newtoken%!
echo !line!>>tempfile.txt
)
del %sourcefile%
move %tempfile% %sourcefile%
Related
i have one property file where i need to update the more than one value in property file using batch . i have using the script
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
call %~dp0\Batchenv.bat
SET sourcedir=C:\Users\1026478\Desktop\local
(
FOR /f "usebackqdelims=" %%a IN ("%sourcedir%\test.Property") DO (
FOR /f "tokens=1*delims==" %%g IN ("%%a") DO (
IF /i "%%g"=="INSTALL_PROD" (ECHO(%%g=%INSTALL_PROD%
) ELSE (ECHO(%%a)
)
)
)>newfile.property
:: newfile.txt now contains a modified version.
:: This line will overwrite the original
MOVE /y newfile.Property "%sourcedir%\test.Property"
GOTO :EOF
but this script only update one value in property file.. i need to update more than one value .
my property file like
test.property
I2_JDK_HOME=D:\\Java8\\jdk1.8.0_181
I2_LICENSE_FILE=D:\\license\\PSA100417.lic
I2_LICENSE_FILE_LOCATION=D:\\license
I2_LICENSE_FILE_NAME=PSA100417.lic
I2_ORACLE_DRIVER=oracle.jdbc.driver.OracleDriver
I2_ORACLE_HOME=D:\\Oracle64\\product\\12.1.0\\client_1
I2_ORACLE_HOST=sl1psadevdb2v.jdadelivers.com
I2_ORACLE_INFO_1=ABPPMGR
I2_ORACLE_INFO_2={E:AES}6AA9880B2F94C92CA09E7665CC4AA76B
I2_ORACLE_INFO_2_XOR=#CQQLFS
I2_ORACLE_INFO_3=PSADVDB
I2_ORACLE_INFO_4=
I2_ORACLE_JAR=D:\\Oracle64\\product\\12.1.0\\client_1\\jdbc\\lib\\ojdbc7.jar
I2_ORACLE_PORT=1521
I2_PLATFORM_HOME=
I2_WEBCLIENT_TYPE=NoWeb
I2_WEBLOGIC_BASE_APP=D:\\JDA\\JDA2017_3\\config\\JDAv2017_SEMs1\\web\\base
I2_WEBLOGIC_DOMAIN_HOME=C:\\bea\\user_projects\\domains\\mydomain
I2_WEBSPHERE_APPNAME=
I2_WEBSPHERE_APPNAME_BASE=base
I2_WEBSPHERE_CONTEXTROOT=
I2_WEBSPHERE_CONTEXTROOT_BASE=/base
I2_WEBSPHERE_HOME=C:\\Program Files\\IBM\\WebSphere\\AppServer
I2_WEBSPHERE_INSTALLABLE=
I2_WEBSPHERE_NODE=
I2_WEB_SERVER_PORT=22246
INSTALLER_UI=SWING
USER_INSTALL_DIR=D:\\JDA\\JDA2017_3\\config\\JDAv2017_SEMs1
i need to update all the value..
can anyone pls help me on this
[Edit /]
The variable INSTALL_PROD is set in another batch-file file.
Batchenv.bat:
echo off
SET sourcedir=C:\Users\1026478\Desktop\local
SET INSTALL_PROD=Prod
SET APPSERVER_PORT2=44444
SET /a ABPP_BRE_PATH=D:\\JDA\\JDA2017_3\\config\\JDAv2017_SEMs1\\bre
The information you wish to input into the new file should be Appended, not overwitten
To append Information use >> instead of >
For Example, If you wish to add the value of %%a into the file, you would:
Echo(%%a>>newfile.property
I'm trying to create a batch script that creates folders based on lines in a text file. I want to be able to automate the "D" function so no typing required.
#echo off
set /P D="Enter Name e.g. ABCD:"
for /F "delims=" %%a in (list.txt) DO (
FOR %%x IN (
"C:\test\%D%\%%a"
"C:\test\%D%\%%a\Folder1"
"C:\test\%D%\%%a\Folder2"
) DO (
MD "%%~x"
)
)
My List file looks like this
JACK-12345
JOHN-12345
I'm wanting to read the first 4 characters of each line and create directories in relevant folders that already exist.
So JACK-12345 will create a folder in C:\test\JACK\JACK-12345 and JOHN will create a folder C:\test\JOHN\JOHN-12345
Any help would be greatly appreciated!
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetdir=U:\destdir"
for /F "delims=" %%a in (q35285628.txt) DO (
SET "ch4=%%a"
SET "ch4=!ch4:~0,4!"
FOR %%x IN (
"%targetdir%\!ch4!\%%a"
"%targetdir%\!ch4!\%%a\Folder1"
"%targetdir%\!ch4!\%%a\Folder2"
) DO (
ECHO(MD "%%~x"
)
)
GOTO :EOF
You would need to change the setting of targetdir to suit your circumstances.
I used a file named q35285628.txt containing your data for my testing.
The required MD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists)
Invoke delayedexpansion to allow substringing of the metavariable %%a Assig the entire string as read to ch4 then select the first 4 characters.
!var! required within the block to access the changing value of the variable at run-time.
For the extended question (unspecified : where "maths" etc fits into the tree - additional, replacement, whatever??
Using the "meat" of the routine:
SET "ch4=%%a"
SET "ch4=!ch4:~0,4!"
SET "folder1=Folder1"
if /i "!ch4:~0,1!"=="m" SET "folder1=Maths"
if /i "!ch4:~0,1!"=="s" SET "folder1=Science"
FOR %%x IN (
"%targetdir%\!ch4!\%%a"
"%targetdir%\!ch4!\%%a\!folder1!"
"%targetdir%\!ch4!\%%a\Folder2"
) DO (
ECHO(MD "%%~x"
)
)
would replace "folder1" from the original scheme with "Maths" if the first character of the line read from the file was "m" (in either case), "Science" if it was "s" and "Folder1" otherwise.
I have a line in a text file that I'm trying to modify using a batch file. The line in this case is the lastexportdate=2014-01-01. I'm trying to get the batch file to modify this from
lastexportdate=2014-01-01
to
lastexportdate= Current timestamp.
Is this possible to have this equal the timestamp of my computer? Not certain how to go about tackling this, so if anyone could help that would be awesome. I'm using Windows 7.
File contents below:
root.footer=</CREDITEXPORT>
root.header=<CREDITEXPORT>
emailaddronsuccess=test#test.com
emailaddronerror=test#test.com
rerunexportdate=2014-09-06
errorfilelocation=C:\\
lastexportdate=2014-01-01
xmloutputlocation=C:\\
#echo off
setlocal
rem getting current time stamp
set "beginJS=mshta "javascript:var t=new Date();var dd=t.getDate();var mm=t.getMonth()+1;var yyyy=t.getFullYear();if(dd^<10) dd='0'+dd;if(mm^<10) mm='0'+mm;close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write("
set "endJS=));""
:: FOR /F does not need pipe
for /f %%N in (
'%beginJS% yyyy+'-'+mm+'-'+dd %endJS%'
) do set cds=%%N
echo cds=%cds%
rem processing the file
rem set the location of the file
set "content_file=c:\some.file"
break>temp.file
for /f "usebackq tokens=1* delims==" %%a in ("%content_file%") do (
if "%%a" NEQ "lastexportdate" echo %%a=%%b>>temp.file
if "%%a" EQU "lastexportdate" echo %%a=%cds%>>temp.file
)
Not tested.Just change the location of the content_file and check if the created temp.file is ok.
To replace the file add move temp.file "%content_file%" at the end.
I have the following batch script to read an xml file and find a word (in this case, "factory"):
#echo off & setlocal enabledelayedexpansion
for /f "delims=" %%a in ('findstr /C:"factory" xcsconfig.xml') do set content=%%a
set content=%content:*"=%
set content=%content:~0,-1%
echo %content%
exit /b
Here's part of the xml file:
<loggers>
<recorder1>
<add name="factory" value="xlog"/>
<add name="alias" value="WSEnterprise.log"/>
</recorder1>
<recorder2>
<add name="factory" value="weblog"/>
</recorder2>
</loggers>
The code works fine and will always return the "first" founding - value="weblog"/. My question is, is there a way to return the founding under a specific tab? (i.e. I want to search specific under recorder1 instead of record2 tab, and return answer value="xlog"/). Thanks in advance.
EDIT: I changed my expected answer, it was incorrect
#ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sectionstart=recorder1"
SET "insection="
for /f "delims=" %%a in ('type q25062317.txt') do (
IF DEFINED insection (
ECHO "%%a"|FINDSTR /c:"factory" >NUL
IF NOT errorlevel 1 SET "content=%%a"
)
ECHO "%%a"|FINDSTR /i /L /c:"<%sectionstart%>" > NUL
IF NOT ERRORLEVEL 1 SET insection=Y
ECHO "%%a"|FINDSTR /i /L /c:"</%sectionstart%>" > NUL
IF NOT ERRORLEVEL 1 SET "insection="
)
set content=!content:*"=!
set content=!content:*"=!
set content=!content:~1,-1!
echo %content%
GOTO :EOF
I used a file named q25062317.txt containing your data for my testing.
You can incorporate also the next statements inside the do block of the for cycle. I mean:
setlocal EnableDelayedExpansion for /f "delims=" %%a in ('findstr /C:"factory" xcsconfig.xml') do (
set content=%%a
set content=!content:*"=!
set content=!content:~0,-1!
echo !content!
)
In this way the output is not only the last XML code line but all the code lines that contain the "factory" string in the file considered. Of course this example doesn't echo only a single desired string but this is possible setting a condition to the output of the loop.
Here is a robust tool that can help you - the command looks like this to get the line you need and it can be wrapped in a for /f loop.
type file.xml |findrepl "<recorder1>" /e:"</recorder1>" /b:"factory"
This uses a helper batch file called findrepl.bat (by aacini) - download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat
Place findrepl.bat in the same folder as the batch file or on the path.
I have a text file with the names of computer names and corresponding static i.p. addresses in the following format.
COMPUTER NAME:PC ADDRESS=154.100.1.1 MASK=255.255.254.0
COMPUTER NAME:PC2 ADDRESS=100.100.1.1 MASK=255.255.254.0
I would like to take the values from each line and put them as variables in a batch file for use later. Is this possible? The overall goal is to have the values from this easily edited text file to be used in netsh commands in another batch file.
I've looked around and found ways to take lines of a text file and place them in one variable using the snippet below. However, I do not know how to create multiple variables from one line. If someone could help me with this I'd greatly appreciate it!
#echo o
setlocal enabledelayedexpansion
set Counter=1
for /f %%x in (D:\COMP_T.txt) do (
set "comp!Counter!=%%x"
set /a Counter+=1
)
This should work:
#echo off
setlocal EnableDelayedExpansion
set "Count=1"
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%A in (C:\File.txt) do (
set "%%A[!Count!]=%%C"
set "%%D[!Count!]=%%E"
set "%%F[!Count!]=%%G"
set /a "Count+=1"
)
:: Call other batch script here.
endlocal
Example Output:
COMPUTER[1]=PC
COMPUTER[2]=PC2
ADDRESS[1]=154.100.1.1
ADDRESS[2]=100.100.1.1
MASK[1]=255.255.254.0
MASK[2]=255.255.254.0
Here is a solution that avoids the need for delayed expansion. It uses FINDSTR to insert a line number followed by : at the beginning of each line. The search string of "^" is guaranteed to match every line in the file.
The only other issue is to set TOKENS and DELIMS to parse the line properly.
#echo off
setlocal
for /f "tokens=1,4,6,8 delims=:= " %%A in ('findstr /n "^" "d:\comp_t.txt"') do (
set "comp%%A=%%B"
set "addr%%A=%%C"
set "mask%%A=%%D"
set "counter=%%A"
)
To use the set of variables in another batch file, line by line, just parse the lines as done in other answers here, and call the other batch file with the metavariables.
#echo off
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%a in ('type "File.txt" ') do (
echo "computer_name=%%c"
echo "address=%%e"
echo "mask=%%g"
Call "batch script" "%%c" "%%e" "%%g"
)