Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fso problem

Status
Not open for further replies.

jordan15

Technical User
Joined
Dec 5, 2006
Messages
9
Location
GB
I have this script that works fine but the only problem is every time I run the script it appends the same data to the end I would like the results to overwrite the reults currently in the second folder.


Const ForAppending = 8, ForReading = 1, ForWriting = 2
set files = objFSO.GetFolder("c:\fso\test").Files
index=1
for each file in files
if index<>5 then

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & "\" & file.name, 1, True)



sRead = objTextFile.ReadAll

objTextFile.Close



Set objTextFile = objFSO.OpenTextFile _
(strDirectory2 & strFile2, 8, True)

' Writes strText every time you run this VBScript
objTextFile.WriteLine(sRead)



objTextFile.Close
index=index+1
end if
next
'objTextFile.Close
WScript.Quit

Thanks

 
Delete the file before entering the loop.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Or if index=1 , opentextfile for writing (2), else opentextfile for appending (8).
 
Thanks I tried using Tsuji way because I also need to have objTextFile.ReadAll if it is the first file and to skip a line if any other file after that as i need to get the header of the first file. I get an error when i added the above code.

this is where I added the new line.

const ForAppending = 8, ForReading = 1, ForWriting = 2
set files = objFSO.GetFolder("c:\fso\test").Files
if index=1 , opentextfile for writing (2), else opentextfile for appending (8)

for each file in files
if index<>5 then

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & "\" & file.name, 1, True)

' Writes strText every time you run this VBScript
sRead = objTextFile.ReadAll 'file must not be empty for this to function without error


objTextFile.Close



Set objTextFile = objFSO.OpenTextFile _
(strDirectory2 & strFile2, 8, True)

' Writes strText every time you run this VBScript
objTextFile.WriteLine(sRead)
else


objTextFile.Close
index=index+1
end if
next
'objTextFile.Close
WScript.Quit
 
Tsuji only posted logic, not code. You should have made it be soemthing like this:

Code:
If index = 1 Then
   Set objTextFile = objFSO.OpenTextFile _
   strDirectory & "\" & file.name, ForWriting, True)
Else 
   Set objTextFile = objFSO.OpenTextFile _
   strDirectory & "\" & file.name, ForAppending, True)
End If

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Yes thanks I realised that after I had a look again I managed to get Tsuji method to work yesterday before I went home. But thanks guys for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top