Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need some help with batch script - redirect output into text file

    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):

    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 files\veritas\netbackup\bin\admincmd\
    bpimmedia -mediaid %INPUT% -L  
    pause
    And this script also used input method, it can check information about a particular media in database:

    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 files\veritas\netbackup\bin\admincmd
    bpmedialist -mlist -U -m %INPUT%
    pause
    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


    Last edited by igor7; 23rd May 2011, 07:59.

  • #2
    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 files\veritas\netbackup\bin\admincmd"
    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

    Comment


    • #3
      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:

      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 files\veritas\netbackup\bin\admincmd"
      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
      The input file look like:
      Code:
       
      C040S2 
      F217S3 
      C038S2
      But for some reason, the command
      Code:
      FOR /f %%A IN (%inputfile%) do %bpipath%/bpmedialist -mlist -U -m %%A >> %outputfile%
      is not executed... As I said output file is empty.

      Comment


      • #4
        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 files\veritas\netbackup\bin\admincmd"
        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%" )

        Comment


        • #5
          Re: Need some help with batch script - redirect output into text file

          Originally posted by beddo View Post
          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...

          Comment


          • #6
            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

            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.

            Comment


            • #7
              Re: Need some help with batch script - redirect output into text file

              Originally posted by beddo View Post
              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

              Code:
              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.
              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:
               
              @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 files\veritas\netbackup\bin\admincmd"
              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
              Last edited by igor7; 25th May 2011, 06:28.

              Comment


              • #8
                Re: Need some help with batch script - redirect output into text file

                Cool, got there in the end

                Comment

                Working...
                X