Link to home
Start Free TrialLog in
Avatar of bfuchs
bfuchsFlag for United States of America

asked on

Add date created to the Dos combine file command.

Hi Experts,

I have the following command to combine multiple CSV files into one.

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

Open in new window

Would like to add two columns, file name and/or date created (of each file) to the output file.

Thanks
Avatar of Qlemo
Qlemo
Flag of Germany image

That requires to add the new columns to each line - do you really want to do that?
As it seems, your CSV files don't contain CSV headers?
Avatar of Bill Prew
Bill Prew

Here's a basic approach using a BAT script to get you started, it adds the file name, and date stamp in the first two columns of each output line.  Keep in mind it will run a lot slower since we now have to process each line one at a time, rather than the whole CSV file at once.

@echo off
setlocal

rem Define output file path
set OutputFile=%CD%\output.csv

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

rem Loop through all CSV files in this folder and child folders
for /R %%F in (*.csv) do (
    rem Skip if it is the output file
    if /i "%%F" NEQ "%OutputFile%" (
        rem Process this file
        call :AppendFile "%%F"
    )
)

rem Exit script
exit /b

:AppendFile [file-path]
    rem Get access to file attributes (date modified, name)
    for %%a in ("%~1") do (
        rem Extract just the date from file date/time
        for /f "tokens=1" %%b in ("%%~ta") do (
            rem Append each line to ouput, adding file name and date in first two columns
            for /f "tokens=*" %%c in ('type "%~1"') do (
                echo %%~nxa,%%~b,%%c>>"%OutputFile%"
            )
        ) 
    )
    exit /b

Open in new window


»bp
Avatar of bfuchs

ASKER

Hi Experts,

That requires to add the new columns to each line - do you really want to do that?
Yes,
As it seems, your CSV files don't contain CSV headers?
It does but I can ignore them (filter out).

@Bill,
I ran yours but it did mess up the headers/columns.
Also did you find the requested columns.
attaching a sample file.

Thanks

ENTOUT_613_SchChanges_20201013064512.zip


I ran yours but it did mess up the headers/columns.
Can you be more specific, what was right, and what was wrong in the output.

I understand that the header row would have been treated as data, that was based on your original question post which didn't state there was a header row.  In the code you posted though, using the TYPE command, you would have had a new header row in the output file for each input file found, right?  Is that what you want, or just one header row as the first line of the output file?

It would be helpful if you could post a corresponding example of what you want the output file to look like for that input sample you just posted, that would eliminate some confusion quicker.


»bp
Avatar of bfuchs

ASKER

It would be helpful if you could post a corresponding example of what you want the output file to look like for that input sample you just posted, that would eliminate some confusion quicker.
Okay I am attaching now 4 files, two sample files and two output files, one containing the original output and one demonstrating what I am looking for.

Thanks

ENTOUT_613_SchChanges_20200930063019.zip
In the two output sample files you posted, there were a number of additional differences to the prior output file other than the to new columns.  It looked like most of the date columns were formatted differently.  Is that something you also expected to happen in the new script, or just an unintentional difference in the way you prepared the files?

User generated image

»bp
Avatar of bfuchs

ASKER

Not sure why there are differences, I just made a copy of Output file and added the two columns.
However you can ignore them.
Thanks
Okay, and for the new date column, you want the created date, not the modified date?


»bp
Avatar of bfuchs

ASKER

It doesn't matter as they are read only files.

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bfuchs

ASKER

Thank you Bill!
Avatar of bfuchs

ASKER

BTW, what does it entail to remove the headers from the output file, should only contain one?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bfuchs

ASKER

Thanks Bill!
Welcome, glad that helped.

»bp
Avatar of bfuchs

ASKER

Just a question...

Would you know why is the created date column (along with other date columns) not being recognized in Access as valid dates?

See attached.

Untitled.png

Thanks
I'm pretty sure that is because the time component in the date/time stamp we added is in 12 hour format, so like:

5:00:00 PM

when Access wants it to be in 24 hour format, like:

17:00:00


»bp
One way around that could be to open the CSV in Excel, which I think should be okay with the 12 hour time format, and then format tose date / time columns into 24 hour time format.


»bp
Avatar of bfuchs

ASKER

Its okay I am handling it with CDate() function and seems to work fine.
Thank you.

Great!