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

Copy/Append contents

Status
Not open for further replies.

jessicatech

Programmer
Oct 24, 2006
19
MY
Hello,

I need to automate the following, using scripting language (VBSCRIPT):

Copy all the contents of text files in a folder and paste all the contents to a text file (PROCESS.TXT).

Please advise and Thank you.
 
You don't need to make process.xxx to start with. It will create itself. At the start, there is not process.xxx. Maybe you can even add
[tt]del process.xxx [/tt]
if that annoys you.

If the singleline.xxx is properly made, it should get the desired result if you already set up the testing environment like you've shown.
 
Hi tsuji,

<[1] Make a single line (just a carriage return) file>
Can you advise how the above file with the carriage return is done? I opened up a txt file and enter press the Enter key to go to the next line. Then saved and name it as singleline.xxx.

Is the above correct?

Please advise and Thank you.

 
I am disappointed of the question! Try this to make it fully done automatically. (No need then to create manually the singleline.xxx)
[tt]
c:
cd c:\suppin
del process.txt
echo.>singleline.xxx
for %%a in (*.txt) do copy /b process.xxx+%%a+singleline.xxx process.xxx
del singleline.xxx
rename process.xxx process.txt
[/tt]
 
I don't know if you got your problem fixed but I pieced together a script from bits of code I commonly use.

The Class is an example I pulled from another web site some time ago and has been quite handy.

You will need to change the path to reflect where your files are.

This script has MANY cool examples for you also.

Class, Functions, File system objects, and WMI query in one script.

I hope you can find a use for this.[afro2]

Code:
' VB Script Document
option explicit

Dim objFSO, objFolder, strFolder, StrFileName, objFile, args, strComputer
Dim objWMIService, colFiles, objTextFile, objTextFile1, strTXT, objCreateTF
Dim CT, s, r

Set CT = new ClsTS

strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _
  ExecQuery("Select * from CIM_DataFile where Path = [highlight]'\\Path\\to\\Your\\Files\\'[/highlight] And Extension = 'txt'")
For Each objFile in colFiles
'		WScript.Echo objFile.Name

		s = CT.ReadFile(objFile.Name)
		WScript.Echo s		
		r = CT.AppendToFile("C:\Process.txt", s & vbcrlf)
Next
Set CT = Nothing
WScript.Quit

'----------------------------------------------------
Class ClsTS

Private FSO

 Private Sub Class_Initialize()
    Set FSO = CreateObject("Scripting.FileSystemObject") 
 End Sub
          
 Private Sub Class_Terminate()
    Set FSO = Nothing
 End Sub
 
'------ Read text file. Returns file text. --------------------
'--Ex.: s = Cls.ReadFile("C:\File1.txt")
Public Function ReadFile(sFilePath)
Dim TS
  If FSO.FileExists(sFilePath) = False Then
     ReadFile = ""
     Exit Function
  End If
  
 Set TS = FSO.OpenTextFile(sFilePath, 1)
    ReadFile = TS.ReadAll
 Set TS = Nothing   

End Function
  
'------ Replace text file. sFilePath is path of file. sText is text of new file.
'-- This Sub will Write the new file and Set its attributes to whatever they
'-- were originally.
'-- ex.: Cls.Replacefile "C:\file1.txt", s

Public Sub ReplaceFile(sFilePath, sText)
Dim Atr, oFil, TS
     '--confirm valid path:
   If FSO.GetParentFolderName(sFilePath) = "" Then Exit Sub
   
      '--Set attributes to 0 and delete:
    If FSO.FileExists(sFilePath) Then
         Set oFil = FSO.GetFile(sFilePath)
            Atr = oFil.Attributes
            oFil.Attributes = 0
         Set oFil = Nothing
           FSO.DeleteFile sFilePath, True
    End If     
    
     Set TS = FSO.CreateTextFile(sFilePath, True)
       TS.Write sText
       TS.Close
     Set TS = Nothing
     
     Set oFil = FSO.GetFile(sFilePath)
        oFil.Attributes = Atr
     Set oFil = Nothing
 End Sub 
 
'---------- Append text to text file -----------------
 '-- Adds text to the End of an existing text file.
 '-- ex.: r = Cls.AppendToFile("C:\file1.txt", s)  '-- returns -1 If file does Not exist.  
 
