We help IT Professionals succeed at work.
Get Started

VBS copy text to new file, but remove header

totalimpact
totalimpact asked
on
1,652 Views
Last Modified: 2012-05-11
I have a group of csv files that I am merging into a "master" file, but before merging them to the master file, I need to remove the header from the copied text.

My attempt is around lines 77-90, but it is not working.
 
' Compare temp files to archived files, merge new data to master file, then archive temp files

Option Explicit
'variables: -----------------------------------------------
Dim archived_Path, temp_path, sorted_logs_path, Master_file_path, Log_File, ftp_days, ftp_adr, ftp_user, ftp_pass, ftp_files
Dim intAnswer, datFetchDate, strFetchDate, strDestDir, intDays 
Dim oFTPScriptFSO, oFTPScriptShell, sFTPScript, sFTPTemp, sFTPTempFile, sFTPResults, sResults, fFTPScript, fFTPResults
Dim sOriginalWorkingDirectory, sRemotePath, sLocalPath 
Dim objFSO, objFSOText, objFile, objTextFile, objHash
Dim MyArray, IP, Value 
Dim i,strNextLine,strHeader
Dim bNeedToWriteHeader
Dim objFolder, Subfolder, objsubFolder, colFiles, objFiletemp, objMaster, strlinetemp, Lastline_temp 
Dim objFilear, Lastline_ar, subfolder_ar, strlinear, objfilear_path, file_ar_readall, AShell
Dim myIdx

archived_Path="c:\test\arc\" 'archived folder path
temp_path="c:\test\temp\" 'tempfolderpath for ftp downloads
sorted_logs_path="c:\test\sortedlogs\"
Master_file_path="c:\test\masterfile.txt" 'master file path - this is really a temp file.
'end variables: -------------------------------------------

intAnswer = _
    Msgbox("Do you want to process the logs?", _
        vbOkCancel, "Process New Logs")

If intAnswer = vbCancel Then
	WScript.Quit
Else
    End If
		
Set objFSO = CreateObject("Scripting.FileSystemObject")
'----------- Sort new files in temp folder, merge data into master file, and archive processed files --------------

'Set objFSO = CreateObject("Scripting.FileSystemObject")

If objfso.FileExists(Master_file_path) = False Then
    objfso.CreateTextFile(Master_file_path)
End If

Set objFolder = objFSO.GetFolder(temp_path)
'WScript.Echo objFolder.Path

For Each Subfolder In objFolder.SubFolders
'    WScript.Echo Subfolder.Path & " - " & temp_path & " - " & archived_path
    subfolder_ar=Replace(LCase(Subfolder.Path),LCase(temp_path),LCase(archived_Path))
    
'    WScript.Echo "Archive Subfolder =" & subfolder_ar
    If objfso.FolderExists(subfolder_ar) Then
        Set objsubFolder = objFSO.GetFolder(Subfolder.Path)
        
        Set colFiles = objsubFolder.Files
        For Each objFile In colFiles
'            WScript.Echo objfile.Path
            Set objFiletemp = objFSO.OpenTextFile(objfile.Path, 1)
            Do Until objFiletemp.AtEndOfStream
                strlinetemp=objFiletemp.ReadLine
                Lastline_temp = strlinetemp
            Loop
            objFiletemp.Close
            
            objfilear_path=Replace(LCase(objfile.Path),LCase(temp_path),LCase(archived_Path))
'            WScript.Echo objfilear_path
            If objfso.FileExists(objfilear_path) Then
                Set objFilear = objFSO.OpenTextFile(objfilear_path, 1)
                Do Until objFilear.AtEndOfStream
                    strlinear=objFilear.ReadLine
                    'WScript.Echo strlinear
                    Lastline_ar = strlinear
                Loop
                objFilear.Close
            End If
'            WScript.Echo objfso.FileExists(objfilear_path)
            If (StrComp(Lastline_temp,Lastline_ar)<>0) Or (objfso.FileExists(objfilear_path)=False)  Then
'                WScript.Echo objfile.Path & " - in if strcomp"
                Set objFiletemp = objFSO.OpenTextFile(objfile.Path, 1)
'-------------	Code to remove firstline before write to Master
	  Set objMaster=objFSO.OpenTextFile(Master_file_path, 8)
	  myIdx = 0
                Do Until objFiletemp.AtEndOfStream
	      myIdx = myIdx + 1
                    strlinetemp=objFiletemp.ReadLine
	      If myIdx > 1 Then
		objMaster.WriteLine strlinetemp
	      End If
                Loop
                objFiletemp.Close
                Err.Clear
                objMaster.Close
'-------------	end new code to remove first line
                If Err.Number=0 Then
'                    WScript.Echo "in move file 1 - " & Replace(LCase(objfilear_path),LCase(objfile.Name),"")
                    Err.Clear
                    objfso.copyFile objfile.Path,Replace(LCase(objfilear_path),LCase(objfile.Name),""),True
                    objfso.DeleteFile objfile.Path
                    If Err.Number<>0 Then
'                        WScript.Echo Err.Number & Err.Description
                    End If
                End If
            Else
                objfso.DeleteFile(objfile.Path)
'                WScript.Echo "in delete file 2"
            End If    
        Next
    Else
        Set objsubFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objsubFolder.Files
        For Each objFile In colFiles
'            WScript.Echo objfile.Path & " - in move folder copy file content"
            Set objFiletemp = objFSO.OpenTextFile(objfile.Path, 1)
            file_ar_readall=objfiletemp.ReadAll
            objFiletemp.Close
            Set objMaster=objFSO.OpenTextFile(Master_file_path, 8)
            objMaster.Write file_ar_readall
            objMaster.Close
        Next    
        objfso.MoveFolder Subfolder.Path,subfolder_ar
'        WScript.Echo "in move folder 3"
    End If
Next

Set objFolder = objFSO.GetFolder(temp_path)

For Each Subfolder In objFolder.SubFolders
		If subfolder.Files.Count = 0 And subfolder.SubFolders.Count = 0 Then
'    If subfolder.Files.Count = 0 Then
'        WScript.Echo "Delete folder - " & subfolder.Path
        subfolder.Delete
    End If
Next
WScript.Quit

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE