Set output of a command to a variable

时光毁灭记忆、已成空白 提交于 2019-12-24 04:06:10

问题


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 ""


回答1:


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.




回答2:


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 


来源:https://stackoverflow.com/questions/29824223/set-output-of-a-command-to-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!