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?

5 Answers 5

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

FOR /F "tokens=*" %%i IN (' %cmd% ') DO SET X=%%i
Sign up to request clarification or add additional context in comments.

10 Comments

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.
@fduff It is indeed a wonder that assignment of command result to a variable is accomplished using FOR.
Uh, this doesn't work for multi-line output with spaces it seems?
@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 /?
See here for an explanation of this technique.
|
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

2 Comments

So, what if there is already a file named x.txt? (Created by user, or other programs?)
@Mai: I added the code to create unique file names.
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 ...

Comments

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

Comments

-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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.