I would like to make a program to log and compare on bat every day.

Asked 2 years ago, Updated 2 years ago, 161 views

A list of files in a storage is retrieved daily as the file name [yyyymmdd.log] log and
Compare today's and yesterday's logs, and create a program that extracts differences that are in the log yesterday and not in the log today!

What I'm having trouble adding here is
If you don't have yesterday's log, it's a program that repeats comparing the previous day's log.

Rather than yesterday, I put them in the order of new ones with the dir command and compare them to the third one if they are not compared to the second one. I think a program like this would be fine.

I would appreciate it if you could let me know how to reproduce the bat file even if you use a power shell or vbs.

add

I would like to compare it with the latest log except for today's log. I'd like to compare the latest log (today) with yesterday's log (the latest log excluding today's log), but as a condition,

  • On the first day of operation, if today's log is the only one, it will close immediately.
  • If you don't have yesterday's log for reasons such as server outage, go look for the previous day's log.

I would like to add conditions such as

The current code is

setYYYYMMDD=%DATE:/=%

Get the date and time on the same day

for/F"tokens=1*delims="%%ain('dir/b/O:DC:\Users\user\Desktop\batdevelopment\logs*.log') do set zenjitu=%%a

Obtain the Latest Log in the File

dir.txt/b>C:\Users\user\Desktop\bat development\logs\%YYYMMDD%.log

Sweep logs in today

powershell-c"compare(cat./%zenjitsu%.txt)(cat./%YYYMMDD%.log)|?{$_.SideIndicator-eq'<='}|ft-Property InputObject-hide"

Differential output on the .

At this time, I would like to add an option to the code below to get the next latest file excluding the latest file name in the folder.

for/F"tokens=1delims="%%ain('dir/b/O:DC:\Users\user\Desktop\batdevelopment\logs*.log') do set zenjitu=%%a

powershell batch-file

2022-09-30 21:44

2 Answers

I don't know if I understand the requirements correctly, but PowerShell looks like this:
Compare today's log files to the latest non-today's log files.

# Folder with log files
$logDir="C:\Users\user\Desktop\batdevelopment\logs"

# Today's Log File Name
$logFileNameToday="{0:yyyyMMdd}.log"-f[datetime]:Now

# Retrieving Today's Logs
$logToday=Get-Content "$logDir\$logFileNameToday"

# differential output
Get-ChildItem "$logDir\*" - Filter*.log-Exclude$logFileNameToday | select-Last1 | Get-Content | where {$_-notin$logToday}

Just to give you a quick look at the last line,

That's the trend.
Compare-Object is a good way, but it's a little hard to use in scripts, so we extracted it with Where-Object.

Compressed and written for use in batch files:

set LOGDIR="C:\Users\user\Desktop\batdevelopment\logs"
set TODAYLOG="%DATE:/=%.log"
powerhell-c"$t=gc\"%LOGDIR%\\%TODAYLOG%\";ls"%LOGDIR%\\*\"*.log-e%TODAYLOG%|select-l1|gc|?{$_-notin$t}"


2022-09-30 21:44

The technical difficulty is that if yesterday's log wasn't there, it would go back to the previous day's log and repeat the comparison.

I was able to make it with C# rather than with shell, so please consider it.
Install dotnet Core 3.0 and

gitdirname.bat

@echo off
pushd
rem getname.bat log directory name
cd/d% to dp0
US>dotnet run %1
popd

dotnet new console to Program.cs content

using System;

namespace getLog
{
    class program
    {
        static void Main (string[]args)
        {
            String todayDir=DateTime.Today.ToString("yyyyMMdd");
            if(args.Length==0){
                Console.WriteLine(todayDir);
                return;
            }

            string dir = args[0];
            // Look for logs until previous day's log files are found (up to 1 year)
            for(DateTime tm=DateTime.Today.AddDays(-1);tm>DateTime.Today.Add Years(-1);tm=tm.AddDays(-1)){
                String todayLogFileName = tm.ToString("yyyyMMdd") + ".log";

                if(System.IO.File.Exists(System.IO.Path.Combine(dir, todayLogFileName))){
                    Console.WriteLine(todayLogFileName);
                    return;
                }
            }

            // use today's file name if not found
            Console.WriteLine(todayDir);
            return;

        }
    }
}

Finally, put the results in the environment variable

for/F"tokens=1delims="%ain('cmd/c app directory name\gitname.bat') do setPREV_YYYMMDD=%a


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.