【发布时间】: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
感谢您的帮助!!
【问题讨论】:
-
最后一部分是否总是在这些:
0001、0002、0003?
标签: batch-file vbscript