1.

Solve : Command to count number of files in directory called %username% to variable?

Answer»

Hi,

We have a directory of login logs for when users log in, part of the name being the username of the person logged on.

I'm trying to troubleshoot why certain logs are not being written.

if I use forfiles.exe and forfiles -PC:\temp\logs -d+1 -m*%username% I get a list of files for a user in the last day.

I wanted to get this as a count of number of files it finds in a variable, not just a list of them

Any ideas?

Thanks, SukhQuote from: skhudy on May 24, 2010, 07:16:20 AM



If I use forfiles.exe and forfiles -pc:\temp\logs -d+1 -m*%username% I get a list of files for a user in the last day.

I wanted to get this as a count of number of files it finds in a variable, not just a list of them


______________________________


c:\temp\logs>TYPE sk.bat
@echo off

Code: [Select]forfiles /p c:\temp\logs /m %username%* | FIND /c /v "" > filecount.txt

echo type filecount.txt

type filecount.txt
Output:c:\temp\logs>sk.bat
type filecount.txt
3

c:\temp\logs>Quote from: skhudy on May 24, 2010, 07:16:20 AM
if I use forfiles.exe and forfiles -pc:\temp\logs -d+1 -m*%username% I get a list of files for a user in the last day.

I wanted to get this as a count of number of files it finds in a variable, not just a list of them


The above post did not assign the count to a variable. Also, the count was 3 and should be 2.


c:\test>type sk.bat
@echo off

forfiles /p c:\temp\logs /m %username%* | find /c /v "" > filecount.txt

set /p var=set /a var=%var% - 1
echo var=%var%

Output:

c:\test> sk.bat
var=2

c:\test>Quote from: skhudy on May 24, 2010, 07:16:20 AM

if I use forfiles.exe and forfiles -pc:\temp\logs -d+1 -m*%username% I get a list of files for a user in the last day.



c:\test>type sk2.bat
@echo off

forfiles /p c:\temp\logs /m %username%*

forfiles /p c:\temp\logs /m %username%* | wc -l > filecount.txt

set /p var=set /a var=%var% - 1
echo var=%var%

Output:

c:\test>sk2.bat

"marvin.txt"
"marvin.txt"
var=2

c:\test>Quote from: skhudy on May 24, 2010, 07:16:20 AM

if I use forfiles.exe and forfiles -pc:\temp\logs -d+1 -m*%username% I get a list of files for a user in the last day.

I wanted to get this as a count of number of files it finds in a variable, not just a list of them


c:\test>type sk3.bat
Code: [Select]@echo off

forfiles /p c:\temp\logs /m %username%*

forfiles /p c:\temp\logs /m %username%* | wc -l > filecount.txt

set /p var=<filecount.txt
set /a var=%var% - 1
echo var=%var%

cd c:\temp\logs

dir /OD /A-D %username%*

dir /OD /A-D %username%* | findstr %username% | wc -l > count2.txt
set /p var2=<count2.txt

echo var2=%var2%

cd c:\test\

Output:

c:\test>sk3.bat

"marvin1.txt"
"marvin2.txt"
var=2
Volume in DRIVE C has no label.
Volume Serial Number is 0652-E41D

Directory of c:\temp\logs

05/24/2010 01:09 PM 8 marvin2.txt
05/24/2010 01:09 PM 8 marvin1.txt
2 File(s) 16 bytes
0 Dir(s) 295,916,421,120 bytes free
var2=2
c:\test>


Discussion

No Comment Found