#1
|
|||
|
|||
check for valid file
I am working on a script that I almost have completed. However I am trying to add a fail-safe that will prevent a runtime error. My script for SecureCRT is written in vb. At the start of the script the user is to select a .csv file that will be read into an array for the script to use. I want to have the script check to make sure that the user entered a file, did not select cancel, and the file is a .csv file and not some other file type. If it is not a valid csv file I want to give the option to retry or abort the script.
Here is all I have that is working so far: filepath = crt.Dialog.FileOpenDialog("Please select the *.csv file to open") If FileSysObj.GetExtensionName(filepath) = "csv" <> False Then crt.Dialog.MessageBox _ "The selected file is not a valid csv file!" & vbCrLf & _ "Please choose a valid csv file!" & vbCrLf & _ End If However if the don't select a valid file the script tries to proceed and just crashes. |
#2
|
|||
|
|||
Hi Ka0sFact)r,
Thanks for the post. There are some things to consider as you write your script:
Here is an example of how to handle the cancel case: Code:
strFilePath = crt.Dialog.FileOpenDialog("Please select the CSV file to open.") If strFilePath = "" Then crt.Dialog.MessageBox "You cancelled the script." End If Code:
strFilePath = crt.Dialog.FileOpenDialog("Please select the CSV file to open.") Set fso = CreateObject("Scripting.FileSystemObject") If Not fso.FileExists(strFilePath) Then crt.Dialog.MessageBox "The file you entered does not exist." End If Code:
strFilePath = crt.Dialog.FileOpenDialog("Please select the CSV file to open.") Set fso = CreateObject("Scripting.FileSystemObject") strExt = fso.GetExtensionName(strFilePath) If strExt <> "csv" Then crt.Dialog.MessageBox "Invalid file type selected." End If Code:
strFilePath = crt.Dialog.FileOpenDialog("Please select the CSV file to open.",_ ,_ "*.csv",_ "CSV Files (*.csv)|*.csv||") Last edited by rtb; 03-03-2014 at 10:01 AM. |
#3
|
|||
|
|||
Solution I used
Hi Todd,
Thanks for your response. I found something that seems to work the way I wanted it to and I wanted to be sure to share it in case someone else runs into a similar issue. The file selection takes place at the start of my script and I decided that the safest course to take is to stop the script all together if the user does not select a valid file. Here is the code I chose to use though I may try to integrate your example for if the file doesn't exist: filepath = crt.Dialog.FileOpenDialog("Please select the *.csv file to open") If Not FileSysObj.GetExtensionName(filepath) = "csv" Or filepath = "" Then crt.Dialog.MessageBox _ "The selected file is not a valid csv file!" & vbCrLf & _ "Please choose a valid csv file!" & vbCrLf Exit Sub End If |
![]() |
Tags |
file calling , file types , securecrt , vbscript |
Thread Tools | |
Display Modes | Rate This Thread |
|
|