VBscript用文件名的一部分替换文本

时间:2016-08-06 22:29:19

标签: vbscript

我有一个文件目录,我想循环并使用其部分文件名替换模板文档中的文本。

例如,一个文件名可能是'NV_AD32_city.dxf'。目录中的所有文件都遵循相同的文件名模式XX_XXXX_string.dxf,使用两个下划线。

我需要捕获第一个“_”右侧和“。”左侧的字符串,所以这个例子就是'AD32_city'

如何使用捕获活动文件的文本来替换模板中的文本?我想我需要创建一个对象?但是目录中的当前文件使用的对象是什么? ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++

回复的答案,伙计们。经过几天尝试你的代码,我只是没有“得到它”。我知道它设置为采用我想要的文件名字符串的一部分但是如何告诉脚本使用我循环的当前文件?到目前为止,这是我的脚本。我在Sub'GetNewInputs'

下的第20行有你的代码
Set fso = CreateObject("Scripting.FileSystemObject")

Option Explicit
Dim WritePath : WritePath = "S:\TempFolder\"
Dim OutFile : OutFile = "VEG_DXF-2-SHP_script-"
Dim WorkingFile : WorkingFile = GetFileContent(SelectFile())
Dim NewState, NewSection, NewArea
Dim OldState, OldSection, OldArea

Call GetNewInputs()
Call GetOldInputs()

Sub GetNewInputs()       
        NewState = UCase(InputBox("INPUT STATE:", _
        "INPUT STATE", "SOCAL"))

        NewSection = ("Section_" & InputBox("INPUT SECTION NUMBER:", _
        "INPUT SECTION", "14"))

        NewArea = "^[^_]+_(.*)\.dxf$"       
End Sub

Private Sub GetOldInputs()
        OldState = "XX"
        OldSection = "_X"
        OldArea = "ZZZZ"   
End Sub

Function SelectFile()  
        SelectFile = vbNullString  
        Dim objShell : Set objShell = WScript.CreateObject("WScript.Shell")  
        Dim strMSHTA : strMSHTA = "mshta.exe ""about:" & "<" & "input        type=file id=FILE>" _  
        &"<" & "script>FILE.click();new     ActiveXObject('Scripting.FileSystemObject')" _  
       &".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);" & "<" & "/script>"""  

    SelectFile = objShell.Exec(strMSHTA).StdOut.ReadLine()  

    If SelectFile = vbNullString Then  
            WScript.Echo "No file selected or not a text file."  
            WScript.Quit  
    End If  
End Function

Private Function GetFileContent(filePath)      
    Dim objFS, objFile, objTS      
    Set objFS = CreateObject("Scripting.FileSystemObject")      
    Set objFile = objFS.GetFile(filePath)      
    Set objTS = objFile.OpenAsTextStream(1, 0)      

    GetFileContent = objTS.Read(objFile.Size)      
    Set objTS = Nothing      
End Function

For Each FileRefIn fso.GetFolder("S:\SOCAL\Section_14\Veg DXFs\").Files
    NewFile = WorkingFile
    NewFile = Replace(NewFile, OldState, NewState)
    NewFile = Replace(NewFile, OldSection, NewSection)
    NewFile = Replace(NewFile, OldArea, NewArea)
    WriteFile NewFile, WritePath & OutFile & ".gms"
    WScript.Echo NewArea
Next

Private Sub WriteFile(strLine,fileName)  
    On Error Resume Next  
    Dim objFSO, objFile  
    Set objFSO = CreateObject("Scripting.FileSystemObject")  

    Do Until IsObject(objFile)  
            Set objFile = objFSO.OpenTextFile(fileName, 8, True)  
    Loop  

    objFile.WriteLine strLine  
    objFile.Close  
End Sub

2 个答案:

答案 0 :(得分:0)

嗯,这实际上是两个问题。

要枚举目录中的文件,可以使用FileSystemObject,如下所示(未经测试)

const strFolderPath = "C:\Temp\Whatever"
set objFSO = CreateObject( "Scripting.FileSystemObject" )
set objFolder = objFSO.GetFolder( strFolderPath )
set colFiles = objFolder.Files
for each objFile in colFiles
    ' Do whatever you want with objFile
next

Here's the reference这些对象的属性/方法。

要提取部分文件名,您可以使用正则表达式。

Here’s some guide如何在VBScript中使用&#39; em。

以下表达式应该适用于您,它将捕获您要求的文件名部分:

"^[^_]+_(.*)\.dxf$"

答案 1 :(得分:0)

如果需要编辑.dxf文件的内容,则需要在AutoCAD VBA(Visual Basic for Applications)环境中工作。

如果是这种情况,您需要从以下内容开始:

GetObject("AutoCAD.Application.20")
CreateObject("AutoCAD.Application.20")

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-ActiveX/files/GUID-0225808C-8C91-407B-990C-15AB966FFFA8-htm.html

**请注意“VBA不再随AutoCAD安装一起分发;必须单独下载和安装。可以下载适用于Autodesk AutoCAD的VBA启动器here。”