Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 02-28-2014, 12:46 PM
Ka0sFact)r Ka0sFact)r is offline
Registered User
 
Join Date: Feb 2014
Posts: 2
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.
Reply With Quote
  #2  
Old 02-28-2014, 02:50 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,307
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?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730

Last edited by rtb; 03-03-2014 at 10:01 AM.
Reply With Quote
  #3  
Old 03-03-2014, 09:59 AM
Ka0sFact)r Ka0sFact)r is offline
Registered User
 
Join Date: Feb 2014
Posts: 2
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
Reply With Quote
Reply

Tags
file calling , file types , securecrt , vbscript


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 01:38 PM.