Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch script for processing files for a date in name

Tags:

batch-file

cmd

Suppose my directory contains these files

dir C:\data\
Statistics_A-1-1_2012-10-21 00_10_19.csv
Statistics_B-1-1_2012-10-21 00_12_01.csv

How do I write a script in batch that gets today's date and grabs all the files with that date in it. If the files are missing tag A-1-1 or tag B-1-1, it should print an error for the missing file for that tag. Ideally, I would like to provide a date range as argument to the script. If not provided, it defaults to today's date

I am used to bash and new to windows programming. I also don't have any scripting languages such as perl, python, etc available. Any help appreciated. Cheers.

like image 348
user236215 Avatar asked Jan 20 '26 03:01

user236215


2 Answers

@echo off
for /F "tokens=1-3 delims=/" %%a in ("%date%") do set today=%%c-%%a-%%b
for %%a in (*%today%*.csv) do (
   for /F "tokens=2 delims=_ " %%b in ("%%a") do (
      if %%b equ %today% (
         echo %%a       ERROR: Missing tab
      ) else (
         echo %%a
      )
   )
)

You may process a range of dates using Julian Day Numbers this way:

set startJDN=Julian Day Number of start of range
set endJDN=Julian Day Number of end of range
for %%a in (*%today%*.csv) do (
   set fileJDN=Julian Day Number of file %%a
   if !fileJDN! geq %startJDN% if !fileJDN! leq %endJDN% (
      echo The file is in range, process it...
   )
)

See this post: Bat file for moving files

like image 118
Aacini Avatar answered Jan 21 '26 16:01

Aacini


The easiest way to get the date parts is with WMIC.(As you did't mentioned what you want to do with files here are only listed):

EDITED VERSION:

   @echo off
    FOR /F "skip=1 eol=D tokens=1,2,3 delims= " %%A IN ('WMIC Path Win32_LocalTime Get Year^,Month^,Day /Format:table') DO (
            SET TODAY=%%A-%%B-%%C
            setlocal ENABLEDELAYEDEXPANSION
            if [!TODAY!] NEQ [--] (
                goto :(endOfLoop^)
            )
            endlocal

    )
    :(endOfLoop^)
    echo %TODAY%

        DIR /B "Statistics_A-1-1_%TODAY% *.csv"
        DIR /B "Statistics_B-1-1_%TODAY% *.csv"
like image 30
npocmaka Avatar answered Jan 21 '26 16:01

npocmaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!