0

A file is dropped in a directory when another job finished processing it. The requirement is to run a batch script to check whether the file is available for today. and if available, I need to execute certain batch script. (Example: File with Naming pattern ABC-D*.txt should be available with modification date=today)

What I have figured out till now is:

FOR /F "tokens=1,2,3* Delims=/ " %%I in ('DATE /T') DO set TODAY=%%I-%%J-%%K
xcopy C:\BatchJobs\Odd*.* /L /D:%TODAY%

Running this is giving the output:

C:\BatchJobs\OddEven.txt
File cannot be copied onto itself
0 File(s)

C:\BatchJobs\OddEven.txt, showing in console is what I need. But I need to store it in some file or in some variable to be able to use this path later in my batch script. Can somebody help me in storing this file path in a variable or a file? Or suggest some other ways to achieve this goal?

2 Answers 2

3

You could use forfiles which is capable of filtering files by their last modification date, but not regarding the time:

forfiles /P "D:\ROOT" /M "ABC-D*.txt" /D +0 /C "cmd /C echo @file"

Instead of echo you can state your batch script to execute.

Sign up to request clarification or add additional context in comments.

2 Comments

Currently the file name is showing in quote. Any way to remove the quote?
forfiles expands all its path-related variables including surrounding quotes; to remove them, use a for loop, like this: forfiles /P "D:\ROOT" /M "ABC-D*.txt" /D +0 /C "cmd /C for %%I in (@file) do @echo/%%~I"
2

This code will identify "ABC-D*.txt" files last written today. Place the code below into a file such as doit.ps1 (but choose a better name). I did an attrib command on the file, but you will want to do something else.

$srcdir = 'C:\src\t\empty'

Get-ChildItem -File -Path $srcdir -Filter 'ABC-D*.txt' |
    Where-Object { $_.LastWriteTime -ge [datetime]::Today } |
    ForEach-Object {
        # Do something to the $_ file
        & attrib $_
    }

Then, you can run it from a cmd shell with:

powershell -NoProfile -File doit.ps1

2 Comments

A simpler/shorter way to get todays date at 00:00:00.000 is [datetime]::Today
or (get-date).Date ;)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.