批处理命令以查找和替换字符串

时间:2019-12-23 10:28:11

标签: cmd

我希望批处理命令在Word文件中查找并替换字符串,还希望使用相同的字符串以及文件夹的名称来重命名该文件。

需要搜索多个文件并用字符串替换,同时还要检查文件名。

2 个答案:

答案 0 :(得分:1)

没有批量集成功能。 Powershell具有这样的功能,但是我会考虑使用fart.exe,它更易于使用。

这是链接-> http://fart-it.sourceforge.net/

// EDIT:看起来我还没有识别出“ word文件”。 如果是这样的话,我不知道使用批处理/ cmd进行此操作的可能性。

答案 1 :(得分:0)

这是Allen Wyatt的宏脚本,可以执行此操作。 Source

Public Sub MassReplace()
    With Application.FileSearch
        .LookIn = "C:\"             ' where to search
        .SearchSubFolders = True    ' search the subfolders
        .FileName = "*.doc"         ' file pattern to match

        ' if more than one match, execute the following code
        If .Execute() > 0 Then
            ' for each file you find, run this loop
            For i = 1 To .FoundFiles.Count
                ' open the file based on its index position
                Documents.Open FileName:=.FoundFiles(i)

                ' search and replace the address
                selection.Find.ClearFormatting
                selection.Find.Replacement.ClearFormatting
                With selection.Find
                    .Text = "OldAddress"
                    .MatchCase = True
                    .Replacement.Text = "NewAddress"
                End With
                selection.Find.Execute Replace:=wdReplaceAll

                ' replace e-mail address
                With selection.Find
                    .Text = "Oldemail"
                    .Replacement.Text = "Newemail"
                End With
                selection.Find.Execute Replace:=wdReplaceAll

                ' save and close the current document
                ActiveDocument.Close wdSaveChanges
            Next i
        Else
            ' if the system cannot find any files
            ' with the .doc extension
            MsgBox "No files found."
        End If
    End With
End Sub

根据您的需要更改这3行:

.LookIn = "C:\"             ' where to search
.SearchSubFolders = True    ' search the subfolders
.FileName = "*.doc"         ' file pattern to match

除此之外,从批处理文件(特别是因为您在说单词文档)中执行此操作超出了CMD的能力。