Results 1 to 19 of 19

Thread: sztext and wite new links

  1. #1

    Thread Starter
    Junior Member gucci's Avatar
    Join Date
    Jun 2008
    Posts
    26

    sztext and wite new links

    Hi,

    I need help writing a vbs script and wondered if anybody here could help me?

    I have a simple txt file which i need to exract the link from inside it. The contents of the file are as follows:
    ----------------------------------------------------------------------
    <head>
    <title>Display PRMSLmsl: GFS High Resolution Ensemble Forecast</title>
    </head>
    <body bgcolor="#ffffff">
    <h3 align="center">GFS High Resolution Ensemble Forecast gec00_00z.ctl</h3>
    <h3 align="center">PRMSLmsl 1000 00Z26aug2008</h3>
    <FORM NAME="form1" action="pdisp_gens.sh">
    <center>
    <img src="/tmp/ATEST21591.png" alt="plot">
    </center>
    <pre>
    image was generated
    </pre>
    <small><p>&nbsp&nbsp <p align=left>Problems with the datasets, contact: [email protected], [email protected]<br>
    pdisp version v0.9.6.7: [email protected],[email protected]
    </body>
    -----------------------------------------------------------------------

    I need to extract /tmp/ATEST21591.png and place it in a new text file with the following added upfront of the link i have extracted:

    http://nomad5.ncep.noaa.gov/tmp/ATEST21591.png

    any help would be really great - many thanks,

    Ian
    Last edited by mendhak; Aug 26th, 2008 at 01:39 PM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: sztext and wite new links

    I've modified your post to dummy-up the email addresses. I'm sure they'd not appreciate you posting email addresses here.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: sztext and wite new links

    You can use regular expressions with VBScript to determine what is a hyperlink in that text there.

    http://www.regular-expressions.info/vbscript.html

  4. #4

    Thread Starter
    Junior Member gucci's Avatar
    Join Date
    Jun 2008
    Posts
    26

    Re: sztext and wite new links

    Quote Originally Posted by mendhak
    I've modified your post to dummy-up the email addresses. I'm sure they'd not appreciate you posting email addresses here.
    Cheers for that!, was meaning to do that. Dont suposse you know a cure to my problem?

    Cheers,

    Ian

  5. #5

    Thread Starter
    Junior Member gucci's Avatar
    Join Date
    Jun 2008
    Posts
    26

    Re: sztext and wite new links

    Quote Originally Posted by mendhak
    You can use regular expressions with VBScript to determine what is a hyperlink in that text there.

    http://www.regular-expressions.info/vbscript.html

    Hi,

    I use it to grab images of necp server (weather server).

    I first use wget to get the the html page and generate the server to create me a image.

    example:
    http://nomad5.ncep.noaa.gov/cgi-bin/...=%2Fgens%DATE%

    This generates me the html, the image is stored for a brief time in a tmp dir, and i need to generate the full link so i can use wget to download me the image.

    Cheers,

    Ian

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: sztext and wite new links

    We posted at the same time. Scroll up to look at post #3 please.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: sztext and wite new links

    Quote Originally Posted by gucci
    Hi,

    I use it to grab images of necp server (weather server).

    I first use wget to get the the html page and generate the server to create me a image.

    example:
    http://nomad5.ncep.noaa.gov/cgi-bin/...=%2Fgens%DATE%

    This generates me the html, the image is stored for a brief time in a tmp dir, and i need to generate the full link so i can use wget to download me the image.

    Cheers,

    Ian
    Not sure I understand - are you using regex already to get the image URL out?

  8. #8

    Thread Starter
    Junior Member gucci's Avatar
    Join Date
    Jun 2008
    Posts
    26

    Re: sztext and wite new links

    Quote Originally Posted by mendhak
    Not sure I understand - are you using regex already to get the image URL out?
    No am not using any vbs yet, but because i could not get the wget program to do what i wanted in full because of wildcards and a couple of other things - The only option left was vbs.

    I am quite a novice at vbs, and have.nt really had a need to use it until now really. I can write simple things to a txt file, and i did today try a search and write to a text file, but all i manged to do was get the full html tag img src="/tmp/ATEST21591.png" alt="plot">, rather than just the /tmp/ATEST21591.png.

    If i could figure that out then, i should have cracked it.

  9. #9
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: sztext and wite new links

    If you are able to get the image tag then this should work
    Code:
    'Assumes
    'You already captured the img tag
    'The filename doesn't contain spaces
    Dim strTag
    Dim intPos
    
        strTag = "<img src='/tmp/ATEST21591.png' alt='plot'>"
    
        'find where the backslash is
        intPos = InStr(strTag, "/")
    
        'remove everything before the backslash
        strTag = Mid(strTag, intPos)
        'Will return
        '/tmp/ATEST21591.png' alt='plot'>
        
        'Now find the position of the space from the retrun
        intPos = InStr(strTag, " ")
    
        'Grab everything up to the space
        strTag = Left(strTag, intPos - 2)
        
        'use the return as you please
        MsgBox strTag

  10. #10

    Thread Starter
    Junior Member gucci's Avatar
    Join Date
    Jun 2008
    Posts
    26

    Re: sztext and wite new links

    Quote Originally Posted by MarkT
    If you are able to get the image tag then this should work
    Code:
    'Assumes
    'You already captured the img tag
    'The filename doesn't contain spaces
    Dim strTag
    Dim intPos
    
        strTag = "<img src='/tmp/ATEST21591.png' alt='plot'>"
    
        'find where the backslash is
        intPos = InStr(strTag, "/")
    
        'remove everything before the backslash
        strTag = Mid(strTag, intPos)
        'Will return
        '/tmp/ATEST21591.png' alt='plot'>
        
        'Now find the position of the space from the retrun
        intPos = InStr(strTag, " ")
    
        'Grab everything up to the space
        strTag = Left(strTag, intPos - 2)
        
        'use the return as you please
        MsgBox strTag
    Hi Mark,

    I could not get this to work - It might be beacuse the numbers change for each time i donwload a new file.

    I was thinking along the lines of a seach where it starts at ATEST and ends at .png and includes everything in the middle! but not really sure if this is possible. The script i am using to find the tag is below, not sure weather this could be modified.

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    strFind = "tmp"

    Set objInFile = objFSO.OpenTextFile("C:\METWEB\BATCH\IMAGES_ENS\RAW\TEST.txt", 1)
    Set objOutFile = objFSO.OpenTextFile("C:\METWEB\BATCH\IMAGES_ENS\RAW\MID.txt", 8, True)


    Do Until objInFile.AtEndOfStream

    strLine = objInFile.Readline

    intPos = inStr(strLine, strFind)

    If NOT intPos = 0 Then

    objOutFile.WriteLine strLine

    End If

    Loop


    Any other help you might have would be really great. Many thanks Ian

  11. #11
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: sztext and wite new links

    Where is your html file coming from?
    Is there only going to be one image per html file?
    Is the image always going to be in the tmp directory?

  12. #12

    Thread Starter
    Junior Member gucci's Avatar
    Join Date
    Jun 2008
    Posts
    26

    Re: sztext and wite new links

    Quote Originally Posted by MarkT
    Where is your html file coming from?
    Is there only going to be one image per html file?
    Is the image always going to be in the tmp directory?
    Hi Mark,

    The html file is coming from the NCEP server. I request what image file to make by inputing a URL
    eg - http://nomad5.ncep.noaa.gov/cgi-bin/...2Fgens20080825

    Note the dates and different vars included with the url. I use DOS and a program called DOFF to create the correct url, then i use a program called wget to get the html file, but the image url is local to the server, so in order the GRAB the image of the server i then need the full url of the image from the html page, so i can use wget to get the correct image - dos that make sense?

    The dos code is below for the html:

    REM ENS IMAGES

    REM SET DATES

    REM SET MAIN DATE
    for /f "tokens=1-3 delims=/ " %a in ('C:\METWEB\PROGRAMS\DOFF\doff yyyymmdd -1') do (set DATE=%a)
    ECHO %DATE%
    REM SET DAY NUMBER
    for /f "tokens=1-3 delims=/ " %a in ('C:\METWEB\PROGRAMS\DOFF\doff dd') do (set DAY=%a)
    ECHO %DAY%
    REM SET MONTH TEXT
    for /f "tokens=1-3 delims=/ " %a in ('C:\METWEB\PROGRAMS\DOFF\doff mm') do (set mm=%a)
    if %mm%==01 set mm=jan
    if %mm%==02 set mm=feb
    if %mm%==03 set mm=mar
    if %mm%==04 set mm=apr
    if %mm%==05 set mm=may
    if %mm%==06 set mm=jun
    if %mm%==07 set mm=jul
    if %mm%==08 set mm=aug
    if %mm%==09 set mm=sep
    if %mm%==10 set mm=oct
    if %mm%==11 set mm=nov
    if %mm%==12 set mm=dec
    ECHO %mm%
    REM SET YEAR
    for /f "tokens=1-3 delims=/ " %a in ('C:\METWEB\PROGRAMS\DOFF\doff yyyy ') do (set YEAR=%a)
    ECHO %YEAR%



    C:\METWEB\programs\wget\wget "http://nomad5.ncep.noaa.gov/cgi-bin/pdisp_gens.sh?ctlfile=gec00_00z.ctl&ptype=map&povlp=noovlp&psfile=off&var=PRMSLmsl&level=1000&op1=no ne&op2=none&hour=00Z&day=%DAY%&month=%mm%&year=%YEAR%&proj=custom&lon0=-60&dlon=90&lat0=30&dlat=40&type=shaded&cint=def&white=def&fwrite=off&plotsize=800x600&title=&dir=%2F gens%DATE%" -O C:/METWEB/BATCH/IMAGES_ENS/RAW/TEST.txt


    ------------------------------------------------------------------

    Now wget should be able to convert the links with the html page for me by using -convert links command, but because of the wildcards within the url i can't get this the work.

    The image will always be in the tmp dir.

    Cheers,

    Ian

  13. #13
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: sztext and wite new links

    If you are able to down load the file then this should work.
    Code:
    Option Explicit
    Const ForReading = 1
    
    Dim fso, ts
    Dim strHTML, strFile 
    
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        strFile = "C:\METWEB\BATCH\IMAGES_ENS\RAW\TEST.txt"
        
        'Check if the file exists
        If fso.FileExists(strFile) Then
            'open the file
            Set ts = fso.OpenTextFile("C:\METWEB\BATCH\IMAGES_ENS\RAW\TEST.txt", ForReading)
            'Read the contents into a variable
            strHTML = ts.ReadAll
    
            'Don't need the TextStream any more so do your cleanup
            ts.Close
            Set ts = Nothing
        
            'Pull the image tag from the HTML
            strHTML = FindImageTag(strHTML)
        
    		'Get the source attribute from the image tag
            strHTML = FindImageSource(strHTML)
            
            'do with as you please
            MsgBox strHTML
        End If
        
        'Cleanup
        Set fso = Nothing
    
    
    Function FindImageTag(ByVal HTML)
    Dim intPos
    
        'Find the start of the image tag
        intPos = InStr(1, HTML, "<img", vbTextCompare)
        'Throw out everything before the start
        HTML = Mid(HTML, intPos)
        
        'Find the end of the image tag
        intPos = InStr(HTML, ">")
        'Throw out everything after the end of the image tag
        HTML = Left(HTML, intPos)
        
        'Return the stripped out tag
        FindImageTag = HTML  
    End Function
    
    Function FindImageSource(ByVal ImgTag) 
    Dim objDoc, objNode, objAttrib 
    
        'Make sure the image tag has a src attribute
        If InStr(1, ImgTag, "src=", vbTextCompare) > 0 Then 
            Set objDoc = CreateObject("Microsoft.XMLDOM")
            objDoc.async = False
            
            'Set the img tag to be a valid xml node
            ImgTag = Replace(ImgTag, ">", " />")
            
            'Load the img tag as xml
            objDoc.loadXML "<image>" & ImgTag & "</image>"
            
            'pull the image node
            Set objNode = objDoc.selectSingleNode("image/img")
            
            'Get the src attribute from the node
            Set objAttrib = objNode.Attributes.getNamedItem("src")
            
            'Return the source
            FindImageSource = objAttrib.Text
            
            'cleanup
            Set objAttrib = Nothing
            Set objNode = Nothing
            Set objDoc = Nothing
        End If
    End Function

  14. #14

    Thread Starter
    Junior Member gucci's Avatar
    Join Date
    Jun 2008
    Posts
    26

    Re: sztext and wite new links

    hi mark,

    works a treat - thanks,

    how would i write this to a txt file, the script looks complicated, don't know where to put the create new file and write the strhtml.

    cheers,

    Ian

  15. #15
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: sztext and wite new links

    The code should return something like
    /tmp/ATEST21591.png

    What exactally are you trying to write to a file? How should it look and what is the filename?

  16. #16

    Thread Starter
    Junior Member gucci's Avatar
    Join Date
    Jun 2008
    Posts
    26

    Re: sztext and wite new links

    Hi Mate,

    Need to create an new text file called med.txt with http://nomad5.ncep.noaa/tmp/ATEST21591.png


    Also, if you get a chance could you go though step by step on how the script works so i can understand it please.

    Many thanks,

    Ian

  17. #17

    Thread Starter
    Junior Member gucci's Avatar
    Join Date
    Jun 2008
    Posts
    26

    Re: sztext and wite new links

    Hi Mark,

    After trying lots of different things, i am still unable to wrtie the strHTML message to a new text file.

    I am not sure were or how to put the code.

    The code i was using to write is below:

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objOutFile = objFSO.OpenTextFile("C:\METWEB\BATCH\IMAGES_ENS\RAW\MID.txt", 8, True)

    strLine = "http://nomad5.ncep.noaa.gov/"&objOutFile
    intPos = inStr(strLine,strHTML)
    If NOT intPos = 0 Then
    objOutFile.WriteLine strLine
    Cheers,

    Ian

    Webmaster and Forecaster
    MetWeb - UK

    http://www.metweb.axspace.com

  18. #18

    Thread Starter
    Junior Member gucci's Avatar
    Join Date
    Jun 2008
    Posts
    26

    Re: sztext and wite new links

    hi Mark,

    found the problem and fixed it - i might be starting to get the hang on this!!!

    I placed this code in to write :

    'create and write new file

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.CreateTextFile("C:\METWEB\BATCH\IMAGES_ENS\RAW\med.txt",true,false)
    ts.Write"http://nomad5.ncep.noaa"&(strHTML)
    ts.Close
    Set ts = Nothing

    Cheers for you help
    Cheers,

    Ian

    Webmaster and Forecaster
    MetWeb - UK

    http://www.metweb.axspace.com

  19. #19
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: sztext and wite new links

    I would replace the MsgBox line with your code.

    If you place it there, you don't have to create the fso again since it hasn't been set to nothing yet. So in the code above I would replace
    Code:
    MsgBox strHTML
    With
    Code:
        Set ts = fso.CreateTextFile("C:\METWEB\BATCH\IMAGES_ENS\RAW\med.txt",true,false)
        ts.Write "http://nomad5.ncep.noaa" & strHTML
        ts.Close
        Set ts = Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width