Frage

Trolled through similar questions and am stuck on my script.

Basically, I need this .bat to check the directory for the number of files with the Lockbox prefix, store the count to a variable, and eventually call an .exe to import each of the files.

Here is what I have so far. My problem is that the test directory has a total of 12 txt files, but I only need the ones with the Lockbox prefix (11 of them):

@echo off

set count=0

for %%a in ('dir /a-d /a-h /a-s "\\ip_of_server\Directory\LockBox*.txt"') do set /a count+=1

@echo File count = %count%
pause
War es hilfreich?

Lösung

If you don't want to show directories and subdirectories, there is no need to use the "dir" command since FOR won't include them. However, it will likely include any hidden files should they start with LockBox. Just change line 5 to (you may need to use parentheses if spaces are in the path):

for %%a in (\\ip_of_server\Directory\LockBox*.txt) do set /a count+=1

Andere Tipps

I'd be very careful - look closely at your COUNT. If you have 11 LOCKBOX*.txt files that pass the attributes test, then the response from your code should be 4.

The code you have should produce 4 because you/ve omitted the /f from your FOR, so it evaluates the 'dir, a/d ... and skips the double-quoted item (I've no idea why it skips the double-quoted item).

If you use FOR /F in its place, you'd land up with 16 because the output of the dir command includes 5 non-empty lines - 3 in the header and 2 in the summary.

If you try

for /f %%a in ('dir /B /a-d /a-h /a-s "\\ip_of_server\Directory\LockBox*.txt"') do set /a count+=1&echo %%a

then your count should be correct and you'll get the filename being displayed as well.

If you only want the count, you could also try

for /f %%a in ('dir /a-d /a-h /a-s "\\ip_of_server\Directory\LockBox*.txt"^|find "File(s)"') do set count=%%a
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top