【问题标题】:Adding quotations around [duplicate]在[重复]周围添加引号
【发布时间】:2017-10-07 03:16:25
【问题描述】:

下面的脚本运行良好。它在指定位置搜索 .exe 文件并输出包含文件位置的文件。

所有这些都很好。除了我希望输出文件行都包含在“”中,怎么做?

Option Explicit 'force all variables to be declared

On Error Resume Next
Const ForWriting = 2
Dim objFSO
Set objFSO = CreateObject(Scripting.FileSystemObject)

Dim objTS 'Text Stream Object
Set objTS = objFSO.OpenTextFile("output_path", ForWriting, True)

Recurse objFSO.GetFolder("location_of_files")
objTS.Close()

Sub Recurse(objFolder)
    Dim objFile, objSubFolder

    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile.Name)) = "exe" Then
            objTS.WriteLine(objfile.Path)
        End If
    Next

    For Each objSubFolder In objFolder.SubFolders
        Recurse objSubFolder
    Next
End Sub

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    改变这一行:

    objTS.WriteLine(objfile.Path)
    

    对此:

    objTS.WriteLine("""" & objfile.Path & """")
    

    这看起来很奇怪,因为您必须将嵌入的引号加倍。

    或者您可以使用 chr(34),如下所示:

    objTS.WriteLine(chr(34) & objfile.Path & chr(34))
    

    chr() 函数将指定的character code 转换为字符。

    【讨论】:

    • 这个确切的答案已经给出countless times before,它不像在字符串中转义引号是新事物。标记并继续前进。
    猜你喜欢
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多