/build/static/layout/Breadcrumb_cap_w.png

Is this ok?

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

Answers (4)

Posted by: anonymous_9363 13 years ago
Red Belt
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.
Posted by: Lucid 13 years ago
Purple Belt
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
Posted by: admaai 13 years ago
Orange Senior Belt
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.
Posted by: admaai 13 years ago
Orange Senior Belt
0
Thank you Ian, for the explanation and the link.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