Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange WSH behavior

Status
Not open for further replies.

Jerim65

Technical User
Aug 8, 2010
99
AU
In my apps which I have written over a number of years I have Used the Windows Scripting Host capabilty to identify special folders on the user's PC.

In particular I need their My Documents folder path so that I can access folders created during the installation.

As a test to check these values I have created a file with the values in that can be inspected - the code is


WshShell = Createobject("WScript.Shell")

Strtofile('Special Folders on this PC ','paths.txt',1)
Strtofile('' +Chr(013)+Chr(10),'paths.txt',1)
Strtofile('' +Chr(013)+Chr(10),'paths.txt',1)

For i = 1 To 17

mytarget = Addbs(WshShell.SpecialFolders(i))

reporter = Transform(i)+' ' +mytarget

Strtofile(reporter,'paths.txt',1)
Strtofile('' +Chr(013)+Chr(10),'paths.txt',1)


Endfor

the output I am using in my application is for #16 :-

mytarget = Addbs(WshShell.SpecialFolders(16))

Which gives me the path to the My Documents or Documents folder.

Now today I was chasing a missing output file on that path - so I ran the program again

Today I get for #16
16 - C:\Users\bryan\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\
Previously
16 - C:\Users\bryan\Documents

Is anyone aware of what might have happened in my PC ?
Is it a registry entry which has been corrupted?

Thanks

Coldan
 
Coldan,

By hard-coding the number 16, you are relying on the assumption that MyDocuments will always be the 16th member of the SpecialFolders collection. As far as I know, this is undocumented. The number could change with different versions of Windows.

Instead of this:

mytarget = Addbs(WshShell.SpecialFolders(16))

you should be doing this:

mytarget = Addbs(WshShell.SpecialFolders("MyDocuments"))

Another option is to use the Windows SHGetSpecialFolderLocation() function. If you like, I can give you an example of that, but I suggest you try the above solution first, as it's much simpler.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Thanks Mike,

I understand what you are saying BUT this has been used all over the world for the last 6 years without a complaint from my users. It must have given a consistent result.
It also works on my WIN7 EeePC and it did work on my desktop till it came out wrong today.


I get an empty string with your alternative code.

I've tried
mytarget = Addbs(WshShell.SpecialFolders(16))
mytarget = Addbs(WshShell.SpecialFolders("MyDocuments"))
mytarget = Addbs(WshShell.SpecialFolders("Documents"))
mytarget = Addbs(WshShell.SpecialFolders("Personal"))

The only one that gives a result is the first.

I am experimenting with some code for SHGetSpecialFolderLocation()

If you are able to give me your example I would appreciate it.

Regards

Coldan
 
Mike,

As an addendum here is the code I am trying
<vfp>

Strtofile('Special Folders on this PC ','paths2.txt',1)
Strtofile('' +Chr(013)+Chr(10),'paths2.txt',1)
Strtofile('' +Chr(013)+Chr(10),'paths2.txt',1)

fname = 'rego.txt'

N = Adir(aValues,fname )

Set Step On

If N > 0

m = Alines(aValues2,Filetostr(fname))
For i = 1 To m

lcname = aValues2

Strtofile(Alltrim(lcname),'paths2.txt',1)
Strtofile(''+Chr(013)+Chr(10),'paths2.txt',1)&&+Chr(015)+Chr(012)

If !Empty(lcname)
lcnode = Getwordnum(m.lcname, Getwordcount(m.lcname))

Strtofile(Alltrim(lcnode),'paths2.txt',1)
Strtofile('' +Chr(013)+Chr(10),'paths2.txt',1)&&+Chr(015)+Chr(012)


iRetval=SHGetSpecialFolderPath(0, @cDir,&lcnode, 1)
*!* original code line iRetval=SHGetSpecialFolderPath(0, @cdir, 0x001A, 1)

If iRetval=1
*- function succeeded
Strtofile(Alltrim(cDir),'paths2.txt',1)
Strtofile('' +Chr(013)+Chr(10),'paths2.txt',1)&&+Chr(015)+Chr(012)

Endif


Endif

Endfor

Endif

</vfp>
 
I don't know what, but there has to be something wrong with your desktop, To me this works:

o = Createobject("WScript.Shell")
? o.SpecialFolders("MyDocuments")

It's also documented this way here:
So you're supposed to use the generalized names like "MyDokuments" and not IDs like 16.

But both work for me and so getting the wrong folder for 16 and an empty string for "MyDocuments" suggests there is something wrong with your Wscript installation or the registry or whatever fills that SpecialFolders collection.

SHGetSpecialFolderLocation may give you more reliable results.
Either Mike gives you his example or look here:

Bye, Olaf.
 
Thanks to Olaf and Michael,

There was indeed something wrong in the registry.

I was given this link


and returned Personal to the default value and now everything works as before.

I changed 16 to 'My Documents' also - thanks

Thanks

Coldan
 
In FoxPro arrays start with element 1, in many other languages arrays start with the zero element. Therefore your range 1-17, while not wrong, is incomplete. The top number is 17 as you hardcoded, you are missing item 0 which is the Desktop folder. Just an FYI.
Code:
WshShell = Createobject("WScript.Shell")
[COLOR=blue][b]nSFcount = WshShell.SpecialFolders.Count[/b][/color]
For i = [COLOR=blue][b]0[/b][/color] To [COLOR=blue][b]nSFcount - 1[/b][/color]
    ? Addbs(WshShell.SpecialFolders(i))
Endfor
 
Windows XP has 18 items in the Special FOlders collection, Vista and newer have 20 items.
 
dbMark,

Everything you say is true, but it isn't the answer to Coldan's problem. If you look at his last post, you will see that he traced the problem to a fault in the registry. Once he fixed that, it was fine.

That sounds entirely plausible, given that his code always used to work, and then suddently stopped working.

But it's good that you drew attention to the base-zero issue. It's caught me out several times in the past.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top