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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fso loop problem 1

Status
Not open for further replies.

jordan15

Technical User
Dec 5, 2006
9
GB
Hi,

I am not sure where I am going wrong, I have written a script that opens, reads a text file then adds the contents to a text file in another folder. The problem I am having is the first folder has several text files and I want to loop through each text file and copy the contents of each one to the new folder but to one text file. e.g folder 1 has 6 text files and I want the contents of each file only copied to folder2 to a single text file. I also need file 5 to be ignored, as this file does not need to be added.

This works but only moves the first folder as I have declared the folder to look but the problem is it does not loop through the files and it does not ignore file 5.



Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile, sRead
Dim strDirectory, strFile, strText,strDirectory2, strFile2, files, file
strDirectory = "c:\fso\test"
strDirectory2 = "c:\fso2\test2"
strFile = "\fso.txt"
strFile2 = "\fso.txt"
'strText = "this should come from the txt file"

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else

WScript.Echo " The File" & strDirectory & "does not exist"
End If


set objFile = nothing
set objFolder = nothing

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

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, 1, True)

' Writes strText every time you run this VBScript
sRead = objTextFile.ReadAll
objTextFile.Close


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

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

next
objTextFile.Close
WScript.Quit


Can anyone help.

Thanks in advance
 
You ought to ask at vbs forum from the look of the script.

If this quick advice can help you get it over, let it be so.
[1] 5th is not generic or you know what you're talking about. In that case, use an index to count it.
[2] You need to close objTextFile before the next statement.
[3] The flow of the script is such that even the folder1 does not exist, you still loop for nonexisting file in nonexisting folder.
[4] you open file in the folder with a static file name, that's incorrect.
[tt]
Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile, sRead
Dim strDirectory, strFile, strText,strDirectory2, strFile2, files, file[red], index[/red]
strDirectory = "c:\fso\test"
strDirectory2 = "c:\fso2\test2"
strFile = "\fso.txt"
strFile2 = "\fso.txt"
'strText = "this should come from the txt file"

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
WScript.Echo " The File" & strDirectory & "does not exist"
[red]wscript.quit[/red]
End If


set objFile = nothing
set objFolder = nothing

Const ForAppending = 8, ForReading = 1, ForWriting = 2
set files = objFSO.GetFolder("c:\fso\test").Files
[blue]index=1[/blue]
for each file in files
[blue]if index<>5 then[/blue]
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & [red]"\" & file.name[/red], 1, True)

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


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

' Writes strText every time you run this VBScript
objTextFile.WriteLine(sRead)
[blue]objTextFile.Close
index=index+1
end if[/blue]
next
[red]'[/red]objTextFile.Close
WScript.Quit
[/tt]
If any remaining problem, bring the question to the vbs forum.
 
Thanks Tsuji
You desreve a star for that.
That works fine now, 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.

I will post this problem in the vbs forum as you suggested.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top