【问题标题】:vbscript how to check if txt file exists and whe not create empty onevbscript如何检查txt文件是否存在以及不创建空文件
【发布时间】:2012-02-27 12:48:09
【问题描述】:

如何使用 vbscript 检查 C:\Temp\CAD_Kunde.txt 中的 txt 文件是否存在,如果不存在,则应为空创建。

编辑: 我收到一个错误(当我使用这个时,第 11 行字符 1 上的预期语句:

   <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>

【问题讨论】:

  • 将 Option 显式行放在子行之前。

标签: vbscript hta


【解决方案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>

【讨论】:

    【解决方案2】:

    很简单

    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
    

    【讨论】:

      【解决方案3】:

      如果您不关心区分存在/不存在而只想确保存在一个,您可以

      set f = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\blabla", 1, true)
      f.close()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-09
        • 1970-01-01
        • 1970-01-01
        • 2011-08-03
        • 1970-01-01
        • 2013-07-22
        • 1970-01-01
        相关资源
        最近更新 更多