【问题标题】:batch find and replace批量查找和替换
【发布时间】:2016-10-27 12:27:27
【问题描述】:


这是我的问题: 在 Windows 2003 服务器上,我有一个文件夹 (c:\test),每天都有一个应用程序在上面放置 3 个新文件。

1° 文件:
31201610181207000100000000630001
31201610181213000100000000440001
31201610181227000100000000630001
....

2° 锉刀:
31201610181214000100000000380002
31201610181234000100000009830002
31201610181344000100000000380002
...

3° 锉刀:
31201610181826000100000000580003
31201610190722000100000000580003
31201610191801000100000000580003
...

我的目标是只用 .bat 或 .vbs 脚本替换每个文件的最后 4 个字符
(0001 --> 0031)
(0002 --> 0032)
(0003 - -> 0033)
.

我已经完成了一个可以工作的 .vbs 文件,但它搜索所有字符串而不是最后 4 个字符。

Option Explicit

Dim objFSO, strFolder, objFolder, objFile
Dim strOldValue1, strNewValue1, strNewValue2, strOldValue2, strNewValue3,      
strOldValue3, objRead, strContents, objWrite
Const ForReading = 1
Const ForWriting = 2

strFolder = "c:\test"
strOldValue1 = "0001"
strNewValue1 = "0031"
strOldValue2 = "0002"
strNewValue2 = "0032"
strOldValue3 = "0003"
strNewValue3 = "0033"



' I take the folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)


' I count the file on the folder
For Each objFile In objFolder.Files
' Read file with textstream object.
Set objRead = objFSO.OpenTextFile(objFile.Path, ForReading)
' Trap error if file is empty or cannot read.
On Error Resume Next
strContents = objRead.readall
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Cannot read: " & objFile.Path
    strContents = ""
End If
On Error GoTo 0
objRead.Close


' check what's is inside the folder


If (InStr(strContents, strOldValue1) > 0) Then

    strContents = Replace(strContents, strOldValue1, strNewValue1)
    Set objWrite = objFSO.OpenTextFile(objFile.Path, ForWriting)
    objWrite.Write strContents
    objWrite.Close
End If


If (InStr(strContents, strOldValue2) > 0) Then

    strContents = Replace(strContents, strOldValue2, strNewValue2)
    Set objWrite = objFSO.OpenTextFile(objFile.Path, ForWriting)
    objWrite.Write strContents
    objWrite.Close
End If


If (InStr(strContents, strOldValue3) > 0) Then

    strContents = Replace(strContents, strOldValue3, strNewValue3)
    Set objWrite = objFSO.OpenTextFile(objFile.Path, ForWriting)
    objWrite.Write strContents
    objWrite.Close
End If

next

感谢您的帮助!!

【问题讨论】:

  • 最后一部分是否总是在这些:000100020003

标签: batch-file vbscript


【解决方案1】:

这是一个简短的批处理脚本,它会立即相应地修改所有文件C:\test\*.*

for %%F in ("C:\test\*.*") do (
    for /F "delims=" %%L in ('type "%%~F" ^& ^> "%%~F" rem/') do (
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        set "LEFT=!LINE:~,-4!"
        set "RIGHT=!LINE:~-4!"
        if "!RIGHT!"=="0001" set "RIGHT=0031"
        if "!RIGHT!"=="0002" set "RIGHT=0032"
        if "!RIGHT!"=="0003" set "RIGHT=0033"
        >> "%%~F" echo(!LEFT!!RIGHT!
        endlocal
    )
)

【讨论】:

    【解决方案2】:

    使用JREPL.BAT - a regular expression find/replace utility

    for %%F in (c:\test\*) do call jrepl "000(?=[123]$)" "003" /f "%%F" /o -
    

    上述代码在每行末尾查找“1”、“2”或“3”之前的“000”,并将“003”替换为“000”。

    JREPL 是纯脚本(混合批处理/JScript),可在从 XP 开始的任何 Windows 机器上本地运行 - 不需要第 3 方 exe 文件。

    【讨论】:

    • 非常紧凑!听着,如果我想以这种方式更改行:当我找到“1”时,我将字符串的“第二”字符更改为“1”,当我找到“2”时,我将“第二”字符更改为“ 2" 等...这可能吗?
    • @Matteo - 当然 - 你应该学习正则表达式;网上有很多教程。您还应该研究 JREPL 文档——它是一个强大的实用程序。我不确定如何解释您的请求,但如果您希望 0001 变为 0131,并且希望 0002 变为 0232,等等,请将 jrepl 调用更改为 call jrepl "000([123])$" "0$13$1" /f "%%F" /o -
    • 感谢 dbenham 的回答!不幸的是,我的意思是:原始行:312016101207000100000000630001->修改线3120161018120120100000000630001(在这种情况下,没有任何更改,因为前2个字符已经正确设置了)可以看到 2) 中第二个字符发生了变化,原行:31201610181207000100000000630003,修改后的行:33201610181207000100000000630003(如您所见,第二个字符在 3 中发生了变化)
    • @Matteo - 嗯,不。我的回答完全符合您在实际原始问题中的要求。您在评论中要求的内容与您在原始问题中的要求完全不同。我的回答是否符合您的要求-我想不是。它是否回答了您最初的问题 - 是的。这应该是一个程序员问答网站,所以你应该能够利用你所学的知识并开发自己的解决方案。看看您是否可以研究正则表达式并调整解决方案。如果不能,请提出一个新问题。
    • 你是对的,我有很多关于正则表达式的知识,当然你的答案是对的......我以错误的方式使用了评论部分......对于给您带来的不便,我深表歉意。
    【解决方案3】:

    非常感谢!!!!它有效!

    另外,如果您有兴趣,我已经了解了如何使我的脚本工作:
    我必须将 & VBCrlf 添加到变量中,这样脚本将搜索值 + 新行。

    strOldValue1 = "0001" & VBCrlf
    strNewValue1 = "0031" & VBCrlf
    strOldValue2 = "0002" & VBCrlf
    strNewValue2 = "0032" & VBCrlf
    strOldValue3 = "0003" & VBCrlf
    strNewValue3 = "0033" & VBCrlf
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2014-03-23
      • 2015-07-17
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      相关资源
      最近更新 更多