VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   check for valid file (https://forums.vandyke.com/showthread.php?t=11406)

Ka0sFact)r 02-28-2014 01:46 PM

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.

rtb 02-28-2014 03:50 PM

Hi Ka0sFact)r,

Thanks for the post. There are some things to consider as you write your script:
  1. Have you handled the cancel case?
  2. Have you handled the file doesn't exist case?
  3. Handle the CSV case.
It doesn't make sense to try and open a file unless you are sure that there is actually a file to open. You probably want to handle the possible issues in the order that I presented above.

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

Here is an example of how to handle the file doesn't exist case:
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

If you know that the user didn't press Cancel, and that the file is valid, you could use the following to see if it is a valid CSV file:
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

Another way that you can help your users select CSV files is to use the filter option of the FileOpenDialog() method. For example:
Code:

strFilePath = crt.Dialog.FileOpenDialog("Please select the CSV file to open.",_
    ,_
    "*.csv",_
    "CSV Files (*.csv)|*.csv||")

Does this help you move forward writing your script?

Ka0sFact)r 03-03-2014 10:59 AM

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


All times are GMT -6. The time now is 05:23 PM.