3

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.

2 Answers 2

3
@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

3
  • Thansk for your help. I was able to get the first part working. Get the date and process files for that date. However, the tag checking part throws an error as "unexpected do". Also, dont know how to prcess date range
    – user236215
    Nov 6, 2012 at 23:05
  • @user236215: I guess the offending part is the missing in ("%%a") on the inner FOR /F loop's header line, between %%b and do.
    – Andriy M
    Nov 6, 2012 at 23:22
  • @AndriyM: Oops! I am really absentminded these days! Thanks, Andriy, I fixed the error in the code...
    – Aacini
    Nov 6, 2012 at 23:59
3

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"
4
  • 1
    Thanks. I ran this on Windows XP but something is not right. It sets TODAY to "--"
    – user236215
    Nov 6, 2012 at 22:31
  • Actually I can just grab the date using date env variable as shown below
    – user236215
    Nov 6, 2012 at 22:41
  • You're right.The loop's processing one more line and tokens remain empty.Despite you already have a solution I've fixed the script.Thanks.
    – npocmaka
    Nov 7, 2012 at 11:56
  • thanks man for looking into this. I can confirm this works on my box too.
    – user236215
    Nov 8, 2012 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.