I have a htm file with href="example.com/page" somewhere on its source code, how could i get the link between the " "?
So far I have tried modifying this piece of code:
#echo off
setlocal EnableDelayedExpansion
set "str="
set "string=stuff href="example.com/page"end morestuff"
set string=!string:href=^
!
set string=!string:end=^
!
FOR /F skip^=1eol^= %%S in ("!string!") do if NOT DEFINED str set "str=%%S"
echo(!str!
pause > nul
However on line 6 it appears that changing href to href=" breaks the code, and changing end to " also breaks something, would like to know if it is possible to fix this or if there is an alternative for this?
I believe you just want the example.com/page part:
#echo off
set "string=stuff href="example.com/page"end morestuff"
for /f tokens^=2delims^="" %%a in ("%string%") do set "substr=%%a"
echo %substr%
Reading from file as per comment:
#echo off
set "file=file.txt"
for /f tokens^=2delims^="" %%a in ('findstr /IRC:"href=" "%file%"') do set "substr=%%a"
echo %substr%
Seeing that you require reading from an html file, I would recommend using something a little more robust, like powershell.
Create a file with .ps1 extension, paste the content, make sure you put the path and filename of you file in $file_path replacing file.txt:
$file_path = 'file.txt'
$rgx = '(?<=href\=").*?(?=">)'
select-string -Path $file_path -Pattern $rgx -AllMatches | % { $_.Matches } | % { $_.Value }
now you can either run it from cmd:
powershell -File test_url.ps1
Or simply open powershell cli and run directly from there:
.\test_url.ps1
Related
I'm using windows command prompt (cmd).
I have this file "myfile.txt" and i want to get the string in each line that match my pattern.
For instance, if i have this in "myfile.txt":
The file alpha_123.txt has been created
The file gama_234.txt has been created
The new file alpha_789.txt has been created
And if I search for "alpha", the result should be this:
alpha_123.txt
alpha_789.txt
For now i have this code:
findstr /c "alpha" myfile.txt but it returns the full line, but i just want the specific string.
Thanks in advance!
This can be used at a cmd command-prompt on windows.
powershell -nop -c ((sls 'alpha.*?\s' myfile.txt).Matches.Value).Trim()
If placed into a cmd batch-file, aliases should not be used.
powershell -NoLogo -NoProfile -Command ^
((Select-String -Pattern 'alpha.*?\s' -Path 'myfile.txt').Matches.Value).Trim()
Assuming you're in the correct directory, something like this should work.
for /f "tokens=3 delims= " %G in ('findstr "alpha" MyFile.txt') do #echo %G
Or something like this:
for /f "tokens=3 delims= " %G in ('findstr /r "\<alpha_.*txt" MyFile.txt') do #echo %G
Quite straight forward: get each line, write each word (token) and filter for the desired string:
#echo off
setlocal
set "search=alpha"
for /f "delims=" %%a in (t.txt) do (
for %%b in (%%a) do (
echo %%b|find "%search%"
)
)
I am trying to find a particular line from an XML file and extract the value using string manipulation operations.
Below is the code I am trying.
#echo off
setlocal enabledelayedexpansion
::Expected line is "<filename>c:\temp\file1</filename>"
for /f "tokens=*" %%i in ('findstr /i "filename" file1.props') do (
SET LINE=%%i
)
echo !LINE!
SET FILENAME=!LINE:<filename>=!
SET FILENAME=%FILENAME:</filename>=%
ECHO !FILENAME!
And the output is:
<filename>c:\temp\file1</filename>
The system cannot find the file specified.
ECHO is off.
I actually want this value c:\temp\file1
Someone please help me correct the code or please suggest any other simpler way.
The problem is the execution of set. The parser interprets the > and < as redirection, so it will fail with a syntax error. Use quotes to process it as intended (`set "var=value"):
#echo off
setlocal enabledelayedexpansion
REM echo ^<filename^>c:\temp\file1^</filename^>>file1.props
::Expected line is "<filename>c:\temp\file1</filename>"
for /f "tokens=*" %%i in ('findstr /i "filename" file1.props') do (
SET "LINE=%%i"
)
echo !LINE!
SET "FILENAME=!LINE:<filename>=!"
SET "FILENAME=%FILENAME:</filename>=%"
ECHO !FILENAME!
Output is:
<filename>c:\temp\file1</filename>
c:\temp\file1
The last word in every texts in testing.txt is "< /a>", I echo out the the word and it seems to be no problem, but when I echo out in the for loop, cmd gave me this error : "The system cannot find the file specified."
I know the problem is on the "<" and ">" sign, it stands for redirection, that's how the error created. How am I going to make cmd think I'm working with a string instead of redirection?
#echo off
setlocal EnableDelayedExpansion
set "remove_char=< /a>"
echo !remove_char!
for /f "skip=2 tokens=6*" %%a in (testing.txt) do (
set "string=%%a %%b"
set string=!string:%remove_char%=!
echo !string!
)
pause >nul
If you want to use < and > symbols as variables you have to change them to ^< or ^>
Otherwise they will be treated as Input or Output!
Maybe you can replace this line
set string=!string:%remove_char%=!
with
for %%i in (!remove_char!) do (set string=!string:%%i=!)
I am trying a batch file which replaces a changing text named X by a static text 'English' into the path
Y:\SUT\ISTQB\Board\X\Dashboard\Open
About the position of the X text, it is always surrounded with 'Board' and 'Dashboard'.
In the path below, the current below text for the given path is 'X=Language' and my goal is finally to get 'X=English' each time of course!
Y:\SUT\ISTQB\Board\Language\Dashboard\Open
The string of replacement is read from a file. This file only contains 'English'
Is that possible to find a solution through a pipe with the 'find' command?
Thank you in advance
#ECHO OFF
SETLOCAL
SET mypath=Y:\SUT\ISTQB\Board\X\Dashboard\Open
FOR /f %%i IN (languagefile.txt) DO SET language=%%i
SET mypath=%mypath:\Dashboard\=*%
SET mypath=%mypath:\Board\=*%
FOR /f "tokens=1,3delims=*" %%i IN ("%mypath%") DO SET mypath=%%i\Board\%language%\Dashboard\%%j
ECHO %mypath%
"*" chosen because it cannot occur in a pathname.
Try this:
#echo off &setlocal enabledelayedexpansion
set "string=Y:\SUT\ISTQB\Board\Language\Dashboard\Open"
set "search=%string:*\Board\=%"
for /f "delims=\" %%i in ("%search%") do set "search=%%i"
set "string=!string:%search%=English!"
echo %string%
I am trying to insert a certain string with spaces "sBody = " before each line in a text document. I found this answer in other forums, but it does not work? A few questions I have about this script are, should "myfile" be the full filepath? Also, this script was originally made to insert a string at the end of the file, what do I need to take out to make it insert just one string at the beginning?
Thank you for all of your answers!
#echo off > newfile & setLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in (myfile) do (
echo STRING %%a STRING >> newfile
)
The script is creating a new file named 'newfile' reading in the original file 'myfile'. Once it's done you can delete 'myfile' and rename 'newfile' to 'myfile' if you like. This example replaces each <line> in the file with STRING <line> STRING.
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in (myfile) do (
echo STRING %%a STRING >> newfile
)
sed 's/.*/pattern &/' yourfile.txt -i
works for me, in a single command line