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
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