Avatar of bfuchs
bfuchs
Flag for United States of America asked on

How to concatenate all files from one folder into one ignoring headers #2

Hi Experts,

I have this question after resolving the following

https://www.experts-exchange.com/questions/29109077/How-to-concatenate-all-files-from-one-folder-into-one-ignoring-headers.html?anchorAnswerId=42623555#a42623555

How can I modify the command to

1- Ignore all headers
2- include only files created in specific date range
3- include only files with specific names, like "*PatSched* and "*PatChanges*"

Thanks in advance.
System ProgrammingMicrosoft DOSWindows 7Scripting LanguagesVisual Basic Classic

Avatar of undefined
Last Comment
bfuchs

8/22/2022 - Mon
Roy Cox

One of my old colleagues on a different Forum wrote this several years ago. I haven't used it but Will's code was good. Test it on a copy of your database.

Sub Import_multiple_csv_files()
     'Modified from WillR - www.willr.info (December 2004)
     
    Const strPath As String = "C:\Addresspoint\" 'Directory Path
    Dim strFile As String 'Filename
    Dim strFileList() As String 'File  Array
    Dim intFile As Integer 'File Number
     
     'Loop through the folder & build file list
    strFile = Dir(strPath & "*.csv")
    While strFile <> ""
         'add files to the list
        intFile = intFile + 1
        ReDim Preserve strFileList(1 To intFile)
        strFileList(intFile) = strFile
        strFile = Dir()
    Wend
     'see if any files were found
    If intFile = 0 Then
         MsgBox "No files found"
        Exit Sub
    End If
     'cycle through the list of files &  import to Access
     'creating a new table called MyTable
    For intFile = 1 To UBound(strFileList)
        DoCmd.TransferText acImportDelimi, , _
        "Test", strPath & strFileList(intFile)
         'Check out the TransferSpreadsheet options in the Access
         'Visual Basic Help file for a full description & list of
         'optional settings
    Next
    MsgBox UBound(strFileList) & " Files were Imported"
End Sub

Open in new window

Bill Prew

1- Ignore all headers
How are "headers" identified, is it always just the first line of every file, or is there some other pattern?
2- include only files created in specific date range
Please give some examples of what this selection would be. DO you just want a from and a to date, or a certain number of days, etc?
3- include only files with specific names, like "*PatSched* and "*PatChanges*"
All files will be in the same base folder? And only files matching one or more normal file patterns will be selected?


»bp
Professor J

we do not need to reinvent the wheel.  MVP fellow Ron De Bruin has built an excellent free Excel Add-in that can do what you have described.

rdbmerge.png
you can download the add-in for free from here RDBMerge, Excel Merge Add-in for Excel for Windows

You can use the parameters in the add-in to select which files and to ignore the header you set the range to start from A2.
Your help has saved me hundreds of hours of internet surfing.
fblack61
bfuchs

ASKER
@ProfessorJimJam,
This is something I will run on win task scheduler on a daily bases, do they offer such an option?

@Bill,
How are "headers" identified, is it always just the first line of every file, or is there some other pattern?
Always the 1st line.
Please give some examples of what this selection would be. DO you just want a from and a to date, or a certain number of days, etc?
Would start with files created in the last 24 hours, with option to modify..
All files will be in the same base folder? And only files matching one or more normal file patterns will be selected?
Yes.
What is considered abnormal?

@Roy,
Will this (besides do the concatenation) also address those 3 issues?
Don't see option for date created for example?

@All,
Forgot to mention, I need them concatenated in order they were created.


Thanks,
Ben
Bill Prew

Okay, so the list looks like this now.

1- Ignore all headers
2- include only files created in specific date range
3- include only files with specific names, like "*PatSched* and "*PatChanges*"
4- concatenate files in date/time created order, oldest to newest

1, 3, and 4 can easily be done with small mods to the BAT script you had. And with the use of a small VBS helper routine it could handle 2 as well.  BAT is not good at all with date/time comparisons...

Is BAT acceptable?

Of course Powershell could do it too, but not sure your comfort level there...


»bp
bfuchs

ASKER
Is BAT acceptable?
Yes.

Thanks,
Ben
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
bfuchs

ASKER
@Bill,
I saved as the following
@echo off
setlocal EnableDelayedExpansion

rem Set path to folder where files to process exist
set BaseDir=H:\FTP

rem Set name for merged output file
set OutputFile=merged.txt

rem Get path to folder where this BAT script exists
set ScriptPath=%~dp0

rem Delete output file if it exists
if exist "%BaseDir%\%OutputFile%" del "%BaseDir%\%OutputFile%"

rem Process all matching files in base folder, sorting by create date/time

for /f "tokens=*" %%F in ('dir /b /a-d /od /tc "%BaseDir%\*SchChanges*"') do (

    rem For each file, call VBS script to calculate created age in hours from now
    set Fileage=
    for /f "tokens=*" %%A in ('cscript //nologo "%ScriptPath%\EE29116756.vbs" "%BaseDir%\%%~F"') do (
        set Fileage=%%A
    )

    rem If we got it's age, see if it meets criteria, if so append to output file skipping first line
    if defined FileAge (
        if !FileAge! LEQ 24 (
            more +1 "%BaseDir%\%%~F" >> "%BaseDir%\%OutputFile%"
        )
    )
)

Open in new window

Saved both under same folder as those files exists (see attached), but nothing gets created.
or perhaps it does, but i don't know where to look for it...?

Thanks,
Ben
Untitled.png
Bill Prew

Well, for one thing, since you changed the name of the VBS script (which is fine), you need to reflect the new name on this line:

    for /f "tokens=*" %%A in ('cscript //nologo "%ScriptPath%\EE29116756.vbs" "%BaseDir%\%%~F"') do (

Open in new window


The output file will be in the same folder as the files being merged.


»bp
bfuchs

ASKER
Thank you!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Bill Prew

Welcome, hope that is useful.


»bp
bfuchs

ASKER
Hi experts,
Any idea how to exclude the output file itself when using the following to concatenate (so I dont end up with duplicate data)?
for /R %F in (*.csv) do type "%F" >> output.csv

Open in new window

Thanks,
Ben
Bill Prew

Try this:

for /R %F in (*.csv) do (if /i "%F" NEQ "%CD%\output.csv" (type "%F">> output.csv))

Open in new window


»bp
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
bfuchs

ASKER
Thanks a million Bill!