We used to face scenarios like copying data and append to a file after
installation. e.g.: services, host, networks files.
Have written a script to do the same, but facing an issue like the data is appending on starting from last line of the previous file data and not appending on new line.
Below is the example:
File1.txt
File2.txt
Script to copy data from file2 and appending data to file1
After executing the script, the data from file2 get appended into file1, but here we see an issue like appending data starting on last line of previous file data.
Solution: Have altered the script and make it to append data starting from the new line.
Dim oShell,Programfol,FSO,strTextFile,strData,strLine
Dim arrLines,strTextFile1,strData1,finalval,sub_str,file
CONST ForReading = 1
CONST ForAppending = 8
Set oShell = CreateObject("WScript.Shell")
Programfol = oShell.ExpandEnvironmentStrings("%ProgramFiles%")
strTextFile1 = "C:\Users\Administrator\Desktop\File1.txt"
strTextFile= "C:\Users\Administrator\Desktop\File2.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(strTextFile) Then
'Open the text file to append data
strData = FSO.OpenTextFile(strTextFile,ForReading).ReadAll
strData1 = FSO.OpenTextFile(strTextFile1,ForReading).ReadAll
Set file = FSO.OpenTextFile(strTextFile1,ForAppending)
arrLines = Split(strData,vbCrLf)
file.Write(vbCrLf)
For Each strLine in arrLines
sub_str = Mid(strLine, 1)
finalval = Trim(sub_str)
If Not(Instr(strData1, finalval) > 0) Then
file.WriteLine(strLine)
End If
Next
Msgbox "Updated the words in file--> " & strTextFile1
file.Close
Else
MsgBox strTextFile & " File not found!!!"
End If
Set file = Nothing
Set FSO = Nothing
Have written a script to do the same, but facing an issue like the data is appending on starting from last line of the previous file data and not appending on new line.
Below is the example:
File1.txt
File2.txt
Script to copy data from file2 and appending data to file1
After executing the script, the data from file2 get appended into file1, but here we see an issue like appending data starting on last line of previous file data.
Solution: Have altered the script and make it to append data starting from the new line.
Dim oShell,Programfol,FSO,strTextFile,strData,strLine
Dim arrLines,strTextFile1,strData1,finalval,sub_str,file
CONST ForReading = 1
CONST ForAppending = 8
Set oShell = CreateObject("WScript.Shell")
Programfol = oShell.ExpandEnvironmentStrings("%ProgramFiles%")
strTextFile1 = "C:\Users\Administrator\Desktop\File1.txt"
strTextFile= "C:\Users\Administrator\Desktop\File2.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(strTextFile) Then
'Open the text file to append data
strData = FSO.OpenTextFile(strTextFile,ForReading).ReadAll
strData1 = FSO.OpenTextFile(strTextFile1,ForReading).ReadAll
Set file = FSO.OpenTextFile(strTextFile1,ForAppending)
arrLines = Split(strData,vbCrLf)
file.Write(vbCrLf)
For Each strLine in arrLines
sub_str = Mid(strLine, 1)
finalval = Trim(sub_str)
If Not(Instr(strData1, finalval) > 0) Then
file.WriteLine(strLine)
End If
Next
Msgbox "Updated the words in file--> " & strTextFile1
file.Close
Else
MsgBox strTextFile & " File not found!!!"
End If
Set file = Nothing
Set FSO = Nothing
OUTPUT
File1.txt
No comments:
Post a Comment