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

前端 未结 5 1974
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 23:10

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

\'dir /b \"20??????\" | find /c \"2\"\'
相关标签:
5条回答
  • 2020-12-02 23:42
    (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

    0 讨论(0)
  • 2020-12-02 23:54
    set cmd="dir /b "20??????" | find /c "2" "
    
    FOR /F "tokens=*" %%i IN (' %cmd% ') DO SET X=%%i
    
    0 讨论(0)
  • 2020-12-02 23:54

    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

    0 讨论(0)
  • 2020-12-03 00:04

    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 ...
    
    0 讨论(0)
  • 2020-12-03 00:07
    @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 讨论(0)
提交回复
热议问题