Public Function AppendToFile(sFilePath, sText)  
Dim Atr, oFil, TS

           '--confirm file exists:
    If FSO.FileExists(sFilePath) = False Then
        AppendToFile = -1
        Exit Function
    End If
    
          '--remove Read-only If necessary:
     Set oFil = FSO.GetFile(sFilePath)
        Atr = oFil.Attributes
        oFil.Attributes = 0
    Set oFil = Nothing
    
    Set TS = FSO.OpenTextFile(sFilePath, 8)
       TS.Write sText
       TS.Close
    Set TS = Nothing
    
    Set oFil = FSO.GetFile(sFilePath)
        oFil.Attributes = Atr
    Set oFil = Nothing
  AppendToFile = 0
      
End Function

End Class

Thanks

John Fuhrman
Titan Global Services
 
HI sparkbyte,

Thank you for the code. I will try it out. Do you know how can I execute FTP commands in VBScript?

The FTP commands that I wanna include are as follow:
OPEN
FTP 127.0.0.1
PUT PROCESS.TXT
BYE

After I have FTP the IP address, it will ask me to enter the USER ID and PASSWORD. Instead of storing them in the VBScript, how do I make it promptable to the user?

Thank you.
 
Hello,

Can anyone tell me why when I merge files contents, there's always the square sign at the beginning and end of file?

Please advise and Thank you.
 
You might try something like this... substituting your file names and folder locations.

Code:
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\appendingfolder")
Set colFiles = objFolder.Files

'Create a new file to place the contents
Set objResultFile = objFSO.CreateTextFile("C:\appendingfolder\results\myNewFile.txt")
objResultFile.Close

' Loop through the files in the collection, opening them and reading the contents
For Each fileName In colFiles
	Set objTextFile = objFSO.OpenTextFile(fileName, ForReading)
	strContents = strContents & trim(objTextFile.ReadAll) & VbCrLf
    objTextFile.Close
Next 

Set objResultFile = objFSO.OpenTextFile("C:\appendingfolder\results\myNewFile.txt", ForWriting)

objResultFile.Write strContents
objResultFile.Close

I created a folder and put three text files in it. Each text file contained one line of text. I suppose they could contain more than one line, but for this test I just had one line. I also made a folder for the resulting text file to land in.

strebor
 
Hi strebor,
The code is great! It combines the files content properly. How can change the code to only pick up TEXT files (*.txt) in the C:\appendingfolder , but excluding the myNewFile.txt result file, which resides in the same folder as the rest of the Text files?

Hi sparkbyte,
Thanks for pointing the URL.

Please advise and Thank you.
 
Hi strebor,

After the VBS run, the myNewFile.txt result file will have an extra empty line at the end of the file. How can I make it to stop the cursor at the end of the last content line?

Please advise and Thank you.
 
Hi jessicatech -

I think that the little square at the end of the line is an end of line marker. I looked at the results with a hex editor and this new file ends with hex 0D. Notepad doesn't know how to display hex 0D, so it places a generic marker there, the little square. The other lines, without the little square end with hex 0D 0A. Notepad knows how to handle this, it is a end of line, new line combination, in typewriter lingo, a carrage return, line feed. The line has to have an end marker... the hex 0D character.

Code:
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\appendingfolder")
Set colFiles = objFolder.Files

'Create a new file to place the contents
Set objResultFile = objFSO.CreateTextFile("C:\appendingfolder\results\myNewFile.txt")
objResultFile.Close

' Loop through the files in the collection, opening them and reading the contents
For Each fileName In colFiles
	If Right(fileName, 4) = ".txt" Then 
	    Set objTextFile = objFSO.OpenTextFile(fileName, ForReading)
	    strContents = strContents & trim(objTextFile.ReadAll) & VbCrLf 
	    objTextFile.Close
    End If
    
Next 
strContents = Left(strContents, len(strContents)-1)
Set objResultFile = objFSO.OpenTextFile("C:\appendingfolder\results\myNewFile.txt", ForWriting)

objResultFile.Write strContents
objResultFile.Close

I put the result file in a separate folder so that there would be no ambiguity with respect to which files it would use to build the result. You could experiment with various strategies to make it be in the same folder. The simplest would be to use a different file ext for the result, such as .log ? Notepad can be used to view log files.

Hope this helps.

strebor
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top