I'm working on an problem where I need to count the number of .PDF files located in a particular folder and use a sensor to report that number. The sensor should return the number and if the number of .PDF files is > 1, create an error event. I messed around with the Folder sensor and File sensors but those don't appear to meet my needs, so I landed on trying to create my own custom sensor. I figured I could just write a simple batch script to count the files in the folder I want and return that integer via the custom exe sensor and viola, problem solved! However, I'm running into a couple of different problems. First, here is the initial code for the batch file I made:
@echo off set /P folderloc= set /a count=0 for /f "tokens=* delims= " %%a in ('dir/b/a-d "%folderloc%\*.pdf"') do ( set /a count+=1 ) echo %count%
The .bat files executes, requires a folder location be entered, then runs this loop to catch how many .PDF files are in this folder and returns the count. I've tested it, it works great. Now, I placed this .bat in the custom sensors/exe folder on the probe that hosts the device I'll be running this sensor on and went off to create the sensor. In the sensor settings, I assumed that the 'Parameters' field was where I would put my user input (ie the folder location), but the senor doesn't like that apparently so here is my first dilemma. Here is an example of what I placed in the Parameters field:
C:\Users\PDF Folder\
My first questions is, am I using this parameters field correctly? I tried different variations of the folder location to see if maybe I had the syntax wrong. Since the script requires folderloc to be defined by a user, would that user input not go into the parameters field? If not, is there any way around this? The error I get in return is just a time out error. So it appears to just be running with nothing happening until the timeout time of 60s.
Since I couldn't get that to work, I thought I'd go ahead and make sure I could get this working if I placed the directory location directly into the script like this:
set /a count=0 for /f "tokens=* delims= " %%a in ('dir/b/a-d "C:\Users\PDF Folder\*.pdf"') do ( set /a count+=1 ) echo %count%
I then created the sensor the same way but left the parameters field empty. It worked just like I wanted and returns a single integer value to my sensor. Excellent! Now dilemma number 2. I obviously don't care about any folders on my probe host and need this check to run on one of the devices that connect to the probe. This is where I think I may be in trouble because the .bat sits on the probe device but needs to read a folder location on the child device. With my working example above, I tried using the $ symbol to indicate I want to grab the folder from the child device, like how you would do it in a folder sensor.
set /a count=0 for /f "tokens=* delims= " %%a in ('dir/b/a-d "C$\Users\PDF Folder\*.pdf"') do ( set /a count+=1 ) echo %count%
Unfortunately, this failed as well and I think it's because this is running straight from the probe device. I was hoping if I could get the parameters in the sensor settings working properly, I would be able to connect to the correct directory since that's how it's handled on other sensors. Ideal scenario would look like this:
set /P folderloc= set /a count=0 for /f "tokens=* delims= " %%a in ('dir/b/a-d "%folderloc%\*.pdf"') do ( set /a count+=1 ) echo %count%
Under custom exe settings, the Parameters field would have something like this: C$\Users\PDF Folder
Any help would be appreciated!
Add comment