2009-09-15 11 views

risposta

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

FOR /F "tokens=*" %%i IN (' %cmd% ') DO SET X=%%i 
+18

Solo una nota per altri che potrebbero utilizzare questo: se questo viene fatto all'interno di un file batch, '% i' deve essere sostituito con' %% i' entrambe le volte nel comando 'for'. –

+0

meraviglioso! :) grazie per questo – fduff

+0

@fduff È davvero una meraviglia che l'assegnazione del risultato del comando a una variabile venga eseguita utilizzando 'FOR'. –

2

Ecco un esempio:

@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 ... 
7
(dir /b "20??????" | find /c "2")>>x.txt 
set /p variable=<x.txt 

Questo è tutto.

Naturalmente, se non si desidera che il file, basta fare questo in seguito:

del x.txt 
+4

Quindi, se esiste già un file chiamato x.txt? (Creato dall'utente o altri programmi?) – Mai

+1

@Mai: scegli un nome univoco? Ne fai uno. – jgauffin

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

Questo è il mio codice:

@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 

Si prende l'input da com e uscite da output

Problemi correlati