Link to home
Start Free TrialLog in
Avatar of DebbieFost
DebbieFostFlag for United States of America

asked on

Need some lines of VBS code to only run a program if a file exisits

I have a VBS script that looks for an input file (C:\matters.csv) when it is run. I am trying to set this up so it runs automatically every hour, but I want to prefix the first few lines of this script some code that will abort the script if the file C:\matters.csv DOES NOT exist.

Can somebody give me give me some lines of code I can insert at the beginning of my script to only run the rest of the script if that actual file exists?
Avatar of WebDevEM
WebDevEM
Flag of United States of America image

Hi... Here's a simple version of it that will either display a Yes or No based on the your file existing or not.  You could also combine the 2 subroutines into 1, or merge them into the main code, but I like to keep things modular so I can reuse them without having to rewrite them for each program I write.

I hope this helps!

Set objFSO = CreateObject("Scripting.FileSystemObject")

If FileExists("C:\matters.csv") Then

    msgbox("Yes")
Else
    msgbox("No")
End If
' <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
Function FileExists (WhatFile)
		
	If CheckFile(WhatFile) Then
        FileExists=true
    Else
        FileExists=false
    End If
End Function
' <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
Function CheckFile (WhatFile)
    If objFSO.FileExists(WhatFile) Then
        CheckFile=true
    Else
        CheckFile=False
    End If
End Function
' <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

Open in new window

Avatar of remmett70
think this will do the trick.

Option Explicit   
dim fs, strFile

Set fs = CreateObject("Scripting.FileSystemObject")
Set strFile = "C:\matters.csv
If fs.FileExists(strFile) Then
    ' 
    ' what you want the file to do.
    '        
End If

Open in new window

Avatar of DebbieFost

ASKER

Remmett70  - I am getting an error message:

Object required "[string: "C:\Matters.csv"]"

This error shows up whether matters.csv file is present or not.
Avatar of Bill Prew
Bill Prew

This line is missing a closing quote, change:

Set strFile = "C:\matters.csv

to:

Set strFile = "C:\matters.csv"

~bp
Sorry, was missing the closing quote after the file name.

Option Explicit   
dim fs, strFile

Set fs = CreateObject("Scripting.FileSystemObject")
Set strFile = "C:\matters.csv"
If fs.FileExists(strFile) Then
    ' 
    ' what you want the file to do.
    '        
End If

Open in new window

I already changed that, so it must be something else - error message points to Live 5 / Char 1, object required. My actual object path is a little different that the example...is the length a problem?:

Option Explicit  
dim fs, strFile

Set fs = CreateObject("Scripting.FileSystemObject")
Set strFile = "\\workgroupserver\Apps\Worldox\WDPClaw\Matters.csv"
If fs.FileExists(strFile) Then
    '
    ' what you want the file to do.


    '
Hello Debbie,

The code you're using from Remmett70 is the combined version of what I posted in my response... same technique, but without using functions.  If you try putting your path in my version it may work, but would probably fail in a different line, which would help point you towards the solution.  (Wherever they each fail, the line should be similar)

Another possible issue may be that you don't have permission to read the file at that particular path.  If you go to Start, Run and paste in "\\workgroupserver\Apps\Worldox\WDPClaw\Matters.csv" does the file open?
the scripting.filesystemobject does not work with UNC path.  It would need to be a mapped drive.

Would you be able to use Powershell instead of VBS?
Remmet70,
    I've tested my script with a UNC path, and it works, so yours should work too.

Debbie,
    If you remove the "set" in line 5, that should do the trick.  I've run it on my computer with this code, and my own UNC path.
Option Explicit   
dim fs, strFile

Set fs = CreateObject("Scripting.FileSystemObject")
strFile = "\\workgroupserver\Apps\Worldox\WDPClaw\Matters.csv"
If fs.FileExists(strFile) Then
    ' 
    ' what you want the file to do.


    '
End If

Open in new window

Ok....we get past that error, but now we get an error if the matters.csv file exists - looks like it is coming from my original VBS script (Line 10 - invalid input file):

Option Explicit  
dim fs, strFile

Set fs = CreateObject("Scripting.FileSystemObject")
strFile = "C:\Apps\Worldox\WDPClaw\Matters.csv"
If fs.FileExists(strFile) Then
    '
    ' what you want the file to do.

InputFile = "c:\Apps\Worldox\WDPClaw\Matters.csv"

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
arrData = Split(objFSO.OpenTextFile(InputFile).ReadAll, vbNewLine)
Dim objOutput : Set objOutput = objFSO.CreateTextFile(InputFile)

For Each strData in arrData
  arrTemp = Split(strData, Chr(34) & "," & Chr(34))
    For x = 0 to uBound(arrTemp)
      objOutput.Write chr(34) & Replace(arrTemp(x),chr(34),"") & chr(34) & ","
    Next
    'if not a blank line, write the data
    If uBound(arrTemp)>0 Then objOutput.WriteLine chr(34) & Replace(arrTemp(4),chr(34),"") & ", " & Replace(arrTemp(3),chr(34),"") & chr(34)
Next

objOutput.Close


    '        
End If
ASKER CERTIFIED SOLUTION
Avatar of remmett70
remmett70
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial