[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

loop files in a folder then removeblak lines

what i want to be able to do is loop through all the csv files in a folder then remove the blank lines.

i've tried the followin gbut i get an error.

any help would be Appreciated
Dim sFolder, getDatFileName
        Const ForReading = 1
        Const ForWriting = 2

        sFolder = "\\Server\Data\ArchivePath\"

        Dim oFSO, oFolder, oFile
        oFSO = CreateObject("Scripting.FileSystemObject")
        objFSO = CreateObject("Scripting.FileSystemObject")
        objFile = objFSO.OpenTextFile(getDatFileName, ForReading)


        If oFSO.FolderExists(sFolder) Then
            oFolder = oFSO.GetFolder(sFolder)
            For Each oFile In oFolder.Files
                If LCase(Right(oFile.Name, 4)) = ".csv" Then
                    getDatFileName = oFile.Name
                    MsgBox(getDatFileName)

                    Do Until objFile.AtEndOfStream
                        strLine = objFile.Readline
                        strLine = Trim(strLine)
                        If Len(strLine) > 0 Then
                            strNewContents = strNewContents & strLine & vbCrLf
                        End If
                    Loop
                End If
            Next
        End If

        objFile.Close()

        objFile = objFSO.OpenTextFile(getDatFileName, ForWriting)
        objFile.Write(strNewContents)
        objFile.Close()

Open in new window

0
aneilg
Asked:
aneilg
  • 2
1 Solution
 
Muhammad KhanManager, ITCommented:
just a piece of code here.. just a piece there.. .


Dim sFolder, getDatFileName
        Const ForReading = 1
        Const ForWriting = 2
        sFolder = "c:\workshop"
        Dim oFSO, oFolder, oFile
        set oFSO = CreateObject("Scripting.FileSystemObject")
        set objFSO = CreateObject("Scripting.FileSystemObject")
        If oFSO.FolderExists(sFolder) Then
            set oFolder = oFSO.GetFolder(sFolder)
            For Each oFile In oFolder.Files
                If LCase(Right(oFile.Name, 4)) = ".csv" Then
                    getDatFileName = oFile.Name
                    MsgBox(getDatFileName)
		    
		    set objFile=ofso.opentextfile(getDatfilename,1)
                    Do Until objFile.AtEndOfStream
                        strLine = objFile.Readline
                        strLine = Trim(strLine)
                        If Len(strLine) > 0 Then
                            strNewContents = strNewContents & strLine & vbCrLf
			 
                        End If
                    Loop
                    objFile.close
		    msgbox strnewcontents
                    set objFile = objFSO.OpenTextFile(getDatFileName, ForWriting)
        	    objFile.Write(strNewContents)
        	    objFile.Close()
                    strnewcontents=""
                End If
            Next
        End If

        objFile.Close()

       ' set objFile = objFSO.OpenTextFile(getDatFileName, ForWriting)
        'objFile.Write(strNewContents)
        'objFile.Close()

Open in new window

0
 
aneilgAuthor Commented:
thanks.
0
 
aneilgAuthor Commented:
A Star. thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now