Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Windows batch: append log files based on datestamp

Hello experts,

I have the following procedure which allows me to append log files on specific folder:
@echo off
setlocal

set BaseDir=B:\EE\EE29154094\Files
set MergeFile=B:\EE\EE29154094\merge.csv

echo.filename;datemodified;content>"%MergeFile%"

for %%A in ("%BaseDir%\*.*") do (
    if /i NOT "%%~A" == "%MergeFile%" (
        for /f "tokens=*" %%B in ('type "%%~A"') do (
            echo.%%~nxA;%%~tA;%%B>>"%MergeFile%"
        )
    )
)

Open in new window


I would like to add the following requirements
-Be able to report a date stamp to append based on this date
Example:
DateStamp: 15
Append files newer or equal than 15 days.
If you have questions, please contact me.
Avatar of Qlemo
Qlemo
Flag of Germany image

@echo off
setlocal

set BaseDir=B:\EE\EE29154094\Files
set MergeFile=B:\EE\EE29154094\merge.csv

echo.filename;datemodified;content>"%MergeFile%"

set /p daysback=How many days to go back? 

forfiles /P "%BaseDir%" /d +%daysback% /c "cmd /c if NOT @isdir == TRUE  if NOT @path == \"%MergeFile%\" echo @file;@fdate @ftime" | ^
for /F "delims=; tokens=1,2" %%A in ('more /s') do ^
(for /F "tokens=*" %%T in ('type "%BaseDir%\%%~A" ') do echo.%%~A;%%B;%%T) >>"%MergeFile%"

Open in new window

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 Luis Diaz

ASKER

Thank you, I will test proposals and keep you informed.
@Bill:
I tested your proposal and I got files with the following date: 05/08/2019  13:24:00 with KeepDays=1.
When I run date I have the following: The current date is: 11/09/2019
Thank you for your help.
@Qlemo I tested your proposal and I got: ERROR: No files found with the specified search criteria.
With KeepDays: 3 and dummy files related to the following dates:
User generated image
Strange, I'm certain I tested that before on W10, but indeed FORFILES is not able to show files newer than a past date. /d +3 shows files which are changed 3 days in the future from now - that really makes no sense :-/
Thank you for your feedback.
Potential workaround:
  • Create a temp folder within merge folder
  • Transfer the files newer than past day
  • Create the merge file with the required information
  • Remove temp folder
@Qlemo, @Bill let me know what do you think.
Avatar of Bill Prew
Bill Prew

Sorry, small typo in my earlier post, I corrected in that post now.


»bp
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
Noted, Thank you. I will retest both proposals and keep you informed.
Tested and it works!
Thank you for you help!