0

I would like to write a batch script which will poll a windows directory for a certain time limit and pick a file as soon as it is placed in the directory. It will also timeout after a certain time if the file is not placed in that directory within that time frame.

I would also like to parse the xml file and check for a status.

Endoro
  • 37,015
  • 8
  • 50
  • 63
user2593173
  • 317
  • 1
  • 5
  • 14
  • You need a real programming language for this. Batch files aren't going to get the job done. You could probably get by with a PowerShell script. – Robert Harvey Jul 17 '13 at 21:12
  • Use Powershell or `VBS`. – Endoro Jul 17 '13 at 21:48
  • [VBScript example](http://stackoverflow.com/a/16119815/1630171) for a folder monitor. – Ansgar Wiechers Jul 17 '13 at 22:55
  • It's possible, depending on your definitions and clarification. "Time limits" don't appear in windows directories, AFAIAA. How are you specifying the "certain time", "time limit", "status" and 'xml file"? What code do you have so far that isn't working? In what way(s) does it not work correctly? – Magoo Jul 18 '13 at 02:09
  • Why do you need to poll? Can you not ask windows to provide you with a folder changed notification? – joojaa Jul 18 '13 at 07:26

1 Answers1

2

Here's a PowerShell script that will do what you asked.

$content variable will store contents of the file (it will actually be an array of lines so you can throw it into foreach loop).

$file = 'C:\flag.xml'
$timeout = New-TimeSpan -Minutes 1
$sw = [diagnostics.stopwatch]::StartNew()
while ($sw.elapsed -lt $timeout)
{
    if (Test-Path $file)
    {
        "$(Get-Date -f 'HH:mm:ss') Found a file: $file"
        $content = gc $file
        if ($content -contains 'something interesting')
        {
            "$(Get-Date -f 'HH:mm:ss') File has something interesting in it!"
        }
        break
    }
    else
    {
        "$(Get-Date -f 'HH:mm:ss') Still no file: $file"
    }
    Start-Sleep -Seconds 5
}
  • Welcome to SO. This looks like a powershell script to me. It would be useful to add that to your answer for future readers. – foxidrive Jul 18 '13 at 08:58
  • i think this is a basic requriement, where the script polls a directory and waits for the file to come for a hardcoded time value. all it needs to do is read the file line by line and check for a particular tag. I have not yet tried the code. I am just looking for options and to check whether it is achievable using a batch file. – user2593173 Jul 18 '13 at 13:27