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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing for folder existance

Status
Not open for further replies.

tobjob

IS-IT--Management
Sep 17, 2009
10
US
I have a script here to test for a folder's existence. If found pop up a message to show that it exists. In the long run it will run a command to uninstall the update if found. Right now I am trying to have it just locate the file and verify that is working, but I haven't had any luck. I have debugged it and it has no errors. Could anyone let me know why it isn't working properly?

option explicit
on error resume next

'initalize variables
Dim WDS2602083, WDS2602057, WDS265Beta, WDS3, WDS4, objFSO, objShell
WDS2602083 = "%systemroot%\$NtUninstallKB907371-V2$"
WDS2602057 = "%systemroot%\$NtUninstallKB907371$"
WDS265Beta = "%systemroot%\$NtUninstallKB911993$"
WDS3 = "%systemroot%/$NtUninstallKB917013$"
WDS4 = "%systemroot%\$NtUninstallKB940157$"

'Initialize File system object and Wscript Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("Wscript.Shell")

'Tests for versions and if found dispalys message
If objFSO.FolderExists(WDS2602083) Then
wscript.Echo "1"
ElseIf objFSO.FolderExists(WDS2602057) Then
wscript.Echo "2"
ElseIf objFSO.FolderExists(WDS265Beta) Then
wscript.Echo "3"
ElseIf objFSO.FolderExists(WDS3) Then
Wscript.Echo "4"
ElseIf objFSO.FolderExists(WDS4) Then
Wscript.Echo "5"
End If
 
%systemroot% is an environmental value. It cannot be used in vbs without extracting it from the objShell.
Code:
set objShell = CreateObject("WScript.Shell")
strWinDir = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%")

-Geates
 
I made the addition you suggested, but aren't able to get the script to run successfully. I have also removed
%systemroot% and put in c:\Windows...... but had no luck with that either. I have another script which you have commented on, about defragging a disk. It uses environmental variables %systemroot% and %compsec% also, but I don't acutally extract it from the objShell as you stated I need to here. Is there a reason it runs there and doesn't work here?



option explicit
on error resume next

'initalize variables
Dim WDS2602083, WDS2602057, WDS265Beta, WDS3, WDS4, objFSO, objShell, strWinDir
WDS2602083 = "%systemroot%\$NtUninstallKB907371-V2$"
WDS2602057 = "%systemroot%\$NtUninstallKB907371$"
WDS265Beta = "%systemroot%\$NtUninstallKB911993$"
WDS3 = "%systemroot%\$NtUninstallKB917013$"
WDS4 = "%systemroot%\$NtUninstallKB940157$"

'Initialize File system object and Wscript Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("Wscript.Shell")
strWinDir = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%")

'Tests for versions and if found dispalys message
If objFSO.FolderExists(WDS2602083) Then
wscript.Echo "1"
ElseIf objFSO.FolderExists(WDS2602057) Then
wscript.Echo "2"
ElseIf objFSO.FolderExists(WDS265Beta) Then
wscript.Echo "3"
ElseIf objFSO.FolderExists(WDS3) Then
Wscript.Echo "4"
ElseIf objFSO.FolderExists(WDS4) Then
Wscript.Echo "5"
End If
 
simply adding the code I suggested is only half of the work. You still need to modify your code to make use of
it.

Code:
strIncorrect = "%systemroot%\$NtUninstallKB907371-V2$"
msgbox strIncorrect

//Outputs:
//%systemroot%\$NtUninstallKB907371-V2$

strCorrect = [b]strWinDir[/b] & "\$NtUninstallKB907371-V2$"
msgbox strCorrect

//Outputs
//c:\windows\$NtUninstallKB907371-V2$

-Geates
 
> I have debugged it and it has no errors.
do this.
>on error resume next
[tt][red]'[/red]on error resume next[/tt]
 
Definately should've turned off on error resume next. I have made the changes. However, I can't get the script to recognize that the folder exists. I have removed the strWinDir and just tested it with c:\windows\etc..., but that doesn't work either. Any suggestions why it isn't recognizing the folder exsisting?

option explicit
'on error resume next

'initalize variables
Dim WDS2602083, WDS2602057, WDS265Beta, WDS3, WDS4, objFSO, objShell, strWinDir
WDS2602083 = strWinDir & "\$NtUninstallKB907371-V2$"
WDS2602057 = strWinDir & "\$NtUninstallKB907371$"
WDS265Beta = strWinDir & "\$NtUninstallKB911993$"
WDS3 = strWinDir & "\$NtUninstallKB917013$"
WDS4 = strWinDir & "\$NtUninstallKB940157$"

'Initialize File system object and Wscript Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("Wscript.Shell")
strWinDir = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%")

'Tests for versions and if found dispalys message
If objFSO.FolderExists(WDS2602083) Then
wscript.Echo "1"
ElseIf objFSO.FolderExists(WDS2602057) Then
wscript.Echo "2"
ElseIf objFSO.FolderExists(WDS265Beta) Then
wscript.Echo "3"
ElseIf objFSO.FolderExists(WDS3) Then
Wscript.Echo "4"
ElseIf objFSO.FolderExists(WDS4) Then
Wscript.Echo "5"
End If
 
You still aren't defining your variable correctly. You are trying to use strWinDir before you assign it a value.

Wrong:
Code:
WDS2602083 = strWinDir & "\$NtUninstallKB907371-V2$"
strWinDir = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%")

Right:
Code:
strWinDir = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%")
WDS2602083 = strWinDir & "\$NtUninstallKB907371-V2$"

-Geates


 
I see what you mean about the variable not being defined correctly. But do you see anything indicating why it wouldn't work with the complete path "c:\windows\etc...." instead of using the variable?
 
Are you running NT or 2000. The root folder in this case would be c:\winnt. Check the %systemroot% EV to verify.

Also, your code is using an IF..ELSEIF..ENDIF statement. This logically runs until 1 condition is met, the the statement breaks. You need to test each condition.

Two ways to do it. You can change your code to IF..ENDIF statements for each condition
Code:
'Tests for versions and if found dispalys message
if objFSO.FolderExists(WDS2602083) then wscript.echo "1"
if objFSO.FolderExists(WDS2602057) then wscript.echo "2"
if objFSO.FolderExists(WDS265Beta) then wscript.echo "3"
if objFSO.FolderExists(WDS3) then wscript.echo "4"
if objFSO.FolderExists(WDS4) then wscript.echo "5"

or iterate an array of folders

Code:
dim arrFolders(4)

arrFolders(0) = "c:\windows\Folder 1"
arrFolders(1) = "c:\windows\Folder 2"
arrFolders(2) = "c:\windows\Folder 3"
arrFolders(3) = "c:\windows\Folder 4"
arrFolders(4) = "c:\windows\Folder 5"

for i = 0 to ubound(arrFolders)
   if objFSO.FolderExists(arrFolders(i)) then wscript.echo i
next

-Geates
 
Perhaps you can show us an example of your code where ""c:\windows\etc...." isn't working
 
I wanted to let you know that you were right with the environmental variables. I assumed %systemroot% was the environmental variable, but it was %windir%. I swapped that out and defined the variable correctly. It works now just as it should. I thought %systemroot% is by default a environmental variable, any information to why it isn't listed?
Thanks all of you for your help. I've learned quite a bit from this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top