vbscript how to check if txt file exists and whe not create empty one

28,401

Solution 1

<SCRIPT Language="VBScript">
Sub Window_OnLoad
Option Explicit 
Dim oTxtFile 
With (CreateObject("Scripting.FileSystemObject"))
  If .FileExists("C:\Temp\CAD_Kunde.txt") Then
    Msgbox "File Exist"
  Else 
    Set oTxtFile = .CreateTextFile("C:\Temp\CAD_Kunde.txt")
    Msgbox "File Created" 
  End If 
End With
End Sub
</script>

Solution 2

Its Easy

Option Explicit 
Dim oFSO, oTxtFile


Set oFSO = CreateObject("Scripting.FileSystemObject") 



If oFSO.FileExists("C:\Temp\CAD_Kunde.txt")  Then
Msgbox "File Exist"
Else 
Set oTxtFile = oFSO.CreateTextFile("C:\Temp\CAD_Kunde.txt") 
Msgbox "File Created"
End If
Share:
28,401
user1225282
Author by

user1225282

Updated on July 09, 2022

Comments

  • user1225282
    user1225282 almost 2 years

    How can i check with vbscript if the txt file in C:\Temp\CAD_Kunde.txt exists and when it does not exist it should be empty created.

    Edit: I get an error (Expected Statement on Line 11 Char 1 when i use this:

       <SCRIPT Language="VBScript"> 
            Sub Window_OnLoad
     //Line 11 is the one below:
        Option Explicit  
        Dim oFSO, oTxtFile   
        Set oFSO = CreateObject("Scripting.FileSystemObject")     
        If oFSO.FileExists("C:\Temp\CAD_Kunde.txt")  then
               Msgbox "File Exist" 
        Else 
              Set oTxtFile = oFSO.CreateTextFile("C:\Temp\CAD_Kunde.txt")  
              Msgbox "File Created" 
        End If 
    
        End Sub
        </script>