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!

VB script to append multiple text files without including Headers 1

Status
Not open for further replies.

rupali08

Programmer
Jun 12, 2010
4
US
Hi All- I need to write a VB script which can append all the text files available within a folder and finally create a single source file.For example if there are 100 text files within the Source Folder,script should create a single source file by combining all the 100 text files.This is the code I am using.

On Error Resume Next
Dim fso, folder, files, NewsFile,sFolder, objTextFile
Const ForReading = 1

Set fso = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = fso.CreateTextFile("FinalSource.txt")
currentPath = Replace(Wscript.ScriptFullName, "\" & Wscript.ScriptName, "")

sFolder = Wscript.Arguments.Item(0)
If sFolder = "" Then
sFolder = currentPath & "\Source"
'Wscript.Echo sFolder
'Wscript.Quit
End If

Set files = fso.GetFolder(sFolder).Files
Echo file.Count
For each folderIdx In files
if lcase(objFSO.getExtensionName(file.path))="txt" then
Set objTextFile = fso_OpenTextFile(folderIdx.path, ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
objOutputFile.WriteLine strText
End if
Next
objOutputFile.Close


This code working fine in creating single file.There are two headers in each text file one for Field name and other is the field data type.But the header is repeating each time means in the final text file header appeared 100 times.

Can someone please help me tweaking above code just to append data without header information.The source file should contain only one header i.e. the field name.

Here is the SAMPLE DATA
file-1
"callManagerId","callId","CallIdentifier","dateTime"
INTEGER,INTEGER,INTEGER,INTEGER
1,1,458593,45640066
1,2,794399,45640053
1,2,794393,45640037
1,4,747779,45639792

file-2
"callManagerId","callId","CallIdentifier","dateTime"
INTEGER,INTEGER,INTEGER,INTEGER
1,9,4585989,45640000
1,9,494396,45640053
1,5,394390,45640037
1,3,747779,45639792


Final Data Should Be
"callManagerId","callId","CallIdentifier","dateTime"
1,1,458593,45640066
1,2,794399,45640053
1,2,794393,45640037
1,4,747779,45639792
1,9,4585989,45640000
1,9,494396,45640053
1,5,394390,45640037
1,3,747779,45639792

Thanks.
 
For example something like this
Code:
On Error Resume Next 
Dim fso, folder, files, NewsFile,sFolder, objTextFile
Const ForReading = 1

Set fso = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = fso.CreateTextFile("FinalSource.txt")
currentPath = Replace(Wscript.ScriptFullName, "\" & Wscript.ScriptName, "")

sFolder = Wscript.Arguments.Item(0) 
If sFolder = "" Then 
  sFolder = currentPath & "\Source"
  'Wscript.Echo sFolder
  'Wscript.Quit 
End If

Set files = fso.GetFolder(sFolder).Files
Wscript.Echo "Files found: "& files.Count
file_nr = 0
For each file In files
  Wscript.Echo file.path
  file_nr = file_nr + 1
  if lcase(objFSO.getExtensionName(file.path))="txt" then
    Set objTextFile = fso.OpenTextFile(file.path, ForReading)
    line_nr = 0
    do while not objTextFile.AtEndOfStream
      'read the line from input file
      line_nr = line_nr + 1
      strText = objTextFile.ReadLine
      if (file_nr = 1 and line_nr <> 2) or (line_nr > 2) then
          ' write the line to the output file
          objOutputFile.WriteLine strText
      end if
    loop   
    objTextFile.Close
  End if
Next 
objOutputFile.Close
 
Thanks Mikrom.It worked.Is there a way I can move all the appended files to a back up folder after all data merged into final text file?

Users send 1000 files everyday.I am going to run above script end of the day,so all data will be in a single file.

I also would like to know how this code can be tweaked so that everyday it will run to add new data into the same final text file and move the user files to back up folder.

Day1:
Day1File1,Day1File2,Day1File3
After the script run at the end of day1,Finalsource file should have all the data from Day1File1,Day1File2,Day1File3 and then these three file will move to back up folder.

Day2:
Day2File1,Day2File2,Day2File3
After the script run at the end of Day2,Finalsource file should have all(day1 and day2 ) data and back up folder should contain individual files of both days
Day1File1,
Day1File2,
Day1File3,
Day2File1,
Day2File2,
Day2File3

Thanks.
 
I tried this
Code:
[COLOR=#804040][b]On[/b][/color] [COLOR=#804040][b]Error[/b][/color] [COLOR=#804040][b]Resume[/b][/color] [COLOR=#804040][b]Next[/b][/color] 
[COLOR=#804040][b]Dim[/b][/color] fso[COLOR=#804040][b],[/b][/color] folder[COLOR=#804040][b],[/b][/color] [COLOR=#a020f0]files[/color][COLOR=#804040][b],[/b][/color] NewsFile[COLOR=#804040][b],[/b][/color]sFolder[COLOR=#804040][b],[/b][/color] objTextFile
[COLOR=#804040][b]Const[/b][/color] ForReading [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color]

[COLOR=#804040][b]Set[/b][/color] fso [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]CreateObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Scripting.FileSystemObject"[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#804040][b]Set[/b][/color] objOutputFile [COLOR=#804040][b]=[/b][/color] fso[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]CreateTextFile[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"FinalSource.txt"[/color][COLOR=#804040][b])[/b][/color]
currentPath [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]Replace[/color][COLOR=#804040][b]([/b][/color]Wscript[COLOR=#804040][b].[/b][/color]ScriptFullName[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"\"[/color] [COLOR=#804040][b]&[/b][/color] Wscript[COLOR=#804040][b].[/b][/color]ScriptName[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]""[/color][COLOR=#804040][b])[/b][/color]

sFolder [COLOR=#804040][b]=[/b][/color] Wscript[COLOR=#804040][b].[/b][/color]Arguments[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]Item[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]0[/color][COLOR=#804040][b])[/b][/color] 
[COLOR=#804040][b]If[/b][/color] sFolder [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]""[/color] [COLOR=#804040][b]Then[/b][/color] 
  sFolder [COLOR=#804040][b]=[/b][/color] currentPath [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"\Source"[/color]
 [COLOR=#0000ff] 'Wscript.Echo sFolder[/color]
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]If[/b][/color]
Wscript[COLOR=#804040][b].[/b][/color]Echo [COLOR=#ff00ff]"Source Folder: "[/color] [COLOR=#804040][b]&[/b][/color] sFolder

BackupFolder [COLOR=#804040][b]=[/b][/color] Wscript[COLOR=#804040][b].[/b][/color]Arguments[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]Item[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]1[/color][COLOR=#804040][b])[/b][/color] 
[COLOR=#804040][b]If[/b][/color] BackupFolder [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]""[/color] [COLOR=#804040][b]Then[/b][/color] 
  BackupFolder [COLOR=#804040][b]=[/b][/color] currentPath [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"\Backup"[/color]
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]If[/b][/color]
Wscript[COLOR=#804040][b].[/b][/color]Echo [COLOR=#ff00ff]"Backup Folder: "[/color] [COLOR=#804040][b]&[/b][/color] BackupFolder

[COLOR=#804040][b]Set[/b][/color] [COLOR=#a020f0]files[/color] [COLOR=#804040][b]=[/b][/color] fso[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]GetFolder[/color][COLOR=#804040][b]([/b][/color]sFolder[COLOR=#804040][b]).[/b][/color][COLOR=#a020f0]Files[/color]
Wscript[COLOR=#804040][b].[/b][/color]Echo [COLOR=#ff00ff]"Number of Files found: "[/color][COLOR=#804040][b]&[/b][/color] [COLOR=#a020f0]files[/color][COLOR=#804040][b].[/b][/color][COLOR=#a020f0]Count[/color]
file_nr [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
[COLOR=#804040][b]If[/b][/color] [COLOR=#a020f0]files[/color][COLOR=#804040][b].[/b][/color][COLOR=#a020f0]Count[/color] [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color] [COLOR=#804040][b]Then[/b][/color]
  Wscript[COLOR=#804040][b].[/b][/color]Echo [COLOR=#ff00ff]"Processing Files:"[/color]
[COLOR=#804040][b]Else[/b][/color]
  Wscript[COLOR=#804040][b].[/b][/color]Echo [COLOR=#ff00ff]"Nothing to do."[/color]
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]If[/b][/color]  
[COLOR=#804040][b]For[/b][/color] [COLOR=#804040][b]each[/b][/color] file [COLOR=#804040][b]In[/b][/color] [COLOR=#a020f0]files[/color]
  Wscript[COLOR=#804040][b].[/b][/color]Echo file[COLOR=#804040][b].[/b][/color]path
  file_nr [COLOR=#804040][b]=[/b][/color] file_nr [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]if[/b][/color] [COLOR=#008080]lcase[/color][COLOR=#804040][b]([/b][/color]objFSO[COLOR=#804040][b].[/b][/color]getExtensionName[COLOR=#804040][b]([/b][/color]file[COLOR=#804040][b].[/b][/color]path[COLOR=#804040][b]))=[/b][/color][COLOR=#ff00ff]"txt"[/color] [COLOR=#804040][b]then[/b][/color]
    [COLOR=#804040][b]Set[/b][/color] objTextFile [COLOR=#804040][b]=[/b][/color] fso[COLOR=#804040][b].[/b][/color]OpenTextFile[COLOR=#804040][b]([/b][/color]file[COLOR=#804040][b].[/b][/color]path[COLOR=#804040][b],[/b][/color] ForReading[COLOR=#804040][b])[/b][/color]
    line_nr [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
    [COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color] [COLOR=#804040][b]not[/b][/color] objTextFile[COLOR=#804040][b].[/b][/color]AtEndOfStream
     [COLOR=#0000ff] 'read the line from input file[/color]
      line_nr [COLOR=#804040][b]=[/b][/color] line_nr [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
      strText [COLOR=#804040][b]=[/b][/color] objTextFile[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]ReadLine[/color]
      [COLOR=#804040][b]if[/b][/color] [COLOR=#804040][b]([/b][/color]file_nr [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color] [COLOR=#804040][b]and[/b][/color] line_nr [COLOR=#804040][b]<>[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]or[/b][/color] [COLOR=#804040][b]([/b][/color]line_nr [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]then[/b][/color]
         [COLOR=#0000ff] ' write the line to the output file[/color]
          objOutputFile[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]WriteLine[/color] strText
      [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
    [COLOR=#804040][b]loop[/b][/color]   
    objTextFile[COLOR=#804040][b].[/b][/color][COLOR=#804040][b]Close[/b][/color]
   [COLOR=#0000ff] 'Move File to Backup Folder[/color]
    destination_file [COLOR=#804040][b]=[/b][/color] BackupFolder [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"\"[/color] [COLOR=#804040][b]&[/b][/color] file[COLOR=#804040][b].[/b][/color][COLOR=#804040][b]name[/b][/color]
    [COLOR=#804040][b]If[/b][/color] fso[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]FileExists[/color][COLOR=#804040][b]([/b][/color]destination_file[COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]Then[/b][/color]
      WScript[COLOR=#804040][b].[/b][/color]Echo [COLOR=#ff00ff]"* Error: destination file already exists, cannot move !"[/color]
    [COLOR=#804040][b]Else[/b][/color]
       WScript[COLOR=#804040][b].[/b][/color]Echo [COLOR=#ff00ff]"Now moving file to Backup Folder '"[/color] [COLOR=#804040][b]&[/b][/color] BackupFolder [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"'"[/color]
       fso[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]MoveFile[/color] file[COLOR=#804040][b].[/b][/color]path[COLOR=#804040][b],[/b][/color] destination_file
    [COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]If[/b][/color]
  [COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]If[/b][/color]
[COLOR=#804040][b]Next[/b][/color] 
objOutputFile[COLOR=#804040][b].[/b][/color][COLOR=#804040][b]Close[/b][/color]
Wscript[COLOR=#804040][b].[/b][/color]Echo [COLOR=#ff00ff]"Done."[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top