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\"
Related
I have a text.txt file that is filled with data that is not separated by a delimiter. I need to add a delimiter based on columns in the text file.
The data currently looks like
00067800000000000000000000N00006N 00000125463150050000012546315012
00067800000000000000000000N00006N 00000125463150810000012546315098
and I need to be add a delimiter such as "," at specific columns in the text so it comes out looking like.
000678,0000000,000000,0000000,N,00006,N, ,00000,12546315005,00000,12546,315012
000678,0000000,000000,0000000,N,00006,N, ,00000,12546315081,00000,12546,315098
I would like to create a batch file that goes through and adds the delimiter , at each column I need it for every line in the file.
Is this possible?
Thanks in advance!
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q39666479.txt"
SET "outfile=%destdir%\outfile.txt"
:: set delimiter and read column-sizes from supplied command-line
SET "delimiter=,"
CALL :colsize %*
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
SET "line=%%a"
CALL :report
)
)>"%outfile%"
GOTO :EOF
:colsize
SET "magic="
:: offset position
SET /a size=0
:colsizelp
IF DEFINED magic (
SET "magic=%magic%%delimiter%`line:~%size%,%1`"
) ELSE (
SET "magic=`line:~%size%,%1`"
)
SET /a size +=%1
SHIFT
IF "%1" neq "" GOTO colsizelp
SETLOCAL enabledelayedexpansion
SET "magic=!magic:`=%%!"
endlocal&SET "magic=set "line=%magic%""
GOTO :eof
:report
CALL %magic%
ECHO %line%
GOTO :eof
You would need to change the settings of sourcedir, filename1 and destdir to suit your circumstances.
I used a file named q39666479.txt containing your data for my testing.
Produces the file defined as %outfile%
Run as thisbatchname 6,7,6,7,1,5,1,1,5,11,5,5,6
to produce the above result. Each number is simply the column-width.
Essentially, build the string magic as appropriate substring commands strung together to assemble the output line as required. With each line from the file, execute the magic command and echo the result.
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%
In one text file I have this lines:
2014-10-09 00:00:00.000;1663;2014-10-09 12:38:46.000;A;"
";0;0;E/272
2014-10-09 00:00:00.000;1663;2014-10-09 12:38:46.000;A;"
";0;0;D/275
I need using a DOS batch file this new output:
2014-10-09 00:00:00.000;1663;2014-10-09 12:38:46.000;A;"";0;0;E/272
2014-10-09 00:00:00.000;1663;2014-10-09 12:38:46.000;A;"";0;0;D/275
And I have tried this solution:
for /f "delims=" %%a in (oldfile.txt) do (
echo/|set /p ="%%a%"
)>>newfile.txt
But the output is wrong, I don't have two lines in txt newfile but only one line:
2014-10-09 00:00:00.000;1663;2014-10-09 12:38:46.000;A;"";0;0;E/2722014-10-09 00:00:00.000;1663;2014-10-09 12:38:46.000;A;"";0;0;D/275
What am I missing?
What's wrong with this code?
Thank you in advance.
New lines in double quoted values are absolutely valid in CSV files.
Why not using a text editor which supports Perl regular expression replaces?
For example running a Perl regular expression replace using as search string (?<=")\s+(?=";) and an empty string as replace string would do the job.
There are console applications written for replacing strings in files. See
How can you find and replace text in a file using the Windows command-line environment?
But if you want to do this with a batch file for some unknown reason, take a look on batch code below working for your example.
#echo on
setlocal EnableDelayedExpansion
set "INTEXTFILE=oldfile.txt"
if not exist "%INTEXTFILE%" goto EndBatch
set "OUTTEXTFILE=newfile.txt"
if exist "%OUTTEXTFILE%" del "%OUTTEXTFILE%"
set "FirstLine="
set "EndFirstLine=""
set "BeginSecondLine= ";"
for /f "usebackq delims=" %%A in ("%INTEXTFILE%") do (
set "Line=%%A"
if not "!FirstLine!"=="" (
if "!Line:~0,22!"=="!BeginSecondLine!" (
set "FirstLine=!FirstLine!!Line:~20!"
echo !FirstLine!>>"%OUTTEXTFILE%"
set "Line="
) else (
echo !FirstLine!>>"%OUTTEXTFILE%"
)
set "FirstLine="
)
if not "!Line!"=="" (
if "!Line:~-1!"=="!EndFirstLine!" (
set "FirstLine=!Line!"
) else (
echo !Line!>>"%OUTTEXTFILE%"
)
)
)
:EndBatch
endlocal
Empty lines are removed by this batch file, too. This cannot be avoided as command for ignores empty lines on parsing the file.
Empty lines may get ignored, but can be re appended by using echo.
for /f "usebackq delims=" %%A in ("%INTEXTFILE%") do (
set "Line=%%A"
if not "!FirstLine!"=="" (
if "!Line:~0,22!"=="!BeginSecondLine!" (
set "FirstLine=!FirstLine!!Line:~20!"
echo !FirstLine!>>"%OUTTEXTFILE%"
echo. >>"%OUTTEXTFILE%"
set "Line="
) else (
echo !FirstLine!>>"%OUTTEXTFILE%"
echo. >>"%OUTTEXTFILE%"
set "FirstLine="
)
)
if no t "!Line!"=="" (
if "!Line:~-1!"=="!EndFirstLine!" (
set "FirstLine=!Line!"
) else (
echo !Line!>>"%OUTTEXTFILE%"
echo. >>"%OUTTEXTFILE%"
)
)
)
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 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