Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file Var output help needed  (Read 3595 times)

0 Members and 1 Guest are viewing this topic.

almulder

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Unknown
    Batch file Var output help needed
    « on: November 01, 2011, 10:54:58 AM »
    I need a few things

    1) need to pass a var to a subroutine to be use in subroutine
    Code: [Select]
    echo. this is a test
    call :test (need to pass var (test1)
    call :test (need to pass var (test2)
    call :test (need to pass var (test3)
    :end

    :test1
    echo. This is (insert variable passed)
    goto :eof


    Result output should be
    Code: [Select]
    This is test1
    This is test2
    This is test3


    2) I need to be able to create a config file to read and write to with ability to modify a particular line within the config file.

    Example:
    I want to give the user the ability to select a particular directory or create a new directory or delete a created directory listing.

    I would want a subroutine menu created from the config file and what ever lines are in the config file to create that many options.
    Config file:
    Code: [Select]
    "C$\testfolder0\"
    "C$\testfolder1\"
    "C$\testfolder2\"
    "C$\testfolder3\"
    "C$\testfolder4\"

    Menu output:
    Code: [Select]
    Directory select menu:

    1) C$\testfolder0
    2) C$\testfolder1
    3) C$\testfolder2
    4) C$\testfolder3
    5) C$\testfolder4
    6) Create New Directory Entry
    7) Delete a Directory Entry

    Select Directory:



    I would then need the result set to a variable to be returned from the subroutine and sent to another subroutine and used in connecting to that directory on a computer (we are copying files to 10 backup servers often but different files and to different folders)
    Code: [Select]
    XCOPY  *.* "\\192.168.1.110 (Variable)"
    XCOPY  *.* "\\192.168.1.120 (Variable)"
    XCOPY  *.* "\\192.168.1.130 (Variable)"

    Salmon Trout

    • Guest
    Re: Batch file Var output help needed
    « Reply #1 on: November 01, 2011, 11:34:08 AM »
    Quote
    echo. this is a test
    call :test (need to pass var (test1)
    call :test (need to pass var (test2)
    call :test (need to pass var (test3)
    :end

    :test1
    echo. This is (insert variable passed)
    goto :eof

    1. pass literal strings

    Code: [Select]
    echo. this is a test
    call :test test1
    call :test test2
    call :test test3
    :end

    2. pass variables

    Code: [Select]
    echo. this is a test
    call :test %test1%
    call :test %test2%
    call :test %test3%
    :end


    Either case, subroutine shows what is passed:

    Code: [Select]
    :test1
    echo. This is %1
    goto :eof

    almulder

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Unknown
      Re: Batch file Var output help needed
      « Reply #2 on: November 01, 2011, 12:09:24 PM »
      Thanks for your help. That work like i needed.

      now another question about var's

      im doing the following subroutine:
      Code: [Select]

      call :s1ofc "Server 1"
      PING 192.168.1.110 -n 1| FIND "bytes=" > NUL
      call :s2ofc "Server 1"
      XCOPY  *.* "\\192.168.1.110\C$\Test Folder" /EXCLUDE:copyfiles.cfg /s/y/z/v/k
      call :s3ofc "Server 1"
      GOTO :EOF



      ::===========================::
      :: Screen1 ouput for copying ::
      ::===========================::
      :s1ofc
      echo.
      echo. -----------------------------------------------
      echo.
      echo. Copying files to %1
      echo.
      echo. Connecting to %1...
      GOTO :EOF

      ::===========================::
      :: Screen2 ouput for copying ::
      ::===========================::
      :s2ofc
      IF ERRORLEVEL 1 ECHO.   Connection failed... && ECHO.     Skipping %1...&& echo %date% %time% Connecting to %1 failed>> .\LOG.TXT && ECHO. && GOTO :EOF
      echo.   Connection established...
      echo.
      echo.     Copying started...
      echo.
      pause
      GOTO :EOF


      ::===========================::
      :: Screen3 ouput for copying ::
      ::===========================::
      :s3ofc
      echo.
      if errorlevel 4 echo "An error has be found" && echo. Please check the log.txt file. && echo %date% %time% Insufficient disk access, space, on %1>> .\LOG.TXT
      if errorlevel 5 echo echo %date% %time% Disk write error occur99red on %1>> .\LOG.TXT
      echo.
      echo. Copying files to %1 Complete...
      PING 1.0.0.0 -n 1 -w 2000 >NUL
      GOTO :EOF

      I need help with my if errorlevel's
      I want that If the errorlevel is true that on that line i can enter in a var and pass it back to the original routine to run an if statement.

      Code: [Select]
      call :s1ofc "Server 1"
      PING 192.168.1.110 -n 1| FIND "bytes=" > NUL
      IF erq1=1 goto: eof
      call :s2ofc "Server 1"
      XCOPY  *.* "\\192.168.1.110\C$\Test Folder" /EXCLUDE:copyfiles.cfg /s/y/z/v/k
      call :s3ofc "Server 1"
      GOTO :EOF

      ::===========================::
      :: Screen2 ouput for copying ::
      ::===========================::
      :s2ofc
      set erq1=0
      IF ERRORLEVEL 1 ECHO.  Connection failed... && ECHO.     Skipping %1...&& echo %date% %time% Connecting to %1 failed>> .\LOG.TXT && ECHO. && erq1=1 && GOTO :EOF
      echo.   Connection established...
      echo.
      echo.     Copying started...
      echo.
      pause
      GOTO :EOF

      but for some reason erq1 always equals 0

      Thoughts?

      Salmon Trout

      • Guest
      Re: Batch file Var output help needed
      « Reply #3 on: November 01, 2011, 12:40:24 PM »
      if you use the archaic obsolete MS-DOS method in this format:

      (N is a number)

      IF ERRORLEVEL N action

      You need to understand that the test is passed if the errorlevel is N or greater. Thus if you are testing for different errorlevels you have to do the tests in descending order. The action is usually GOTO a label because otherwise every level below the one you want will be executed also. (do you understand why?)

      if errorlevel 5 action
      if errorlevel 4 action
      if errorlevel 3 action
      if errorlevel 2 action
      if errorlevel 1 action

      Better to use the (since Windows 2000) NT type errorlevel variable where the order does not matter and you can specify more tests and the test can be exactly what you want

      e.g.

      if %errorlevel% equ 4 action

      if %errorlevel% gtr 0 action

      Also, in Windows, && does not mean what you think it does.

      almulder

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Unknown
        Re: Batch file Var output help needed
        « Reply #4 on: November 01, 2011, 01:07:20 PM »
        ok i see why i would want to use %errorlevel%

        how would i fix this line if i should not use &&?
        Code: [Select]
        if %errorlevel% 5 echo echo %date% %time% Disk write error occurred on %1>> .\LOG.TXT
        if %errorlevel% 4 echo "An error has be found" && echo. Please check the log.txt file. && echo %date% %time% Insufficient disk access, space, on %1>> .\LOG.TXT

        Now is there anyway to output a list of files that have an error?

        Currently it will only tell me that there was an error but not what files were the cause.

        Is there something better to use than xcopy? (Free for commercial use)

        Running on XP win7 server2003 & server2008


        Salmon Trout

        • Guest
        Re: Batch file Var output help needed
        « Reply #5 on: November 01, 2011, 01:17:50 PM »
        Quote
        how would i fix this line if i should not use &&?

        Just use one ampersand (&) if you merely want to join commands in one line

        In Unix and Windows & is the command separator

        command1 & command2 means "execute command1 and then execute command2"

        && is a conditional execution operator

        command1 && command2 means "execute command1 and if command1 returns a zero errorlevel then execute command2"

        The opposite is ||

        command1 || command2 means "execute command1 and if command1 returns a non-zero errorlevel then execute command2"

        Quote
        Is there something better to use than xcopy? (Free for commercial use)

        Many people like Robocopy




        almulder

          Topic Starter


          Greenhorn

          • Experience: Beginner
          • OS: Unknown
          Re: Batch file Var output help needed
          « Reply #6 on: November 01, 2011, 01:33:04 PM »
          Thanks :)