这是我的问题:
在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
感谢您的帮助!!
答案 0 :(得分: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
)
)
答案 1 :(得分:0)
非常感谢!!!!它有效!!
另外,如果您感兴趣我已经找到了如何使我的脚本工作:
我要添加&amp; VBCrlf 到变量,这样脚本将搜索值+新行。
strOldValue1 = "0001" & VBCrlf
strNewValue1 = "0031" & VBCrlf
strOldValue2 = "0002" & VBCrlf
strNewValue2 = "0032" & VBCrlf
strOldValue3 = "0003" & VBCrlf
strNewValue3 = "0033" & VBCrlf
答案 2 :(得分:0)
使用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),可以在任何Windows机器上从XP开始本地运行 - 无需第三方exe文件。