Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store the result of a command expression in a variable using bat scripts?

Tags:

I have the command below to count all directories that that follow the pattern 20?????? :

'dir /b "20??????" | find /c "2"' 

For example, if I have the following directories, the command would return 6:

20090901 20090902 20090903 20090904 20090905 20090906 

How can I store the result of this command (6 in the forementioned example) in a variable?

like image 475
Alceu Costa Avatar asked Sep 15 '09 13:09

Alceu Costa


People also ask

Can batch file return a value?

By default when a command line execution is completed it should either return zero when execution succeeds or non-zero when execution fails. When a batch script returns a non-zero value after the execution fails, the non-zero value will indicate what is the error number.

What is %% R in batch command?

/r - iterate through the folder and all subfolders, i.e. recursively. %%I - placeholder for a path/filename. The above command simply lists all files in the current folder and all subfolders. Follow this answer to receive notifications.


1 Answers

set cmd="dir /b "20??????" | find /c "2" "  FOR /F "tokens=*" %%i IN (' %cmd% ') DO SET X=%%i 
like image 128
3 revs, 2 users 80% Avatar answered Oct 18 '22 21:10

3 revs, 2 users 80%