update more than one value in parameter file using batch script - batch-file

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

Related

Batch script - Create folder issue

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.

Replace field value in property file using batch

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%

How to find and replace all files in a folder using a dynamic value

I'm having a few html files where I need to add the pagename as a tag value. Plenty of tools to do mass find and replace, but than you are restricted to a static text, while I need to add the actual filename.
It works perfectly fine on a single file, but if I start to loop into a directory it goes wrong, and I'm not able to figure out where things go wrong.
Doing this on a single predefined file works as expected :
#echo off & setLocal enableDELAYedexpansion
rem Create tmp file to store modified information
set tmpFile=%CD%\test\tmp.txt
set myFile=%CD%\test\test.html
set page=test
rem delete current tmp file
if exist "%tmpFile%" del /f /q "%tmpFile%"
rem find and replace values
set search="container"
set replace="container_!page!"
rem loop through all the lines and replace the string in question
for /F "tokens=*" %%a in (%myFile%) do (
set myLines=%%a
set myLines=!myLines:%search%=%replace%!
echo !myLines! >> %tmpFile%
)
rem update current file with updated content
type !tmpFile! > !myFile!
)
pause
The above nicely finds my string, and replaces it with the one I want (though still static in this case)
But if I want the same logic to loop through more files it goes wrong. This is the code I use :
#echo off & setLocal enableDELAYedexpansion
rem Create tmp file to store modified information
set tmpFile=%CD%\test\tmp.txt
rem switch to test folder
cd %CD%/test
rem Start the loop
For /R %%F in (*.html) do (
rem get page name
set page=%%~nxF
set MyFile=%%F
rem delete current tmp file
if exist "%tmpFile%" del /f /q "%tmpFile%"
rem find and replace values
set search="container"
set replace="container_!page!"
rem loop through all the lines and replace the string in question
for /F "tokens=*" %%a in (%%F) do (
set myLines=%%a
set myLines=!myLines:%search%=%replace%!
echo !myLines! >> %tmpFile%
)
rem update current file with updated content
type !tmpFile! > !myFile!
)
pause
This basically doesn't do anything, I've been trying some otehr variations but these result in either empty pages or pages that contain myLines= as a literal string.
What am I overlooking here ?
[EDITED AREA]
I've managed to get it working partially, but have strange unwanted side effects.
Not using search / replace parameters but using it directly in the replace parts as follows works :
for /F "tokens=*" %%a in (%%F) do (
set myLines=%%a
set "myLines=!myLines:container=container_%%~nF!"
echo !myLines! >> %tmpFile%
)
This replaces container with container_pagename as I wanted , BUT it also removes all esclamation marks on my htm, which is quite annoying, as it ruins all the comments on the page.
so <!-- comment -->
becomes <-- comment -->
Any clue where it goes wrong, or why using parameters doesn't work for me ?
type !inputfile! > !tmpFile!
might be a problem. Suggest you consider what it does...
I'd also change
set var="value"
to
set "var=value"
The first assigns a quoted value to var; the second ensures that stray spces on the line are not included in value assigned to var

Batch file that will modify date in txt file

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.

batch: filenames with variables evaluated inside a for loop

File names and directories containing variables are being evaluated indside procedures. How do I make batch store the file's name as a variable without evaluating it?
EG: my path is:
"C:\Rare\Names in a File\%CD%\"
batchfile.bat
file.txt
I want to run a script inside this directory:
REM #####BATCHFILE.BAT#######
pushd "%dp0"
for %%A in ("C:\Test") do set EXEPATH=%%~A
for %%A in ("Test.exe") do set EXECUTABLE=%%~A
for %%A in ("%CD%") do set DIRNAME=%%~A
for %%A in ('dir /b /a-d "*.txt"') do (
SET FILENAME=%%~A
CALL :PROCEDURE
)
exit /b
:PROCEDURE
start /w "" "%EXEPATH%\%EXECUTABLE%" "%DIRNAME%\%FILENAME%"
I have made a menu in batch which is a little bit different to your problem.
You need to have one variable per file. The easiest way is to just increment their names:
var1=path1\exe1
var2=path1\exe2
var3=path2\exe1
var4=path2\exe2
On each variable in the for loop you could call a procedure which saves the file with the path in a variable.
for ....(
call :Var %path% %file%
)
:Var
set /a id+=1
set %id%.path=%1&& shift
set %id%.file=%1
goto:EOF
after this you could do
for ..(1,1,%id%) do (
start !%id%.path!\!%id%.file!
)
if you want to have a look at my Batch menu, you can contact me.

Resources