I'm working on a DOS script in order to search a line and replace it into files from a specific folder. Here is what i have already done :
#echo off
setlocal enableextensions disabledelayedexpansion
set "search=#Interceptors({ RuntimeExceptionInterceptor.class }^)"
set "replace=#Interceptors({ RuntimeExceptionInterceptor.class, ReportInterceptor.class, CorrelationIdInterceptor }^)"
set "textFile=C:\Utilisateurs\a669884\Documents\test.txt"
for %%a in (*.txt) do (
echo "%%a"
for /f "Delims=" %%i in (%%a ^& break ^> %%a) do (
set "line=%%i "
setlocal enabledelayedexpansion
>>%%a echo(!line:%search%=%replace%!
endlocal
)
)
Problem is that my first line is not replace by the new one, the new line is added under it like that :
#Interceptors({ RuntimeExceptionInterceptor.class })
#Interceptors({ RuntimeExceptionInterceptor.class, ReportInterceptor.class, CorrelationIdInterceptor })
Do you know why my first line isn't replaced? Thanks a lot
Seb
Not sure what setting textfile has to do with the routine - it's not used.
Your original code produced a complaint that & was not found and created an empty file for me.
Changing the for to
for /f "Delims=" %%i in ('type "%%a" ^& break ^> "%%a"') do (
seemed to make the substitution, given the one line (partial?) of sourcefile you've provided, plus a few dummy lines.
execute the type... with the filenames enclosed in quotes to ensure correct operation with separators, replacing the file; substitute as specified.
Related
I've written a batch file to modify the attribute of an XML file. The script works and the attribute if modified, however I'm having an issue with escaping some characters.
I've tried every solution I can possibly find online with no luck.
My intended output is:
<xs:import schemaLocation="2e9dd7db-f58b-4c91-8575-3b3af05d3178.xsd" namespace="urn:verastar:veracore:types" />
However I'm getting:
<xs:import schemaLocation="="2e9dd7db-f58b-4c91-8575-3b3af05d3178.xsd"" namespace="urn:verastar:veracore:types
I've tried escaping the quotes with ^, this works when using echo to the console however doesn;t work when writing to the file.
Why am I getting ="=" also I'm getting double quotes, but when I remove one nothing gets written to the file.. and finally the last quote seems to be messing things up and therefore the output is missing the XML closing tag.
How can I escape these characters properly?
My code is:
#echo on
setlocal EnableExtensions DisableDelayedExpansion
rem check if the XSD to modify exists in the batch directory
set "XSDFile=%~dp0test.xsd"
if not exist "%XSDFile%" goto EndBatch
rem environment variables
set "LineNumber="
set "LineCount=0"
set "TmpFile=%TEMP%\%~n0.tmp"
rem Search for the line containing attribute schemaLocation and get its
rem line number and the line itself loaded into environment variables
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /L /N /C:schemaLocation= "%XSDFile%" 2^>nul') do (
set "LineNumber=%%I"
set "FileLine=%%J"
)
rem If no line with attribute schemaLocation found, exit this batch file
if not defined LineNumber goto EndBatch
setlocal EnableDelayedExpansion
set "FileName=!FileLine:*schemaLocation=!"
for /f "tokens=1 delims=?" %%a in ("%FileName%") do (set test=%%a)
set "test=<xs:import schemaLocation="%test%.xsd"" namespace="urn:verastar:veracore:types" />"
pause
endlocal & set "FileLine=%test%
rem Make sure the temporary file used next does not already exist.
del "%TmpFile%" 2>nul
rem Copy all lines from XML file to a temporary file including empty
rem lines with the exception of the line containing attribute schemaLocation
rem which is copied to temporary file with the modified schemaLocation.
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /R /N "^" "%XSDFile%" 2^>nul') do (
set "XmlLine=%%J"
set /A LineCount+=1
setlocal EnableDelayedExpansion
if not !LineCount! == %LineNumber% (
echo/!XmlLine!>>"%TmpFile%"
) else (
echo/!FileLine!>>"%TmpFile%"
)
endlocal
)
rem Overwrite original file with temporary file automatically deleted on success.
move /Y "%TmpFile%" "%XSDFile%" >nul
:EndBatch
endlocal
I need a batch script that will read in another batch script (batch2) and:
look for the string "configout:" OR "configin:"
If it encounters one of these two strings, extract what's after it until the string ".xml"
copy paste it in a new text file.
and do this for each line of batch2.
For exemple:
If this is my first line in the batch script
/configin:%faxml%fm_sellin_in.xml /configout:%faxml%transco_fm_sellin_out%col_transco%.xml /inputfile:
I should have this in my text file:
%faxml%fa_sellin_in.xml
%faxml%transco_fm_sellin_out%col_transco%.xml
I have seen a good code in Here:
for /f "tokens=1-2 delims=~" %%b in ("yourfile.txt") do (
echo %%b >> newfile.txt
echo removed %%a)
but i don't know how to adapt it to my specific case.
Why not replace all the /configin and /configout with newlines? -
(Replace string with a new line in Batch)
For example
setlocal EnableDelayedExpansion
set "str=/configin:%%faxml%%fm_sellin_in.xml /configout:%%faxml%%transco_fm_sellin_out%%col_transco%%.xml /inputfile:"
set str=!str:/configin^:=^
!
set str=!str:/configout^:=^
!
Now, !str! would contain
fm_sellin_in.xml
transco_fm_sellin_out.xml /inputfile:
Then, you could use the for loop to extract the strings
for /f "tokens=1,2 delims=. " %%a in ("!str!") do ()
this for loop iterates through each line and splits each line with the and . characters.
So %%a is your file name and %%b is the extension.
then
if [%%b]==[xml] (echo %%a.%%b>>mytextfile.txt)
We will do this for all the lines of batch2.
And the finished code is
setlocal EnableDelayedExpansion
for /f "delims=" %%c in (batch2.txt) do (
set str=%%c
set str=!str:/configin^:=^
!
set str=!str:/configout^:=^
!
for /f "tokens=1,2 delims=. " %%a in ("!str!") do (if [%%b]==[xml] (echo %%a.%%b>>mytextfile.txt))
)
I want to be able to replace a line in a properties file but i only know part of the line string at any one time
Heres the line i want to replace: mb.datasource.password=ENC(8dF45fdD)
with this: mb.datasource.password=apassword
What i have just now is this
#echo off &setlocal
set "search=mb.datasource.password="
set "replace=mb.datasource.password=apassword"
set "textfile=mb.properties"
set "newfile=mb-new.properties"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
This ends up giving me mb.datasource.password=apassword=ENC(8fFdeUdK)
I can't just find the full string it needs to only be mb.datasource.password= because the part after the equals changes
Any help would be greatly appreciated?
You can do it with batch. I put together a quick script and it worked for me:
#ECHO OFF
SETLOCAL EnableExtensions
SET SourceFile="mb.properties"
SET OutputFile="mb-new.properties"
SET "FindKey=mb.datasource.password"
SET "NewValue=apassword"
REM Basic parse for INI file format.
(FOR /F "usebackq eol= tokens=1,* delims==" %%A IN (`TYPE %SourceFile%`) DO (
REM If the key value matches, build the line with the new value.
REM Otherwise write out the existing value.
IF /I "%%A"=="%FindKey%" (
ECHO %%A=%NewValue%
) ELSE (
ECHO %%A=%%B
)
)) > %OutputFile%
REM Replace old with new.
DEL %SourceFile%
MOVE %OutputFile% %SourceFile%
ENDLOCAL
A few notes about the above:
I assumed basic INI file format.
The FOR loop will skip blank lines so they would be removed from the new output file. There are ways around this using tricks with the FIND command if the blanks are needed.
If you have special chars (% or ! especially) - this may cause some problems, but if you have just "normal" text then it should be fine.
I am writing a .bat program that will find and replace text in a file. The problem that I am having is that it is removing blank lines and left justifying the other lines. I need the blank lines to remain and the new text to remain in the same location. Here is what I have wrote, and also the result. Can anybody please help.
program:
#ECHO OFF
cls
cd\
c:
setLocal EnableDelayedExpansion
For /f "tokens=* delims= " %%a in (samplefile.tx) do (
Set str=%%a
set str=!str:day=night!
set str=!str:winter=summer!
echo !str!>>samplefile2.txt)
ENDLOCAL
cls
exit
samle File:
this line is the first line in my file that I am using as an example.This is made up text
the cat in the hat
day
winter
below is the result:
this line is the first line in my file that I am using as an example.This is made up text
the cat in the hat
night
summer
I need the lines, spaces and new text to remain in the same position while making the text replacement. Please help
Your use of "tokens=* delims= " will trim leading spaces. Instead, use "delims=" to preserve leading spaces.
FOR /F always skips empty lines. The trick is to insert something before each line. Typically FIND or FINDSTR is used to insert the line number at the front of each line.
You can use !var:*:=! to delete the the line number prefix from FINDSTR.
Use echo(!str! to prevent ECHO is off message when line is empty
It is more efficient (faster) to redirect only once.
#echo off
setlocal enableDelayedExpansion
>samplefile2.txt (
for /f "delims=" %%A in ('findstr /n "^" samplefile.txt') do (
set "str=%%A"
set "str=!str:*:=!"
set "str=!str:day=night!"
set "str=!str:winter=summer!"
echo(!str!
)
)
This still has a potential problem. It will corrupt lines that contain ! when %%A is expanded because of the delayed expansion. The trick is to toggle delayed expansion on and off within the loop.
#echo off
setlocal disableDelayedExpansion
>samplefile2.txt (
for /f "delims=" %%A in ('findstr /n "^" samplefile.txt') do (
set "str=%%A"
setlocal enableDelayedExpansion
set "str=!str:*:=!"
set "str=!str:day=night!"
set "str=!str:winter=summer!"
echo(!str!
endlocal
)
)
Or you could forget custom batch entirely and get a much simpler and faster solution using my JREPL.BAT utility that performs regular expression search and replace on text. There are options to specify multiple literal search/replace pairs.
jrepl "day winter" "night summer" /t " " /l /i /f sampleFile.txt /o sampleFile2.txt
I used the /I option to make the search case insensitive. But you can drop that option to make it case sensitive if you prefer. That cannot be done easily using pure batch.
#ECHO Off
SETLOCAL
(
FOR /f "tokens=1*delims=]" %%a IN ('find /n /v "" q27459813.txt') DO (
SET "line=%%b"
IF DEFINED line (CALL :subs) ELSE (ECHO()
)
)>newfile.txt
GOTO :EOF
:subs
SET "line=%line:day=night%"
SET "line=%line:winter=summer%"
ECHO(%line%
GOTO :eof
Thi should work for you. I used a file named q27459813.txt containing your data for my testing.
Produces newfile.txt
Will not work correctly if the datafile lines start ].
Revised to allow leading ]
#ECHO Off
SETLOCAL
(
FOR /f "delims=" %%a IN ('type q27459813.txt^|find /n /v "" ') DO (
SET "line=%%a"
CALL :subs
)
)>newfile.txt
GOTO :EOF
:subs
SET "line=%line:*]=%"
IF NOT DEFINED line ECHO(&GOTO :EOF
SET "line=%line:day=night%"
SET "line=%line:winter=summer%"
ECHO(%line%
GOTO :eof
#echo off
setlocal enableextensions disabledelayedexpansion
for /r %f in (xis_a*) do More +1 %~nxf >> No_header_%~nxf
set "search=:20:"
set "replace={:20:"
for /f "delims=" %%i in ('type (No_header_*.txt) ^& break ^> (No_header_*.txt) ') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>>No_header_*.txt echo(!line!
endlocal
)
am trying to skip the header line in a text file and replace :20: with {:20:. i have written and have achieved almost.. please try to help me am totally new to this
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
set "search=:20:"
set "replace={:20:"
for /r "%sourcedir%" %%f in (xis_a*) do (
REM DEL "%destdir%\No_header_%%~nf.txt" >NUL 2>nul
for /f "usebackqskip=1delims=" %%i in ("%%f") do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>>"%destdir%\No_header_%%~nf.txt" echo(!line!
ENDLOCAL
)
)
GOTO :EOF
This should solve the problem.
In your code, the initial setlocal is establishing the default condition, so I omitted it.
You have %f in your 'for /r - the % before the metavariable f needs to be doubled.
The more ceremony isn't required - for /f has a skip option which will allow the first n lines to be skipped.
So - all that is required is to get the list of files generated by the for...%%f... loop and with each, process each individual file, skipping the first line. Quoting the filename appearing in %%f is simply a safety-measure to allow for filenames containing separators, but this means that usebackq needs to be invoked to tell for /f that the list given is not a literal string (which it will assume for a "quoted string,") but a filename.
Then, since %%f is in-context for the %%i loop, you can select the target filename by using %%~nf. I'm not sure whether you want ~nf or ~nxf (adding a second .txt extension) so I elected to use ~n alone. Note that your use of * is doomed - that means "all files matching" - probably not quite what you want, and cmd will get very confused.
I've aded a remmed-out del command to allow the destination file to be deleted - just remove the rem if required, otherwise the data will be appended to any exiating file.
I use my u: drive for testing, and have left my source and destination names in place. No doubt you would need to change those to suit your system.