3
我有一個正在工作的腳本,用於替換固定寬度文件中的某些字符(從第2行開始)。如果文本文件已存在,則將文本追加到文本文件中
想避免覆蓋目標文件(如果它已經存在)。相反,如果它存在,則將行(從源文件的第2行開始)追加到目標文件的末尾。我正在努力尋找適當建議的線索。這是當前的代碼:
Dim objFSO
dim objFile
dim thisLine
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (objFSO.FileExists("C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat")) Then
Set objFile = objFSO.GetFile("C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat")
Else
WScript.Quit()
End If
If objFile.Size > 0 Then 'make sure the input file is not empty
Set inputFile = objFSO.OpenTextFile("C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat", 1) 'Replace the filename here
set outputFile = objFSO.CreateTextFile("C:\Users\Dimitar\Desktop\BPSDRC\PAYIMP.dat", TRUE) 'replace it with output filename
' first line - leave it as it is
thisLine = inputFile.ReadLine
newLine = thisLine
outputFile.WriteLine newLine
'all remaining lines - read them and replace the middle part with 18 zeroes
do while not inputFile.AtEndOfStream
thisLine = inputFile.ReadLine ' Read an entire line into a string.
'the zeroes are to fix issue N1 (payment in other amt)
'the CDF are to fix issue N2 (payment in local amt)
newLine = mid(thisLine,1,47) & "000000000000000000" & mid(thisLine,66,121) & "CDF" & mid(thisLine,190)
outputFile.WriteLine newLine
loop
inputFile.Close
outputFile.Close
objFSO.DeleteFile "C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat"
end if
考慮將此鏈接到答案文件:https://msdn.microsoft.com/en-us/library/aa265347( v = vs.60)的.aspx – mountainclimber