Results 1 to 6 of 6

Thread: Windows batch file: set output of program to a variable?

  1. #1
    Join Date
    Jul 2009
    Posts
    124

    Windows batch file: set output of program to a variable?

    I am annoying to locate the production of a command line program to a variable in a Windows batch file. For example, if I did like to study the output of the "ver" command to a variable called "myvar", how would I do it? So "ver" produce for me: "Microsoft Windows XP [Version 5.1.2600]" How would I interpret that into a variable called myvar? I have tried out piping it, but cannot able to outline out what the syntax it would be. This is trivial in other languages such as PHP, where I did basically have: $myvar = shell_exec("ver" ) I don’t understand it so any help related to this problem will be appreciated. Thank you in Advance.

  2. #2
    Join Date
    May 2008
    Posts
    3,316

    Re: Windows batch file: set output of program to a variable?

    Would myvar be external to the batch file? If so, you have ver > (location of myvar) without the parens of path; e.g. ver> c:myvar you would require the back slash connecting colon & myvar. We are not permitted to type it. Then you could position it afterward in the batch file. Myvar doesn't even have to exist. The redirection would create it.

  3. #3
    Join Date
    Jul 2009
    Posts
    124

    Re: Windows batch file: set output of program to a variable?

    I am facing the problem with my Windows batch file. If anyone’s being unsure how to get that variable back into the batch file, here's how:
    Code:
    cmd > tmpFile 
    set /p myvar= < tmpFile 
    del tmpFile
    The second line interpret back from the temporary file. Do anyone have any idea what does this code means to. Please help me out.

  4. #4
    Join Date
    Jan 2008
    Posts
    3,755

    Re: Windows batch file: set output of program to a variable?

    To set the output of var into a variable without creating a temp file, go a bit it is supplementary composite with:

    Code:
    for /f "delims="%a in ('ver ^| findstr /v "linux") you can set myvar=%a 
    
    check it with: echo %myvar% =wrybread
    I tried piping it, but unable to figure out what the syntax would be. There your piped code, but I am well known about this command, I now it as it is three years old but figured that someone could get help to solve their problem. Sometimes this may help you.

  5. #5
    Join Date
    Apr 2008
    Posts
    2,139

    Re: Windows batch file: set output of program to a variable?

    I have written using FOR statements that it will not need you to output everything to a text file first. This is significant if you are functioning in areas where you might not have mark access. I have also observe that sometimes you want to get rid of spaces like from the Date /T or Ver commands which in turn impressive like "Tue 06/20/2009" or "Microsoft Windows Version [6.0.6001]" into another variables.

    Code:
    @ECHO OFF 
    
    :; Clear the screen and turn echo off (above) to keep it clean 
    CLS 
    
    :; Clear any previous variables set 
    SET To= 
    SET DM= 
    SET MD= 
    SET Y= 
    SET Var1= 
    SET WinVe= 
    SET WinMa= 
    SET WinMi= 
    SET WinBu= 
    SET WD= 
    SET DOW= 
    
    :; Get Value from 'VER' command output 
    FOR /F "tokens=*" %%i in ('VER') do SET WinVe=%%i 
    FOR /F "tokens=1-3 delims=]-" %%A IN ("%WinVe%" ) DO ( 
    SET Var1=%%A 
    ) 
    
    :; Get version number only so drop off Microsoft Windows Version 
    FOR /F "tokens=1-9 delims=n" %%A IN ("%Var1%" ) DO ( 
    SET WinVe=%%C 
    echo %WinVe% 
    ) 
    
    :; Separate version numbers 
    FOR /F "tokens=1-8 delims=.-" %%A IN ("%WinVe%" ) DO ( 
    SET WinMa=%%A 
    SET WinMi=%%B 
    SET WinBu=%%C 
    ) 
    
    :; Fix the extra space left over in the Major 
    FOR /F "tokens=1 delims= " %%A IN ("%WinMa%" ) DO ( 
    SET WinMa=%%A 
    ) 
    
    :; Display Results 
    ECHO WinVe = %WinVe% 
    ECHO WinMa = %WinMa% 
    ECHO WinMi = %WinMi% 
    ECHO WinBu = %WinBu% 
    
    :; Pause for a moment 
    Pause 
    
    :; Get the output from the "Date /T" 
    FOR /F "tokens=*" %%i in ('DATE /T') do SET To=%%i 
    FOR /F "tokens=1-3 delims=/-" %%A IN ("%To%" ) DO ( 
    SET DM=%%A 
    SET MD=%%B 
    SET Yr=%%C 
    ) 
    
    :; Separate the Day from Month like "Tue 06" to "Tue" 
    FOR /F "tokens=1 delims= " %%A IN ("%DM%" ) DO ( 
    SET DOW=%%A 
    ) 
    
    :; Separate the Day from Month like "Tue 06" to "06" 
    FOR /F "tokens=2 delims= " %%A IN ("%DM%" ) DO ( 
    SET Month=%%A 
    ) 
    
    :; Now show the date output using your variables 
    ECHO Month = %Month% 
    ECHO DOW = %DOW% 
    ECHO Date = %DOW% %Month%/%MD%/%Y%

  6. #6
    Join Date
    Apr 2008
    Posts
    4,088

    Re: Windows batch file: set output of program to a variable?

    I was unable to understand what you mean by the echo code the same code is given below. Then I came to know that the below given coding is correct.
    Code:
    @echo off 
    setlocal enable extensions 
    for /f "tokens=*" %%a in ('VER' ) do ( set myvar=%%a ) 
    echo/%%myvar%%=%myvar% 
    pause 
    endlocal
    I think basically we can place any .exe along with correct path, in the place of VER, just put it in involving the inverted commas.

Similar Threads

  1. Formatting Modified Date Variable in Batch File
    By erock24 in forum Software Development
    Replies: 1
    Last Post: 21-10-2011, 12:39 PM
  2. Learning Batch file to open a program or a file
    By Ikshana in forum Windows Software
    Replies: 3
    Last Post: 04-04-2011, 07:24 PM
  3. Windows Batch file to output directory names and size to txt file
    By m2thearkus in forum Software Development
    Replies: 6
    Last Post: 16-07-2010, 12:04 AM
  4. How can I play a sound file from a Windows batch file?
    By Linoo in forum Operating Systems
    Replies: 4
    Last Post: 27-03-2010, 07:20 PM
  5. Problem with variable input in C program
    By Zool in forum Software Development
    Replies: 2
    Last Post: 14-05-2009, 09:43 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,957,807.46047 seconds with 17 queries