Variable Not Recognised by IMG SRC in vbscript

May 22, 2016 at 06:18:34
Specs: Windows 7, 1.6 gb 4gb
Had started to work on a script but soon realised that it wasn't going to be as simple as I thought. Naively, I was expecting it to be a matter of simply identifying the jpg files from a folder and passing them via a variable for display. What I didn't realise was that the variable "strPathway" is not considered a variable by IMG SRC. Checking out the net I realise that the answer probably lies in using inner HTML but I can't figure out how to make that work. I came across a number of solutions in Javascript but that was of no use to me as it has to be VBScript. However, for all I know, which isn't much, it perhaps can't be done using vbscript! Can anyone shed some light on this for me? How do I pass the info held by the variable "strPathway" to IMG SRC.

Attached is the bare bones of my script. I didn't take it any further as I was trying to resolve my problem. I would be grateful for any suggestions offerred.

Thank you

Script Starts here:
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objExplorer = CreateObject("InternetExplorer.Application")

strPath = "C:\Local History\"

'Set the folder
Set FLD = FSO.GetFolder(strPath)

'Loop through each file in the folder.
For Each fil in FLD.Files
'Get complete file name with path.
strOldName = fil.Name
strPathway = fil.Path

'Identify jpg files
if right(strPathway,4) = ".jpg" then
strJPGPathway = strPathway & VBCrLf

' IE settings
With objExplorer
.Navigate "about:blank"
.Visible = 1
.Document.Title = "Show Image"
.Document.Body.InnerHTML = "<img src='strJPGPathway' height = 200 width=200>"
End With
end if

End of script

See More: Variable Not Recognised by IMG SRC in vbscript

Report •

May 22, 2016 at 23:29:25
This is your main pain:
.Document.Body.InnerHTML = "<img src='strJPGPathway' height = 200 width=200>"

note that you need to handle the quotes and the concatenations presented to the document:
.Document.Body.InnerHTML = "<img src='"&strpathway&"' height = 200 width=200>"

