Need some help with batch script – redirect output into text file

Home Forums Scripting General Scripting Need some help with batch script – redirect output into text file

This topic contains 7 replies, has 2 voices, and was last updated by Avatar beddo 8 years, 10 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • Avatar
    igor7
    Member
    #154742

    Hi, everyone!!
    We have NetBackup application running on W2003K server to backup all clients data on tapes. Netbackup software has useful applets that can be executed from command line. Using those applets I can check for example images expiration time on particular cassette, tape properties… etc. And it’s very conveniently to use those command in batch scripts…
    For example this script use input method and it can check content of particular tape (all backup images stored on it):


    [COLOR=#0000ff]@echo off
    echo.
    echo Please insert tape ID bellow:
    echo.
    :input
    set INPUT=
    set /P INPUT=Type input: %=%
    if “%INPUT%”==”” goto input
    cd c:program filesveritasnetbackupbinadmincmd
    bpimmedia -mediaid %INPUT% -L
    pause[/COLOR]
    [/CODE]

    [COLOR=black]And this script also used input method, it can check information about a particular media in database:[/COLOR]
    [COLOR=#000000][/COLOR]
    [COLOR=#0000ff][CODE]
    [COLOR=#0000ff]@echo off
    echo.
    echo Please insert tape ID bellow:
    echo.
    :input
    set INPUT=
    set /P INPUT=Type input: %=%
    if “%INPUT%”==”” goto input
    cd C:program filesveritasnetbackupbinadmincmd
    bpmedialist -mlist -U -m %INPUT%
    pause[/COLOR]
    [/CODE]

    [COLOR=black][COLOR=black][FONT=Verdana]When I need to check few tapes it’s ok. But real hard work is came if I need to check whole weekly or monthly policy on different servers (each one contains about 40 tapes)… So question is if it possible to improve scripts that are using input method to read tapes info from text file and redirect output into another text file?[/FONT][/COLOR][COLOR=blue][FONT=Verdana][/FONT][/COLOR]
    [COLOR=black][FONT=Verdana]Thank you in advance,[/FONT][/COLOR][COLOR=blue][FONT=Verdana][/FONT][/COLOR]
    [COLOR=black][FONT=Verdana]Igor[/FONT][/COLOR][COLOR=blue][FONT=Verdana][/FONT][/COLOR]
    [/COLOR][COLOR=#000000][/COLOR]
    [COLOR=#000000][/COLOR][/COLOR]
    [/COLOR][CODE]
    @echo off
    echo.
    echo Please insert tape ID bellow:
    echo.
    :input
    set INPUT=
    set /P INPUT=Type input: %=%
    if “%INPUT%”==”” goto input
    cd c:program filesveritasnetbackupbinadmincmd
    bpimmedia -mediaid %INPUT% -L
    pause

    [/CODE]

    And this script also used input method, it can check information about a particular media in database:


    [COLOR=#0000ff]@echo off
    echo.
    echo Please insert tape ID bellow:
    echo.
    :input
    set INPUT=
    set /P INPUT=Type input: %=%
    if “%INPUT%”==”” goto input
    cd C:program filesveritasnetbackupbinadmincmd
    bpmedialist -mlist -U -m %INPUT%
    pause[/COLOR]
    [/CODE]

    [COLOR=black][COLOR=black][FONT=Verdana]When I need to check few tapes it’s ok. But real hard work is came if I need to check whole weekly or monthly policy on different servers (each one contains about 40 tapes)… So question is if it possible to improve scripts that are using input method to read tapes info from text file and redirect output into another text file?[/FONT][/COLOR][COLOR=blue][FONT=Verdana][/FONT][/COLOR]
    [COLOR=black][FONT=Verdana]Thank you in advance,[/FONT][/COLOR][COLOR=blue][FONT=Verdana][/FONT][/COLOR]
    [COLOR=black][FONT=Verdana]Igor[/FONT][/COLOR][COLOR=blue][FONT=Verdana][/FONT][/COLOR]
    [/COLOR][COLOR=#000000][/COLOR]
    [COLOR=#000000][/COLOR][/COLOR]
    [/COLOR][CODE]
    @echo off
    echo.
    echo Please insert tape ID bellow:
    echo.
    :input
    set INPUT=
    set /P INPUT=Type input: %=%
    if “%INPUT%”==”” goto input
    cd C:program filesveritasnetbackupbinadmincmd
    bpmedialist -mlist -U -m %INPUT%
    pause

    [/CODE]

    When I need to check few tapes it’s ok. But real hard work is came if I need to check whole weekly or monthly policy on different servers (each one contains about 40 tapes)… So question is if it possible to improve scripts that are using input method to read tapes info from text file and redirect output into another text file?
    Thank you in advance,
    Igor

    Avatar
    beddo
    Member
    #362633

    Re: Need some help with batch script – redirect output into text file

    Something like this will read the input from a file, assuming that the file has a single entry per line. Note that if the bpinmedia command requests any user interaction the batch file will wait for that input.

    Should be fairly simply to modify the do line for the other batch file.

    I haven’t tested this so use at your risk. It does check whether the input and output files exist. You could get fancy and have it ask for the input/output files when you run, add checks to ensure that the command files exists etc..

    Code:
    @echo off
    SET “bpipath=C:program filesveritasnetbackupbinadmincmd”
    SET “inputfile=C:tapelist.txt”
    SET “outputfile=”C:output.txt”

    IF NOT EXIST %inputfile% GOTO NEEDINPUT
    IF NOT EXIST %outputfile% GOTO CONTINUE
    echo %outputfile% already exists and will be overwritten if you continue.
    echo Press any key to continue or ctrl+c to abort
    pause

    :CONTINUE
    FOR /f %%A IN (%inputfile%) do %bpipath%bpinmedia -mediaaid %%A -L >> %outputfile%
    GOTO END

    :NEEDINPUT
    echo Number Johnny five needs input to continue, make sure %inputfile% exists

    :END

    Avatar
    igor7
    Member
    #295066

    Re: Need some help with batch script – redirect output into text file

    beddo,
    thank you for help, but script create empty output file… I played around with your script (added date and time to output file). It assume that script itsef, input and output files in the same directory:

    @echo off
    :: prepare date/time stamp
    for /f “Tokens=1-4 Delims=/ ” %%i in (‘date /t’) do set dt=%%i-%%k-%%j-%%l
    for /f “Tokens=1” %%i in (‘time /t’) do set tm=-%%i
    :: timestamp (ddmmmyyyy-hhmm)
    set tm=%tm::=-%
    set dtt=%dt%%tm%
    SET “bpipath=C:program filesveritasnetbackupbinadmincmd”
    SET “inputfile=tapes.txt”
    SET “outputfile=weekly-%dtt%.txt”
    IF NOT EXIST %inputfile% GOTO NEEDINPUT
    IF NOT EXIST %outputfile% GOTO CONTINUE
    echo %outputfile% already exists and will be overwritten if you continue.
    echo Press any key to continue or ctrl+c to abort
    pause
    :CONTINUE
    FOR /f %%A IN (%inputfile%) do %bpipath%/bpmedialist -mlist -U -m %%A >> %outputfile%
    GOTO END
    :NEEDINPUT
    echo This script need input file to continue, make sure %inputfile% exists
    pause
    :END
    [/CODE]

    The input file look like:
    [CODE]
    C040S2
    F217S3
    C038S2
    [/CODE]

    But for some reason, the command [CODE]FOR /f %%A IN (%inputfile%) do %bpipath%/bpmedialist -mlist -U -m %%A >> %outputfile%[/CODE]
    is not executed… As I said output file is empty.[CODE]
    @echo off
    :: prepare date/time stamp
    for /f “Tokens=1-4 Delims=/ ” %%i in (‘date /t’) do set dt=%%i-%%k-%%j-%%l
    for /f “Tokens=1” %%i in (‘time /t’) do set tm=-%%i
    :: timestamp (ddmmmyyyy-hhmm)
    set tm=%tm::=-%
    set dtt=%dt%%tm%
    SET “bpipath=C:program filesveritasnetbackupbinadmincmd”
    SET “inputfile=tapes.txt”
    SET “outputfile=weekly-%dtt%.txt”
    IF NOT EXIST %inputfile% GOTO NEEDINPUT
    IF NOT EXIST %outputfile% GOTO CONTINUE
    echo %outputfile% already exists and will be overwritten if you continue.
    echo Press any key to continue or ctrl+c to abort
    pause
    :CONTINUE
    FOR /f %%A IN (%inputfile%) do %bpipath%/bpmedialist -mlist -U -m %%A >> %outputfile%
    GOTO END
    :NEEDINPUT
    echo This script need input file to continue, make sure %inputfile% exists
    pause
    :END
    [/CODE]

    The input file look like:

    C040S2
    F217S3
    C038S2
    [/CODE]

    But for some reason, the command [CODE]FOR /f %%A IN (%inputfile%) do %bpipath%/bpmedialist -mlist -U -m %%A >> %outputfile%[/CODE]
    is not executed… As I said output file is empty.[CODE]
    C040S2
    F217S3
    C038S2
    [/CODE]

    But for some reason, the command FOR /f %%A IN (%inputfile%) do %bpipath%/bpmedialist -mlist -U -m %%A >> %outputfile%[/CODE]
    is not executed… As I said output file is empty.[CODE]FOR /f %%A IN (%inputfile%) do %bpipath%/bpmedialist -mlist -U -m %%A >> %outputfile%[/CODE]
    is not executed… As I said output file is empty.

    Avatar
    beddo
    Member
    #362635

    Re: Need some help with batch script – redirect output into text file

    I’ve put some of the extra line breaks back in to make it more readable:

    Code:
    @echo off
    :: prepare date/time stamp
    for /f “Tokens=1-4 Delims=/ ” %%i in (‘date /t’) do set dt=%%i-%%k-%%j-%%l
    for /f “Tokens=1” %%i in (‘time /t’) do set tm=-%%i
    :: timestamp (ddmmmyyyy-hhmm)
    set tm=%tm::=-%
    set dtt=%dt%%tm%

    SET “bpipath=C:program filesveritasnetbackupbinadmincmd”
    SET “inputfile=tapes.txt”
    SET “outputfile=weekly-%dtt%.txt”
    IF NOT EXIST %inputfile% GOTO NEEDINPUT
    IF NOT EXIST %outputfile% GOTO CONTINUE
    echo %outputfile% already exists and will be appended to if you continue.
    echo Press any key to continue or ctrl+c to abort
    pause

    :CONTINUE
    FOR /f %%A IN (%inputfile%) do ( %bpipath%/bpmedialist -mlist -U -m %%A >> %outputfile% )
    GOTO END

    :NEEDINPUT
    echo This script needs input file to continue, make sure %inputfile% exists
    pause

    :END

    The do command probably needs enclosing. It may even need some quotes but I can’t remember offhand so if the brackets aren’t working try this:

    Code:
    FOR /f %%A IN (%inputfile%) do ( “%bpipath%/bpmedialist -mlist -U -m %%A >> %outputfile%” )
    Avatar
    igor7
    Member
    #295067

    Re: Need some help with batch script – redirect output into text file

    beddo;238818 wrote:
    I’ve put some of the extra line breaks back in to make it more readable…

    The do command probably needs enclosing. It may even need some quotes but I can’t remember offhand so if the brackets aren’t working try this:

    Code:
    FOR /f %%A IN (%inputfile%) do ( “%bpipath%/bpmedialist -mlist -U -m %%A >> %outputfile%” )

    Thanks one more time! But still… brackets not worked, – same empty file created. I’ve put quotes per your advice, – it even not create output file… I’ve also tried with different configuration (with quotes without brackets and vice versa) – nothing… I believe its some scripts “spelling” issue but I have no idea how to make it work…

    Avatar
    beddo
    Member
    #362637

    Re: Need some help with batch script – redirect output into text file

    Two possibilities. One is that it needs the full path to the program, including the file extension which we are currently missing.

    It is probably just the command that needs quoting

    Quote:
    FOR /f %%A IN (%inputfile%) do “%bpipath%/bpmedialist -mlist -U -m %%A” >> %outputfile%

    The for loop itself is fine, I tested it by changing everything after the do to “echo %%A” and it outputs te first phrase of every line of the batch file on a new line.

    Avatar
    igor7
    Member
    #295068

    Re: Need some help with batch script – redirect output into text file

    beddo;238856 wrote:
    Two possibilities. One is that it needs the full path to the program, including the file extension which we are currently missing.

    It is probably just the command that needs quoting

    FOR /f %%A IN (%inputfile%) do “%bpipath%/bpmedialist -mlist -U -m %%A” >> %outputfile%[/CODE]

    The for loop itself is fine, I tested it by changing everything after the do to “echo %%A” and it outputs te first phrase of every line of the batch file on a new line.[/QUOTE]

    I’ve found where was a problem! And you was right! The command itself just needs quoting in right place. Here is working code:

    [CODE]
    [COLOR=”Blue”]@echo off

    :: prepare date/time stamp
    for /f “Tokens=1-4 Delims=/ ” %%i in (‘date /t’) do set dt=%%i-%%k-%%j-%%l
    for /f “Tokens=1” %%i in (‘time /t’) do set tm=-%%i

    :: timestamp (ddmmmyyyy-hhmm)
    set tm=%tm::=-%
    set dtt=%dt%%tm%

    SET “bpipath=C:program filesveritasnetbackupbinadmincmd”
    SET “inputfile=tapes.txt”
    SET “outputfile=weekly-%dtt%.txt”

    IF NOT EXIST %inputfile% GOTO NEEDINPUT
    IF NOT EXIST %outputfile% GOTO CONTINUE
    echo %outputfile% already exists and will be appended to if you continue.
    echo Press any key to continue or ctrl+c to abort
    pause

    :CONTINUE
    FOR /f %%A IN (%inputfile%) do ( “%bpipath%bpmedialist” -mlist -U -m %%A >> %outputfile% )
    GOTO END

    :NEEDINPUT
    echo This script needs input file to continue, make sure %inputfile% exists
    pause

    :END[/COLOR][/CODE][CODE]FOR /f %%A IN (%inputfile%) do “%bpipath%/bpmedialist -mlist -U -m %%A” >> %outputfile%[/CODE]

    The for loop itself is fine, I tested it by changing everything after the do to “echo %%A” and it outputs te first phrase of every line of the batch file on a new line.

    I’ve found where was a problem! And you was right! The command itself just needs quoting in right place. Here is working code:

    [COLOR=”Blue”]@echo off

    :: prepare date/time stamp
    for /f “Tokens=1-4 Delims=/ ” %%i in (‘date /t’) do set dt=%%i-%%k-%%j-%%l
    for /f “Tokens=1” %%i in (‘time /t’) do set tm=-%%i

    :: timestamp (ddmmmyyyy-hhmm)
    set tm=%tm::=-%
    set dtt=%dt%%tm%

    SET “bpipath=C:program filesveritasnetbackupbinadmincmd”
    SET “inputfile=tapes.txt”
    SET “outputfile=weekly-%dtt%.txt”

    IF NOT EXIST %inputfile% GOTO NEEDINPUT
    IF NOT EXIST %outputfile% GOTO CONTINUE
    echo %outputfile% already exists and will be appended to if you continue.
    echo Press any key to continue or ctrl+c to abort
    pause

    :CONTINUE
    FOR /f %%A IN (%inputfile%) do ( “%bpipath%bpmedialist” -mlist -U -m %%A >> %outputfile% )
    GOTO END

    :NEEDINPUT
    echo This script needs input file to continue, make sure %inputfile% exists
    pause

    :END[/COLOR][/CODE][CODE]
    @echo off

    :: prepare date/time stamp
    for /f “Tokens=1-4 Delims=/ ” %%i in (‘date /t’) do set dt=%%i-%%k-%%j-%%l
    for /f “Tokens=1” %%i in (‘time /t’) do set tm=-%%i

    :: timestamp (ddmmmyyyy-hhmm)
    set tm=%tm::=-%
    set dtt=%dt%%tm%

    SET “bpipath=C:program filesveritasnetbackupbinadmincmd”
    SET “inputfile=tapes.txt”
    SET “outputfile=weekly-%dtt%.txt”

    IF NOT EXIST %inputfile% GOTO NEEDINPUT
    IF NOT EXIST %outputfile% GOTO CONTINUE
    echo %outputfile% already exists and will be appended to if you continue.
    echo Press any key to continue or ctrl+c to abort
    pause

    :CONTINUE
    FOR /f %%A IN (%inputfile%) do ( “%bpipath%bpmedialist” -mlist -U -m %%A >> %outputfile% )
    GOTO END

    :NEEDINPUT
    echo This script needs input file to continue, make sure %inputfile% exists
    pause

    :END[/CODE]

    Avatar
    beddo
    Member
    #362640

    Re: Need some help with batch script – redirect output into text file

    Cool, got there in the end :)

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.