I am having an bit of trouble in replacing an string insde an .txt file with Batch
What i have now is this
:SetHostName
set /p HostName=Please Enter your hostname:
setlocal enabledelayedexpansion
set oldstring=%FTP_HOST%
set newstring=%HostName%
for /f "tokens=*" %%i in (FTPConfig.ini) do (
set str=%%i
set str=!str: %oldstring% = "hi"!
echo !str!
)
But when i test it it wont replace the FTP_HOST instead it shows this
FTP_HOST=test
FTP_USER=hi
FTP_PASS=testing
FTP_PROGRAM_FOLDER=program
FTP_RESOURCE_FOLDER=resource
inside my .txt file i have this
FTP_HOST=test
FTP_USER=hi
FTP_PASS=testing
FTP_SERVER_FILE_LOCATION=program
FTP_SERVER_RESOURCE_LOCATION=resource
Any help would be appriciated
I think you have this quite backwards. This code appears to be trying to replace the actual text FTP_HOST, not the value that appears after it. If you fixed the obvious errors, you'd have:
set oldstring=FTP_HOST
set newstring=%HostName%
for /f "tokens=*" %%i in (FTPConfig.ini) do (
set str=%%i
set str=!str:%oldstring%=%newstring%!
echo !str!
)
Assuming I enter my.new.host, that would give the output:
my.new.host=test
FTP_USER=hi
FTP_PASS=testing
FTP_PROGRAM_FOLDER=program
FTP_RESOURCE_FOLDER=resource
I really don't think that's what you intended. What you probably wanted is something like this:
set oldstring=FTP_HOST
set newstring=%HostName%
for /f "tokens=1,2* delims==" %%i in (FTPConfig.ini) do (
set str=%%j
if "%%i"=="%oldstring%" set str=%newstring%
echo %%i=!str!
)
Which yields:
FTP_HOST=my.new.host
FTP_USER=hi
FTP_PASS=testing
FTP_PROGRAM_FOLDER=program
FTP_RESOURCE_FOLDER=resource
Related
I have some lines of text. Then I have a list with test-words. I like to look up each line of the text and check if one of the test-words appears in it.
Beforehand this works well with a commands like these:
IF not "!stringToTest:%searchstring%=!"=="!stringToTest!"
However, now this seems to be more complicated as I have nested Loops?
I try to create a little MWE for my problem:
#echo off
setlocal enabledelayedexpansion
set /a counterPC=0
set "listPC=Win10,Motherboard,USB-Port,Core"
FOR %%G in (%listPC%) do (
set PCsearchVal[!counterPC!]=%%G
set /a counterPC+=1
)
set /a counterPC-=1
set "dummyline=Environment,1234,ZUIOP,Core"
FOR %%G in (%dummyline%) do (
set "stringToTest=%%G"
echo String to Test: !stringToTest!
FOR /l %%I in (0,1,%counterPC%) do (
set "searchstring=!PCsearchVal[%%I]!"
echo Test for this String: !searchstring!
IF not "!stringToTest:%searchstring%=!"=="!stringToTest!" echo Searchstring is in String to Test
)
)
endlocal
pause
In this he alway enter the IF-Condition. I know that this can may be solved with FINDSTR however in all my other code I used the search-strategy liek above. There may be just a little mistake I oversee? Many thanks in advance-
Need to copy text from one file to another.
file1 as below:
locked/agent.jms.remote.host=<AGENTHOST>
locked/agent.jms.remote.port=<AGENTPORT>
file2 will be
locked/agent.jms.remote.host=mkdfvsh_a-2341
locked/agent.jms.remote.port=1234
& need to replace when creating the second file
command i used:
SET newfile=file2
SEt filetoCOpy=file1
for /f "tokens=*" %%i in (%filetoCOpy%) do (
SET "line=%%i"
SETLOCAL EnableDelayedExpansion
SET line=!line:<AGENTHOST>=%AGENTHOST%!
SET line=!line:<AGENTPORT>=%AGENTPORT%!
echo !line!>>%newfile%
)
Result i got
locked/agent.jms.remote.host=<AGENTHOST>
locked/agent.jms.remote.port=<AGENTPORT>
variable value not changing.
Can someone help here what's wrong?
You should be getting an error with the code you posted. The < and > are treated as redirection unless they are escaped or quoted:
SET line=!line:^<AGENTHOST^>=%AGENTHOST%!
SET line=!line:^<AGENTPORT^>=%AGENTPORT%!
or
SET "line=!line:<AGENTHOST>=%AGENTHOST%!"
SET "line=!line:<AGENTPORT>=%AGENTPORT%!"
But you still have at least one problem - If you enable delayed expansion within a loop, then you must ENDLOCAL at the end of the loop, otherwise you can run out of SETLOCAL stack space.
Also, I don't see where AGENTHOST or AGENTPORT is defined.
You can try something like that :
#echo off
SET newfile=file2.txt
SEt filetoCOpy=file1.txt
set "ReplaceString1=mkdfvsh_a-2341"
set "ReplaceString2=1234"
If exist %newfile% Del %newfile%
for /f "tokens=*" %%i in (%filetoCOpy%) do (
SET "line=%%i"
SETLOCAL EnableDelayedExpansion
SET line=!line:^<AGENTHOST^>=%ReplaceString1%!
SET line=!line:^<AGENTPORT^>=%ReplaceString2%!
echo !line!>>%newfile%
)
EndLocal
start "" %newfile%
I have a file where I search for a specific text and want to replace it with another. For example:
<role-name>test</role-name>
this is for example what I want to replace it with:
<role-name>file</role-name>
The problem here would be that in the <role-name> tag might be other text then "test".
How can I find the whole line and replace it with the text that I need?
Or maybe I can retrieve the line number of the tag and replace it whole, but again I have no idea how to do that :-)
The key to doing this without external code is using the string substitution expression. I assume you want to parameterize the tag and value strings. This would also depend on the sequence not having any newline characters.
C:>type seb.bat
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET EXITCODE=0
SET "TAG=%~1"
SET "FROMVAL=%~2"
SET "TOVAL=%~3"
FOR /F "usebackq tokens=*" %%a IN (`TYPE "seb.html"`) DO (
echo %%a
SET S1=%%a
SET S2=!S1:^<%TAG%^>%FROMVAL%^</%TAG%^>=^<%TAG%^>%TOVAL%^</%TAG%^>!
ECHO !S2!
)
EXIT /B %EXITCODE%
C:>type seb.html
<role-name>test</role-name>
C:>seb role-name test file
<role-name>test</role-name>
<role-name>file</role-name>
<role-name></role-name>
<role-name></role-name>
C:>seb role-name test ""
<role-name>test</role-name>
<role-name></role-name>
<role-name></role-name>
<role-name></role-name>
C:>seb role-name "" "file"
<role-name>test</role-name>
<role-name>test</role-name>
<role-name></role-name>
<role-name>file</role-name>
The problem was that I was reading from a file and the string that I wanted to compare came in a for loop. This is my code that reads from file and writes the code that I needed, if anyone needs it :
set "nameofapp=%<display-name>"
set "nameofapp_country=% <display-name>ASD</display-name>"
set "rolename=%<role-name>"
set "rolename_country=% <role-name>ASD-role</role-name>"
set "propertiesfile=%<env-entry-value>"
set "properties_file=% <env-entry-value>java-asd.properties</env-entry-value>"
set "web=src\main\webapp\WEB-INF\web.xml"
set "web_1=web.xml"
(for /f "delims=" %%i in (%web%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "name=!line:%nameofapp%=%!"
if not !name! == !line! (
set "line=%nameofapp_country%"
)
set "role=!line:%rolename%=%!"
if not !role! == !line! (
set "line=%rolename_country%"
)
set "property=!line:%propertiesfile%=%!"
if not !property! == !line! (
set "line=%properties_file%"
)
echo(!line!
endlocal
))>"%web_1%"
del %web%
Move %web_1% "src\main\webapp\WEB-INF\"
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 am not very good at batch scripting which is why I need help with a task as simple as this.
What I want to do is to scan a file, look for a line matching a specific pattern (need not be regexp) and when it is found, change it.
The line I'm looking for looks like this:
<ApplicationVersion>1.29.586.5771</ApplicationVersion>
And I want to change it to this:
<ApplicationVersion>1.31.633.6832</ApplicationVersion>
Of course, the numbers may be something else. Is there a nice way of doing this in batch without changing anything else in the file?
Install the Find And Replace Text command line utility and then you can simply enter..
fart yourfile.txt 1.29.586.5771 1.31.633.6832
This works for you and will also preserve the empty lines in your input file.....
#Echo OFF
REM Set These Variables
SET "InFile=InputFile.txt"
SET "OutFile=Outputfile.txt"
SET "Replace=1.29.586.5771"
SET "ReplaceWith=1.31.633.6832"
REM Get Total Lines Number [including empty lines]
FOR /F %%A IN ('TYPE "%InFile%"^|find /v /c ""') DO SET "Till=%%A"
REM Create The OutputFile with changes
SETLOCAL EnableDelayedExpansion
<"!InFile!" (
FOR /L %%a IN (1 1 0) DO SET /p "="
FOR /L %%A IN (1 1 %Till%) DO (
SET "line="
SET /P "line="
IF "!line!x" == "x" ( Echo.
) ELSE ( Echo !line:%Replace%=%ReplaceWith%!)
)
)>>"%OutFile%"
ENDLOCAL