Hello
I need to check if a path exists, before launching the app, if not create the folder. App´s shortcut will point to the script. I appreciate any suggestions to make this better, or explain the flaws.

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")


If Not fso.FolderExists("h:\Folder") Then
CreatePath()
End If

WshShell.run ("C:\Program Files\program\program.exe") ,0 ,False


Sub CreatePath()
If Not fso.DriveExists("h:") Then
Wscript.sleep 30000
If Not fso.DriveExists("h:") Then
msgbox "No Drive, Contact your system administrator"
wscript.Quit
End If
Else
If Not fso.FolderExists("h:\Folder") Then
fso.CreateFolder ("h:\Folder")
If err.number <> 0 Then
msgbox "unable to create folder, Contact your system administrator"
wscript.quit
End If
End If
End If
End Sub
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Community Chosen Answer

1
Well, I don't quite understand that last part of your post about the App's shortcut. But if you just need a real quick and dirty script, while yours should get the job done, if it was me, I'd tweak it slightly and do something like:

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

On Error Resume Next

If Not fso.DriveExists("h:") Then
Wscript.sleep 30000
If Not fso.DriveExists("h:") Then
msgbox "No Drive, Contact your system administrator"
wscript.Quit
End If
End If
If Not fso.FolderExists("h:\Folder") Then
fso.CreateFolder ("h:\Folder")
If Not fso.FolderExists("h:\Folder") Then
msgbox "unable to create folder, Contact your system administrator"
wscript.Quit
End If
End If

WshShell.run ("C:\Program Files\program\program.exe") ,0 ,False
Answered 06/13/2010 by: Lucid
Purple Belt

Please log in to comment
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

0
Thank you Lucid for your reply. The script will be deployed to many computers and I would like to do it properly.
I appreciate feedback on any flaws.
Answered 06/14/2010 by: admaai
Orange Senior Belt

Please log in to comment
2
A good habit to get into is avoiding duplication. To wit, you may want to define variables for the drive letter and for the path that you want to create. Something like:Public strTargetDrive
Public strTargetFolderName
Public strTargetPath

strTargetDrive = "H:\"
strTargetFolderName = "SomeFolderOrOther"

strTargetPath = strTargetDrive & "\" & strTargetFolderName
Then, if at any time your target drive changes, you only need edit ONE instance, rather than five. Ditto with the folder. So, with those changes, we have:Public strTargetDrive
Public strTargetFolderName
Public strTargetPath

strTargetDrive = "H:\"
strTargetFolderName = "SomeFolderOrOther"

strTargetPath = strTargetDrive & "\" & strTargetFolderName

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

On Error Resume Next

If Not fso.DriveExists(strTargetDrive) Then
Wscript.sleep 30000
If Not fso.DriveExists(strTargetDrive) Then
msgbox "No Drive, Contact your system administrator"
wscript.Quit
End If
End If
If Not fso.FolderExists(strTargetPath) Then
fso.CreateFolder (strTargetPath)
If Not fso.FolderExists(strTargetPath) Then
msgbox "Unable to create folder, Contact your system administrator"
WScript.Quit(False)
End If
End If

WshShell.run ("C:\Program Files\program\program.exe") ,0 ,False
You'll notice the use of what's known as Hungarian notation, where the variable prefix indicates the variable type. In VBS, all variables start off as variants, they get coerced into their "proper" data types (which you can check using the TypeName and VarType functions) so I like to "declare" the type beforehand. Here, for example, I'd use 'objFSO' instead of 'fso'. You'll see this a lot in good examples.

Lastly, a handy reference for you.
Answered 06/14/2010 by: VBScab
Red Belt

Please log in to comment
0
Thank you Ian, for the explanation and the link.
Answered 06/14/2010 by: admaai
Orange Senior Belt

Please log in to comment
Answer this question or Comment on this question for clarity