26

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?

Alceu Costa
  • 9,733
  • 19
  • 65
  • 83

5 Answers5

50
set cmd="dir /b "20??????" | find /c "2" "

FOR /F "tokens=*" %%i IN (' %cmd% ') DO SET X=%%i
kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80
David J. Liszewski
  • 10,959
  • 6
  • 44
  • 57
  • 22
    Just a note for others who may be using this: if this is done inside a batch file, `%i` must be replaced with `%%i` both times in the `for` command. – Nathan Reed Apr 16 '12 at 17:14
  • @fduff It is indeed a wonder that assignment of command result to a variable is accomplished using `FOR`. – David J. Liszewski Aug 22 '12 at 01:31
  • 3
    Uh, this doesn't work for multi-line output with spaces it seems? – Camilo Martin Sep 08 '12 at 00:52
  • @CamiloMartin Agreed, that snippet is likely not able to capture multi-line output. I no longer have a Windows machine (virtual or otherwise) with which I could attempt an improved solution. Sorry. – David J. Liszewski Sep 10 '12 at 18:15
  • 3
    @CamiloMartin Sorry for the double comment, I accidentally pressed Enter. Also I apparently struggle at linebreaks. `FOR /F "tokens=*" %i IN (' %cmd% ') DO SET X=%i` Additional uses of the tokens command, such as cutting out the second column: `FOR /F "tokens=1,3*" %i IN (' %cmd% ') DO SET X=%i %j %k` That's i equals column one, j equals column 3, and k equals columns 4 and on. It does assume that the delimiter is a space (in the SET section), so modify that if need be (you can define your delims the same same way you define tokens). The question mark is your friend! `FOR /?` – Derek Apr 11 '13 at 17:49
  • @Derek good to know! By the way, for a short while (I think a few minutes) StackOverflow will let you edit a comment, It's useful also when you want to undo a typo, but it only lasts for a little while after the comment was posted. – Camilo Martin Apr 13 '13 at 00:14
  • 3
    See [here](http://blogs.msdn.com/b/oldnewthing/archive/2012/07/31/10334556.aspx) for an explanation of this technique. – Florian Brucker Apr 17 '14 at 06:09
  • @CamiloMartin I *think* I made this answer a community wiki. I'm unsure if such an action is subject to moderation (see http://meta.stackexchange.com/questions/392/should-the-community-wiki-police-be-shut-down/7183#7183). Anyway, I'm hoping you have sufficient reputation to improve upon the answer. Cheers! – David J. Liszewski Jun 16 '15 at 03:35
  • @Derek Comments to CamiloMartin apply equally to you (apologies, apparently SO comment UI limitation permits only one @ reference). – David J. Liszewski Jun 16 '15 at 04:10
  • Can I donate this answer to someone who currently writes BAT scripts? – David J. Liszewski Aug 09 '16 at 04:10
9
(dir /b "20??????" | find /c "2")>>x.txt  
set /p variable=<x.txt

That's all.

Of course, if you don't want the file, just do this afterwards:

del x.txt

EDIT -- How to make the filename unique:

@Mai: use this to create a uniqe file name:

set timestamp=%date:.=%%time::=%
set timestamp=%timestamp:,=%
set timestamp=%timestamp:/=%
set timestamp=%timestamp:-=%
set filename=file%timestamp%.tmp

Add more replacements if the date format of your system culture has other characters inside

Mehrdad Mirreza
  • 984
  • 12
  • 20
Jamie
  • 1,096
  • 2
  • 19
  • 38
2

Here's a sample:

@echo off
set wildcard=C:\*.*
set count=0
FOR /F %%a in ('DIR /B %wildcard%') do set /A count=count+1
echo %count% files matching %wildcard%
set choice=
set /p choice=Press enter to continue ...
Lukman
  • 18,462
  • 6
  • 56
  • 66
0

This is my code:

@echo off
set com=echo HI
::Start Of Code You Need
echo|%com%>>"%temp%\tmp.txt"
for /f "tokens=* delims=" %%x in (%temp%\tmp.txt) do (
set output=%%x
)
del /q %temp%\tmp.txt
::End Of Code You Need
echo This Is The Output:
echo %output%
pause>NUL

It takes the input from com and outputs from output

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
-2
@echo off

REM The command fltmc volumes provides some information about drives
REM I need to extract the drive letter associated to a particular volume name.
REM Here's how:

fltmc volumes | find /i "\Device\HarddiskVolume3" > delme.txt
for /f "tokens=1 delims= " %%i in (delme.txt) DO echo %%i
Del /f delme.txt