Dim fso, folderObject, filesObject, fileObject
Dim topFolderName
Dim fileCounter
topFolderName = InputBox("Enter path to CSV files to update including trailling \.")
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(topFolderName) Then
Set folderObject = fso.GetFolder(topFolderName)
Set filesCollection = folderObject.Files
For Each file In filesCollection
If UCASE(Right(file.Name,3)) = "CSV" Then
removeLines (topFolderName & file.Name)
fileCounter = fileCounter + 1
End If
Next
Else
MsgBox "Source folder not found.", vbExclamation, "Error"
End If
MsgBox fileCounter & " files processed.", vbOKOnly + vbInformation, "Complete"
Set fso = Nothing
Set folderObject = Nothing
Set filesCollection = Nothing
Private Function removeLines(ByVal sFileToOpen)
Dim fsoObject
Dim textFileReadObject
Dim textFileWriteObject
Dim lineCounter
Set fsoObject = CreateObject("Scripting.FileSystemObject")
Set textFileReadObject = fsoObject.OpenTextFile(sFileToOpen)
Set textFileWriteObject = fsoObject.CreateTextFile(sFileToOpen & "_", True)
lineCounter = 0
Do While Not textFileReadObject.AtEndOfStream
lineCounter = lineCounter + 1
sReadLine = textFileReadObject.ReadLine
Select Case lineCounter
Case 1, 2, 4
' The lines above will be skipped - modify them to get desiredresults
Case Else
textFileWriteObject.WriteLine (sReadLine)
End Select
Loop
Set fsoObject = Nothing
Set textfileObject = Nothing
Set textFileReadObject = Nothing
Set textFileWriteObject = Nothing
End Function
Imports System
Imports System.IO
Module Module1
Dim oFile As System.IO.File
Dim arrNewFile As New ArrayList
Dim files() As String
Sub Main(ByVal ParamArray Args() As String)
Dim oRead As System.IO.StreamReader
Dim iC As Integer
ChDir("C:\Folder") 'Change to working Directory e.g "C:\Folder"
files = System.IO.Directory.GetFiles(".", "*.csv*") 'Use GetFiles to add all CSV files to 'files' string array
iC = 0
Do Until iC = files.Count 'Loop until files.count is meet
oRead = oFile.OpenText("C:\Folder" & files(iC)) 'Select files per String array
While oRead.Peek <> -1
arrNewFile.Add(oRead.ReadLine()) 'Loops through current file adding items to array list (arrNewFile)
End While
oRead.Close() 'Close StreamReader (file is locked/in use otherwise)
'Remove unneed lines from arrNewFile
arrNewFile.RemoveAt(0)
arrNewFile.RemoveAt(0)
arrNewFile.RemoveAt(2)
Kill("C:\Folder" & files(iC)) 'Delete starting file
Write_Delimited("C:\Folder" & files(iC)) 'write new replacement files based on array
iC += 1 'Increase count by 1
Loop
End Sub
Private Sub Write_Delimited(ByVal strNewFile)
Dim iC As Integer
iC = 0
Dim objWriter As New System.IO.StreamWriter(strNewFile, True) 'Recreate blank file
Do Until ic = arrNewFile.Count
objWriter.WriteLine(arrNewFile(iC)) 'Write array list into blank file
iC += 1
Loop
objWriter.Close()
End Sub
End Module
SETLOCAL ENABLEDELAYEDEXPANSION
SET COUNT=1
FOR /F "tokens=*" %%A IN (%1) DO (
IF !COUNT!==3 (
ECHO %%A
)
IF !COUNT! GTR 4 (
ECHO %%A
)
SET /A COUNT=COUNT+1
)
Code:SETLOCAL ENABLEDELAYEDEXPANSION SET COUNT=1 FOR /F "tokens=*" %%A IN (%1) DO ( IF !COUNT!==3 ( ECHO %%A ) IF !COUNT! GTR 4 ( ECHO %%A ) SET /A COUNT=COUNT+1 )
Just redirect it, or add redirection to the echo statements.