Thank "someone" that html allows both dbl and single quotes for literals. Otherwise it would look like a plague of locusts: ""'"""'""
I still don't know what you really intended to do, since all this does is display pictures in directory sequence. All you need for that is (batch, not vbs):
::----- batch snip
for %%a in (*.jpg) do (
start %%a
::===== end snippet

Since I already had this, I'll send it out. Batch to build simple picture table:
@echo off>x & setlocal enabledelayedexpansion
set types=*.jpg *.bmp
set wid=140
set row=5
set n=-1
:: build a simple header
>x echo ^<html^>^<body bgcolor='#837d0e' text='white' link='white' alink='green' vlink='gray' bgcolor='#837d0e'^>
>>x echo ^<table^>^<tr align='center'^>

for /f %%a in ('dir /b /on %types%') do (
set /a n+=1
if !n! equ %row% (
>>x echo ^</tr^>^<tr align='center'^>
>>x echo ^!labels!^</tr^>^<tr align='center'^>
set labels=
set n=0

>>x echo ^<td^>^<img align='center' width='%wid%' src='%%a'^>^</a^>^</td^>
set labels=!labels!^^^<td^^^>%%~nxa^^^</td^^^>

>>x echo ^</tr^>^<tr^>!labels!^</tr^>
>>x echo ^</TR^>^</TABLE^>^</BODY^>^</HTML^>
type x
echo contents stored in file "x"
:: edit:
:: corrected this line:
>>x echo ^<td^>^^<img align='center' width='%wid%' src='%%
to remove the doubled carat between "td" and "img"

message edited by nbrane

Report •

May 23, 2016 at 02:24:46
Thank you for your reply. I have tried your batch file assuming it resides in the same folder as image files. Unfortunately it produced an error. I can't see a way to attach a file so here it is:
The system cannot find the file specified
The system cannot find the file specified
<html><body bgcolor='#837d0e' text='white' link='white' alink='green' vlink='gray' bgcolor='#837d0e'>
<table><tr align='center'>
contents stored in file "x"

I am assuming that the file specified refers to the image files both of which are ok.
I renamed the x file to x.html in case it was meant to have that extension. That just produced a full screen a greenish colour and just the names of the 2 image files in white.

I couldn't make your inner html solution work either but it's probably to do with the rest of my script. The reason behind what I am trying to do is that I need to catalogue a lot of jpgs. This means that I have to view each one them so I can decide on a category for it. Rather than separate them into different folders I decided it was easier to keep them all in the one place and just filter them as required. I wanted an uncluttered screen with either one thumbnail displayed at a time or a small selection of thumbnails and I was going to display the file name along with an Input box to let me amend the file name to reflect the category (using FSO.MoveFile OldName, NewName). I know there are lots of utilities around for this kind of thing but I wanted something quite simple.

Thank you again for your help. What am I overlooking in your batch file?

Report •

May 23, 2016 at 13:09:18
Okay, looking at the original VBScript, and there's a lot going on.
• Your extension check doesn't account for different cases. (In VBScript, ".JPG" ≠ ".jpg", but in the rest of Windows, it does.)
• For each ".jpg" file you find, you're setting IE's presentation (including re-navigating to about:blank, removing the previous img tag). If you run the script and no ".jpg" files are found, you're left with an invisible but running copy of IE.
• Your strJPGPathway variable is just the file path, but with a line feed added at the end, ensuring IE could never find the picture.
• As nbrane pointed out, you're not so much as using the "strJPGPathway" variable, as much as you're just throwing the name in the string, and hoping VBScript is smart enough to know you wanted to use the variable. It's not.
• Using objExplorer.Document.Body.InnerHTML = would undo the work done for the previous picture, but the objExplorer.Navigate already did that.
• Your indenting is sloppy, which becomes an issue if you're trying to get someone to look over your code.

So fixing all of that, and you'd end up with something like this:

Const strPath = "C:\Local History\"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objExplorer = CreateObject("InternetExplorer.Application")
Set picList = CreateObject("Scripting.Dictionary")

With picList
  .CompareMode = 1
  .Add "jpg", True : .Add "jpeg", True
  .Add "gif", True : .Add "png", True
  .Add "bmp", True
End With
With objExplorer
  .Navigate "about:blank"
  .Document.Title = "Show Image"
  Set doc = .Document
  Set style = doc.CreateElement("style")
  style.innerHTML = ".pic { padding-bottom: 1em }"
  doc.body.appendChild style
  .Visible = 1
End With

'Loop through each file in the folder.
For Each fil in FSO.GetFolder(strPath).Files
  If picList.Exists(FSO.GetExtensionName(fil)) Then
    Set pic = doc.CreateElement("div")
    Set img = doc.CreateElement("img")
    Set cap = doc.CreateElement("div")
    img.src = fil : img.title = fil
    cap.innerText = fil
    pic.className = "pic"
    pic.appendChild img : pic.appendChild cap
    doc.body.appendChild pic
  end if
Next 'fil

How To Ask Questions The Smart Way

Report •

Related Solutions

May 23, 2016 at 20:38:18
In my post, this was wrong, although I don't know how it happened since my "original" did not have the extra carat:
>>x echo ^<td^>^^<img align='center' width='%wid%' src='%%
should have been:
>>x echo ^<td^>^<img align='center' width='%wid%' src='%%

in my test, I got the same result that you reported, (because the extra carat caused bat to look for input from a file "img".) Removal of the excess carat fixed my test

why don't you just put all the pictures into a work dir, then "start" the work? No need for code, just right-click each icon and rename.

message edited by nbrane

Report •

May 24, 2016 at 05:24:56
Hi Razor,
Thank you for your reply and I am grateful for the script. The way that you have approached it is intriguing and along with your constructive comments has given me much food for thought.

The script does what I wanted. Don't know why the VBCrLf came to be there but the variable strJPGPathway gave me problems. At another point further on in my script (not shown here) I was using FSO.MoveFile with strJPGPathway as the original name of the file and couldn't get it to rename the file - kept getting bad file name error. I realised eventually that I had to use strPathway (originally handed the full path via fil.Name). So this little exercise of mine has produced numerous learning points for me!

Thank you once again for the script!

Report •

May 24, 2016 at 05:46:30
Hi Nbrane,

Thank you for your reply. I changed the batch file and thought for a moment that it still wasn't working until I realised that image files must be free from spaces in the file name otherwise they won't be displayed.

Thought you might wonder why I just didn't click on each name and then change it! It's a long running problem I've had - the bouncing cursor I believed its commonly call. I've had this with XP, Vista and now Windows 7 and have never been able to find a cure for it despite always having the touchpads uninstalled. I've scoured the net and tried everything I could find without success. I know it happens on Macs so it not just Microsoft (who so often get blamed). The cursor jumps about, goes from the bottom of the page right to the top left hand corner without my touching anything (its definitely not due to my touching the touchpad inadvertently - they're always disabled!). I've had files deleted and emails sent. Typing a word can be a nightmare as it comes out all jumbled up. Trying to place the cursor at a certain point in a file name is virtually impossible because it flies away to some other point on the page hence my reason for trying to write this script. The strange thing is it comes and goes. One day I'll boot up and there it is afflicting me. It might stay for a few weeks then it will stop and I'll be free for maybe 3 months until it reappears again. I've given up trying to find a solution! So there you are.

Anyway, many thanks to you for your help and the batch file!

Report •

May 24, 2016 at 20:46:04
Wow! I'd be off the rails if I had that nuisance. My mouse cursor jumps a bit, like you described, but never had it "click" on something. If my pointer did that to me, I would boot without it, or change OS, or something.
Long ago I made a list of all the ways to "keyboard" around mouse moves and operations, mainly for use by "sendkeys". Supposedly there is no mouse move that can't be (albeit tediously) rendered via keyboard. I'm sure it's online somewhere. It may take a hundred tabs and arrows and shift-alts, but eventually it goes. (At least, it did in XP and below). In a "working directory" I used arrow to get to the picture, alt-F, M to get into the naming-box (addend: F2 does this in one step). Once there, I could manipulate the text (ie: rename) and/or clip it to the clipboard for export (of filename, not picture).

Razor's methods are beyond me, but I'll have fun experimenting with some of that code.

ps: If you can camera-capture this behavior and put on you-tube, not only might it go "viral", maybe someone out there will recognize it and have a fix! I've never seen this, so I am interested to watch.

message edited by nbrane

Report •

Ask Question