Announcement

Collapse
No announcement yet.

Batch For/If Loop and Sub question

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

  • Batch For/If Loop and Sub question

    I've done a few web searches to no avail..
    I have a list generated by a FOR line and tried several methods of trying to add a Users Yes No set /p = ....
    Code:
    for /f "delims=" %%a in (Search_Result.txt) do (
       cls
       title Processing Requests
       echo.
       echo.
       echo  Search Requests
       echo.
       echo %%a
       echo.
       set /p MC= Add Y or N to List : 
       pause [to make sure the lines were being read]
       if /i "%MC%"=="y" echo Y response
       echo %%a>>SomeFile.txt
       if /i "%MC%"=="n" echo N response
       pause [to make sure the lines were being read]
    )
    trimmed it down for brevity, but it ran minus the actual User Input

    And I tried a DO call :sub %%a routine in several diff flavors..
    Should I have used errorlevels instead?

    I figure there is something small but important missing
    LOL all its intended to do is echo %%a into a file if the user selects y and skip to the next.. but I am befuddled :-S
    Last edited by Rems; 10th June 2010, 18:20.

  • #2
    Re: Batch For/If Loop and Sub question

    Hi,

    You can use 'Delayed variable expansion '
    or, use a CALL to subroutine.


    EnableDelayedExpansion
    Code:
    @echo off
    
    setlocal EnableDelayedExpansion
    for /f "delims=" %%a in (Search_Result.txt) do (
       cls
       title Processing Requests
       echo.
       echo.
       echo  Search Requests
       echo.
       echo %%a
       echo.
       set /p MC= Add Y or N to List : 
       pause [to make sure the lines were being read]
       if /i "!MC!"=="y" echo Y response
       echo %%a>>SomeFile.txt
       if /i "!MC!"=="n" echo N response
       pause [to make sure the lines were being read]
    )
    endlocal
    CALL:sub arguments
    Code:
    @echo off
    
    for /f "delims=" %%a in (Search_Result.txt) do (
       title Processing Requests & cls
       echo. & echo.
       echo  Search Requests
       echo. & echo %%a & echo.
    
       set /p MC= Add Y or N to List :
       call :response "MC" "%%a"
       pause [to make sure the lines were being read]
    )
    
    
    goto:eof  - - - subroutines - - -
    :response
    call Set "MC=%%%~1%%"
    Set "string=%~2"
    
    IF /i "%MC%"=="y" (
       set response=%MC%
       echo.%MC%es response
       echo.%string%
    ) ELSE (
       if /i "%MC%"=="n" (
         set response=%MC%
         echo.%MC%o response
         echo.%string%
       )
    )
    If not defined response echo SKIPPED
    
    (Set MC=&Set string=&set response=)
    exit /b 0
    \Rems
    Last edited by Rems; 10th June 2010, 20:10.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Batch For/If Loop and Sub question

      Rems I tried your code out..it dropped off after the Y/n Selection.. doin' a line by line.. in both cases though urz n mine I get an extra space added at the end of the transplanted lines... any thing pop out at ya?

      Geez You work fast.. I will try your code out.. I had just opend it back up to post the solution I came up with.. minus the gingerbread while building the rest... having gone thru several variations of Users being able to sort through Search hits of network stores provided by a search.bat and getting feedback I figured a Line by line review of items was less overwhelming for the avg end user..

      Moderator Edit: added [code] tags 'round the code
      Code:
      SETLOCAL ENABLEDELAYEDEXPANSION
      call %SYSTEMDRIVE%\Batches\Local.bat
      call %SYSTEMDRIVE%\Batches\Settings.bat
      title %NTWRKNAME% Search Review
      for /f "delims=" %%a in (Search_Result.txt) DO (
      Set Line=%%a
      Call:output !Line!
      )
       
      goto:eof
       
      :output !Line!
      Set Line=!Line!
      :START
      Set MC=
      echo.
      cls
      echo.
      echo     Search Review Results
      echo ________________________________________________________________
      echo.
      echo !Line!
      echo.
      Set /p MC=     Save To File List? Y/N:
      if /i "%MC%"=="y" (
      echo.!Line!>>"File_List.txt"
      ) else (
      echo     Not Added
      )
      goto EOF
      :EOF
      Last edited by wbsimsjr; 10th June 2010, 21:18. Reason: Cleaning the code up bit.. missin. on the echo to file line

      Comment

      Working...
      X