Prasad Amperayani
Prasad Amperayani

Reputation: 11

Windows Batch script to find files with Timestamp

I am new to Batch scripting, and I wrote the below script to search for a specific file, but I am not able to get the file that I am looking for.

I have a directory in which I have two files

  1. MGD_DJE.csv
  2. MGD_DJE_20210924_094411.csv

I am using the below batch script to find the files which have the date value attached to it, so my output should have MGD_DJE_20210924_094411.csv after I ran the below script.

cd c:\MGD

dir/b | find  "MGD_DJE_%YYYYMMDD%"  > Results.txt

I even tried without the date part (i.e. %YYYMMDD%) but no file is getting returned ,please let me know how to handle the date part while search for a particular file in the find command .

Thank you

Upvotes: 1

Views: 765

Answers (1)

lit
lit

Reputation: 16236

It would be difficult in cmd.exe to validate that the file name had numbers. But, using a PowerShell regex would easily do it. Perhaps something like the following code.

powershell -NoLogo -NoProfle -Command ^
    "Get-ChildItem -Path 'C:\MGD' -Filter 'MGD_DJE_*' |" ^
        "ForEach-Object {" ^
            "if ($_.Name -match '^MGD_DJE_\d{8}.*') { $_ }" ^
        "}"

Upvotes: 2

Related Questions