Home > OS >  Batch Script To Fetch Website and Parse String
Batch Script To Fetch Website and Parse String

Time:12-25

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"  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?.

CodePudding user response:

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

CodePudding user response:

@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)

CodePudding user response:

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
  • Related