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.
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)
Please log in to answer
Posted by:
anonymous_9363
14 years ago
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:
Lastly, a handy reference for you.
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
14 years ago
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
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
14 years ago
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.