首页 > 解决方案 > VBS - 文件已创建但我看不到或打开它

问题描述

我试图构建一个 VBS 来测试创建文件,因为我编写的更大的脚本没有创建输出文件。以下脚本的重点是测试功能;我目前没有看到。

Option Explicit
Dim objFSO, objFSOText, objFolder, objFile
Dim strDirectory, strFile
strDirectory = "C:\Test\next"
strFile = "\Try.txt"

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Create the Folder specified by strDirectory on line 10
Set objFolder = objFSO.CreateFolder(strDirectory)

' -- The heart of the create file script
'-----------------------
'Creates the file using the value of strFile on Line 11
' -----------------------------------------------
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile

Wscript.Quit

运行此代码时,第一次一切正常,但目标目录中没有输出文件。当我再次运行它时,它会引发文件已存在的错误。

标签: vbscript

解决方案


我认为问题在于您正在尝试一次性创建路径"C:\Test\next",这是两个嵌套文件夹的结构)并且也不要测试该路径是否已经存在。

为了创建嵌套文件夹结构,我CreateNestedFolder在您的代码中添加了一个小帮助函数并对其进行了一些整理:

Option Explicit

Dim strDirectory, strFile, overwrite

strDirectory = "C:\Test\next"
strFile      = "Try.txt"
overwrite    = True  'set this to False if you do not wish to overwrite an existing file

'Create the (nested) Folder Structure specified by strDirectory if it does not exist yet
If Not CreateNestedFolder(strDirectory) Then
    Wscript.Echo "Could not create folder " & strDirectory
Else
    Dim objFSO, objFile
    ' Create the File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' -- The heart of the create file script
    '-----------------------
    'Creates the file using the value of strFile
    ' -----------------------------------------------

    'combine the directory and filename
    strFile = strDirectory & "\" & strFile
    'Create the new file and write something in it
    Set objFile = objFSO.CreateTextFile(strFile, overwrite)
    objFile.WriteLine("This is a test.")
    objFile.Close
    Wscript.Echo "Just created " & strFile

    'Clean up the used objects
    Set objFile = Nothing
    Set objFSO  = Nothing
End If

Function CreateNestedFolder(ByVal sPath)
    'Helper function to create a nested folder structure.
    'Returns True on success, False otherwise
    Dim aFolders, oFso, i, firstIndex

    On Error Resume Next
    Set oFso = CreateObject("Scripting.FileSystemObject")

    'Check if the path already exists
    If Not oFso.FolderExists(sPath) Then
        'Find the root drive and split the path in subfolder parts
        aFolders = Split(sPath, "\")
        'Get the root path from the complete path
        If Left(sPath, 2) = "\\" Then
            'If this is a UNC path then the root will be "\\server\share"
            sPath = "\\" & aFolders(2) & "\" & aFolders(3)
            firstIndex = 4
        Else
            'For a local path, the root is "X:"
            aFolders = Split(sPath, "\")
            sPath = aFolders(0)
            firstIndex = 1
        End If
        'Loop through the aFolders array and create new folders if needed
        For i = firstIndex to UBound(aFolders)
            If Len(aFolders(i)) > 0 Then
                sPath = sPath & "\" & aFolders(i)
                If Not oFso.FolderExists(sPath) Then oFso.CreateFolder sPath
            End If
        Next
    End If

    CreateNestedFolder = (Err.Number = 0)
    On Error GoTo 0
    Set oFso = Nothing
End Function

推荐阅读