I generally use the echo method for writing "text" to an HTML-file by using:
echo. >> "C:\Users\Me\Desktop\test.html"
echo ^<div^>TestDIV^</div^> >> "C:\Users\Me\Desktop\test.html"
I've experienced, that echo always writes the additional lines to the end of the target file.
But what if my target file (test.html) already has a structure like:
<div id="wrapperDIV">
<div id="DIVcontent1">DIV 1</div>
<div id="DIVcontent2">DIV 2</div>
</div>
and I want to write the additional lines within this existing structure, for example right after:
<div id="DIVcontent1">DIV 1</div>
I have tried to set up a script for this, but so far I couldn't make it running. (It seems to fail in the for-loop)
set DIVInput=^<div^>TestDIV^</div^>
set inputfile=C:\Users\Me\Desktop\test.html
(for /f usebackq^ delims^=^ eol^= %%a in ("%inputfile%") do (
if "%%~a"=="DIV 1^</div^>" call echo %DIVInput%
echo %%a
))>>"%inputfile%"
pause
Your set DIVInput=... consumes the escaping carets, which is bad in this case, because you need it later to echo the variable correctly. The preferred syntax set "var=value" preserves them (they are safe inside the quoting).
With your if line, you want to check for a substring only, not the whole line (if always compares complete strings and doesn't support wildcards). I used echo fullstring|find "string" to check that. Note that I use quotes (echo "%%~a"') for the same reason as above: proper handling of poison chars like>and<Same reason for not escaping with thefind` command ("save inside quotes")
You redirect to your input file. Redirecting is the first thing for the parser, so the file gets overwritten even before for has a chance to read the file. Use a different file and overwrite your original file when done.
You want to insert a string after a certain trigger line, but your code inserts before that line (well, that's easy to fix).
#echo off
setlocal
set "DIVInput=^<div^>TestDIV^</div^>"
set inputfile=test.html
(for /f usebackq^ delims^=^ eol^= %%a in ("%inputfile%") do (
echo %%a
echo "%%~a" |find "DIV 1</div>" >nul && echo %DIVInput%
))>"%inputfile%.new"
REM move /y "%inputfile%.new" "%inputfile%
Note: I disabled the move command for security reasons. Remove the REM after checking test.html.new looks ok.
Related
I tried searching but couldn't find anything specific to what I need.
So I want to fetch, maybe use curl for Windows, the guid string generated by this website without having to save the html file first. The sources are more or less like this:
<input name="YourGuidLabel" type="text" id="YourGuidLabel" onclick="this.focus(); this.select();" readonly="readonly" class="guidinput" value="852dd74c-4249-4390-85d3-6e9e2116ef2b" /></p>
What I want is this one: 852dd74c-4249-4390-85d3-6e9e2116ef2b. The string is then stored into a variable and echoed to view it.
In linux terminal I can do it in this simple way:
curl -s "https://www.guidgen.com/" | grep -o 'me="YourGuid.*value=.*/>' | cut -d '"' -f14
Does this thing by being able to use a batch file?.
This can do the trick with a batch file on Windows using a PowerShell Command and set it as variable with for /f .. do loop :
#echo off
Title Extract GUID Value from Input Field from site https://www.guidgen.com
#For /f %%a in ('Powershell -C "$(IWR https://www.guidgen.com -UseBasicParsing).InputFields.value"') do Set "GUID=%%a"
Echo GUID=%GUID%
pause
#ECHO OFF
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q74909468.txt"
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO SET "html=%%e"
SET "html=%html:"=%"
SET "html=%html:<=%"
SET "html=%html:>=%"
SET "html=%html:)=%"
SET "html=%html:(=%"
SET "html=%html:;=%"
FOR %%e IN (%html%) DO if "%%e" neq "//p" SET "guid=%%e"
ECHO GUID=%guid%
GOTO :EOF
Always verify against a test directory before applying to real data.
Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around %filename1% can be omitted.
You haven't told us where the html is located - I've presumed a file.
Sadly "more or less like" is not specific enough to generate a reliable solution.
Read the file line to a variable, html
Remove all " < > ) ( ; from that variable.
process the result, assigning each token in turn to guid, unless the token is //p
Assumes the required string is that string which precedes //p which is the last string in the (original text - deleted character set)
The following idea not using PowerShell may also perform the task you've laid out in your question.
#Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "value=" & For /F Delims^=^ EOL^= %%G In ('%SystemRoot%\System32\curl.exe -s "https://www.guidgen.com" ^| %SystemRoot%\System32\findstr.exe /RIC:" value=\"[0123456789abcdef][0123456789abcdef]*-[0123456789abcdef][0123456789abcdef]*-[0123456789abcdef][0123456789abcdef]*-[0123456789abcdef][0123456789abcdef]*-[0123456789abcdef][0123456789abcdef]*\""') Do (Set "value=%%G" & SetLocal EnableDelayedExpansion & For /F Delims^=^"^= %%H In ("!value:* value=!") Do EndLocal & Set "value=%%H")
If Defined value Echo %value% & Pause
I need batch commnad to findstr and retrieve text to the end of text file.
text file include
# failure detail
1. AssertionError Response time is less than 100ms
expected false to be truthy
at assertion:1 in test-script
inside "epos-agentDetailsInfo"
2. AssertionError Response time is less than 100ms
expected false to be truthy
at assertion:1 in test-script
inside "epos-getccounts"
I need to find "AssertionError" and retrieve text to the end of text file.
I expect to show since "AssertionError" untill the end of file.
I try this.
#echo off
setlocal
for /F "tokens=* delims=" %%a in ('findstr /I "AssertionError" test1.log') do set "uniuser=%%a"
echo User is: %uniuser%
endlocal
and it 's show only " 2. AssertionError Response time is less than 100ms"
I believe that this is what you need:
It's not clear whether the empty lines in your data are actually empty, or contain one, two, or more spaces. The code caters for any pf these situations.
#ECHO OFF
SETLOCAL
rem The following settings for the source directory and filenames are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q74454843.txt"
SET "outfile=%sourcedir%\q74454843.rpt"
SET "repro="
(
FOR /f "tokens=1*delims=]" %%b IN ('find /n /v "" ^<"%filename1%"') DO (
SET "mtline=%%c"
IF DEFINED mtline CALL SET "mtline=%%mtline: =%%"
IF DEFINED mtline (
IF DEFINED repro (
ECHO %%c
) ELSE (
ECHO %%c|FINDSTR /L /C:"AssertionError" >NUL
IF NOT ERRORLEVEL 1 (SET "repro=Y"&ECHO %%c)
)
) ELSE (
IF DEFINED repro ECHO.
SET "repro="
)
)
)>"%outfile%"
GOTO :EOF
repro is a flag to determine whether to reproduce the line or not.
Each line is numbered with a leading [linenumber] by the find so the part before the ] is assigned to %%b and the actual line contents to %%c.
mtline is then used as substringing metavariables (like %%c) is not supported; it receives a copy of %%c and if this is not empty, removes any spaces.
if mtline is then empty, batch interprets it as undefined.
if mtline is defined, the code then determines whether repro is defined and echoes %%c if it is defined, otherwise it asks findstr whether the line contains the target string; if it does, then set repro to a value (making it defined and echo the line.
if mtline is not defined, then we have reached the end of the section, so echo an empty line and clear repro so no further lines are reproduced.
The ^< is an escaped redirector which tells cmd that the redirector is part of the find command, not the for.
The entire for command is enclosed in parentheses so that the echoes it executes can be redirected to a file.
I'm trying to read the output of a command (which outputs into multiple lines), and use an arbitrary number of those lines. Because I know neither the number of total lines, nor the number of lines that will be used, I need to analyse and possibly use each line in a loop, which is why I have setlocal enabledelayedexpansion.
Below is a snippet of the code that shows the process of taking the command and reading each line (not using it yet, just reading it to make sure this works (which it doesn't)):
#echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in ('svn status') do (
echo %%i
set file=%%i
echo *!file!
)
The problem that I'm running into is that the %%i values that are being read in are not correct in the for line. The first character is missing from the first line of the input (which is important because I use the first line to decide whether or not to use that line).
The output I get from my code looks like this:
Dir0\TestDoc7.txt
? StatusFile.txt
Whereas if I run this code:
copy /y NUL StatusFile.txt >NUL
>StatusFile.txt (
svn status
)
(Which is just another way of me seeing what the real output of svn status is) I get a proper output into the text file:
! Dir0\TestDoc7.txt
? StatusFile.txt
I'm probably making a fairly clear mistake as I'm rather new to batch scripting.
Thanks in advance.
The cause is EnableDelayedExpansion which will eat the exclamation marks,
Your choice of tokens=* will also strip all leading spaces from the lines.
#echo off
for /f "tokens=1*" %%A in ('svn status') do (
if "%%A" equ "!" (
Rem do whatever
) else If "%%A" equ "?" (
Rem do something else rest of the line is in %%B
) else (
Rem no ! or ? first space sep. content is in %%A rest of the line is in %%B
)
)
There are more than 10 html files with image tags. Every time we deploy our build onto test site we need to change the img source. for eg <img src=/live/Content/xyz.png />
to <img src=/test/Content/xyz.png />.
After looking around and reading for sometime, i have come up with the following batch script, however i cant figure out how do i go further from here :
for /r %%i in (*.html) do echo %%i
for %%f in (*.html) do (
FOR /F %%L IN (%%f) DO (
SET "line=%%L"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "x= <--------------------WHAT DO I SET HERE?
echo %x%
ENDLOCAL )) pause
This is my first batch script, could anyone please guide me in the right direction?
#ECHO OFF
SETLOCAL enabledelayedexpansion
for /r U:\ %%i in (*.html) do (
echo found %%i
SET outfile="%%~dpni.lmth"
(
SETLOCAL disabledelayedexpansion
FOR /F "usebackq delims=" %%L IN ("%%i") DO (
SET "line=%%L"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "line=!line:/live/=/test/!
echo !line!
ENDLOCAL
)
ENDLOCAL
)>!outfile!
)
pause
GOTO :EOF
How about this development?
Notes:
I've modified your FOR/R to ECHO the HTML file being processed and use %%i rather than switching to %%f. U: is my RAMDRIVE; you'd need to modify that to suit.
outfile is set to generate a filename which matches the HTML filename, but with a .lmth extension (can't update in-place) - it gets that from the ~dpn prefixing the i, which means the drive, path and name of the file %%i. It's quoted to take care of potential spaces in the filename or pathname.
The next logical statement is (for /f...[lines] )>!outfile! which sends any echoed text to a NEW file !outfile!. The enabledelayedexpansion in the second physical line of the batch makes !outfile! the RUN-TIME value - as it is changed within the FOR r outer loop.
Since the actual HTML filename in %%i may contain spaces, it needs to be quoted, hence the 'usebackq' clause in the FOR/F. The delims= clause ensures that the ENTIRE line from the file "%%i" is applied to %%L - not just the first token (well, actually, makes the entire line the first token).
The SET command substitutes the string "/test/" for any occurrence of "/live/" in the RUN-TIME value of the variable lineand assigns the result to line. The resultant value is then ECHOd - which is redirected to outfile
Note that in your original, you would be assigning x in the set x= but echo %x% would have reproduced x as it stood when the line was PARSED because batch substitutes the value of any variable for %var% as part of the parsing phase. Consequently, the line would have become simply ECHO (since x would likely be unassigned) and bizarrely would have reported the echo state (Echo is OFF)
A couple of gatchas here. First, % and some other characters are notoriously hard to process with batch, so be careful. Next, FOR/F will bypass empty lines. This can be overcome if required. Third, this will replace ANY occurrence of /live/ in any case with /test/
Good luck!
Edit to support exclamation marks: 20130711T0624Z
Added SETLOCAL enabledelayedexpansion line and ENDLOCAL just before )>!outfile! to match
I have a script in which I read html files which I want to edit. Here I paste the code which calls :remove_redundant_columns subroutine.
It should remove the spaces/white spaces from begin of each line and remove from html file. Only problem is that it adds extra text like = to lines which are almost empty, just have few tabs.
The html file which I downloaded is from hidemyass.com/proxy-list/1
call parse_proxy.bat remove_redundant_columns !FILENAME!
exit /b
:remove_redundant_columns
REM Remove whitespaces from begin of lines and <span></span>
FOR /f "tokens=*" %%t in (%1) do (
SET S=%%t
SET S=!S:^<span^>^</span^>=!
if NOT "!S!"=="" >>$tmp$ echo !S!
)
del %1
REN $tmp$ %1
exit /b
If you believe, that's your only problem... You need to check, if your variable S contains content.
That's required, as substitution on an undefined variable will not produce an undefined/empty variable, the new content will be the substitution text.
:remove_redundant_columns
REM Remove whitespaces from begin of lines and <span></span>
FOR /f "tokens=*" %%t in (%1) do (
SET S=%%t
if defined S (
SET S=!S:^<span^>^</span^>=!
>>$tmp$ echo !S!
)
)
As dbenham stated, you got many other problems,
and one additional problem is the echo !S! command itself.
ECHO has some nasty side effects on different content.
If the content is empty (or only spaces) then it will print it's currently state
ECHO IS OFF
If the content is OFF or ON it will NOT be echoed, it will only change the state.
And if the content is /? it will echo the help instead of /?.
To solve this you could simply change ECHO !S! to ECHO(!S! and all problems are gone.
jeb already solved your = problem (once the extra IF DEFINED check is added to his answer). But you may have at least one other problem.
I agree with Joey that you should not be using batch to manipulate HTML like this. But, if you really want to...
Your potential problem is that HTML usually has ! characters sprinkled within. Your code uses delayed expansion, but that causes corruption of FOR variable expansion when it contains ! character(s). The solution is to toggle delayed expansion on and off within your loop.
:remove_redundant_columns
setlocal disableDelayedExpansion
REM Remove whitespaces from begin of lines and <span></span>
(
FOR /f "usebackq eol= tokens=*" %%t in ("%~1") do (
SET S=%%t
setlocal enableDelayedExpansion
if defined S SET "S=!S:<span></span>=!"
for /f "eol= tokens=*" %%S in ("!S!") do if "%%S" neq "" echo %%S
endlocal
)
) >>$tmp$
move /y $tmp$ "%~1"
exit /b
Other minor changes that were made to the code:
The search and replace can be simplified by using quotes so that special chars don't need to be escaped.
You can replace DEL and REN with a single MOVE.
Redirection is more efficient (faster) if you redirect once using an outer set of parentheses
You may need to search a file name that has spaces and or special characters, in which case you will need to quote the name. But that requires the FOR /F "USEBACKQ" option.
EDIT
Modified code to strip leading spaces after <span></span> has been replaced to eliminate potential of a line containing nothing but spaces and/or tabs.
Also set EOL to space to prevent stripping of lines beginning with ;