【问题标题】:"Path Not Found" when using inputBox variables使用 inputBox 变量时出现“找不到路径”
【发布时间】:2017-06-28 02:33:29
【问题描述】:

我正在构建一个脚本,该脚本使用用户的输入来创建源文件夹的路径。如果我使用完整路径(已注释掉的路径),则此方法有效。否则我会得到“找不到路径”。有人可以引导我走向正确的方向吗?

Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim sFolder : sFolder = "S:\" & newState & "\" & "Section_" & newSection &   "\Images-tracetest"
'Dim sFolder  : sFolder = "S:\SOCAL\Section_31\Images-tracetest"
Dim newState, newSection, newArea, sFile

Call GetNewInputs()
    REM======================New Inputs===============================
Sub GetNewInputs()

    newState = UCase(InputBox("INPUT STATE or REGION:", _
    "INPUT STATE", "SOCAL"))

    newSection = ("Section_" & InputBox("INPUT SECTION NUMBER:", _
    "INPUT SECTION", "31"))

End Sub
For Each sFile In objFSO.GetFolder(sFolder).Files   

    uSplit   = split(file,"_")
    newArea    = uSplit(ubound(uSplit) - 1)

    If InStr(sFile.Name, "CC") > 0 Then     
    WScript.Echo "We found a CC file! File is" & (sFile.name)   
    Else
End If
Next

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    为什么它不会抛出错误。在获取用户输入之前,您正在创建一个变量 sFolder。因此,即使用户输入它们也没关系,它们永远不会被使用。 Dim 语句或变量初始化在任何子或函数之外初始化时发生在最开始。你必须做这样的事情

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    **public sFolder** 
    'Dim sFolder  : sFolder = "S:\SOCAL\Section_31\Images-tracetest"
    Dim newState, newSection, newArea, sFile
    
    Call GetNewInputs()
        REM======================New Inputs===============================
    Sub GetNewInputs()
    
    newState = UCase(InputBox("INPUT STATE or REGION:", _
    "INPUT STATE", "SOCAL"))
    
    newSection = ("Section_" & InputBox("INPUT SECTION NUMBER:", _
    "INPUT SECTION", "31"))
    **sFolder = "S:\" & newState & "\" & "Section_" & newSection &   "\Images-tracetest"**
    
    
    End Sub
    For Each sFile In objFSO.GetFolder(sFolder).Files   
    
        uSplit   = split(file,"_")
        newArea    = uSplit(ubound(uSplit) - 1)
    
        If InStr(sFile.Name, "CC") > 0 Then     
            WScript.Echo "We found a CC file! File is" & (sFile.name)   
        Else
        End If
    Next
    

    【讨论】:

    • 您可能希望添加:在开头添加Option Explicit对于阻止此类错误非常重要
    【解决方案2】:

    此外,您添加了两次“Section_”:

    sFolder = "S:\" & newState & "\" & "Section_" & newSection &   "\Images-tracetest"
    
    
    newSection = ("Section_" & InputBox("INPUT SECTION NUMBER:", _
        "INPUT SECTION", "31"))
    

    sFolder 的诊断 .Echo 是个好主意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多