Sim101011
Sim101011

Reputation: 305

Set output of a command to a variable

I want to get the number of files modified before 10 days to a variable. I can get number of files using

forfiles /P "D:\files" /S /D -10  | find /c /v ""

But when i try to assign it to a variable using FOR it gives error. Command I used in FOR is

FOR /F "delims=" %i IN ('forfiles /P "D:\files" /S /D -10  | find /c /v ""') DO set today=%i

It actually works fine when I remove | find /c /v ""

Upvotes: 2

Views: 556

Answers (2)

Squashman
Squashman

Reputation: 14340

Yes you can use the FIND command to count how many occurrences it finds but you don't need to. You could just use the set command to iterate a variable.

FOR /F "delims=" %%G IN ('forfiles /P "D:\files" /S /D -10') do @set /a count+=1 

Upvotes: 0

npocmaka
npocmaka

Reputation: 57332

FOR /F "delims=" %i IN ('forfiles /P "D:\files" /S /D -10  ^| find /c /v ""') DO set today=%i

in this case you need to escape the pipe.

Upvotes: 3

Related Questions