You are not logged in.

#1 02 Jul 2007 23:23

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 121
Website

IF condition NEQ groupofvariables - question

I have a folder with a group of text files prefixed with a, so they look like ahello.txt, agreetings.txt etc. I am trying to create a choice menu that will enable a user to press a number and select a different text file so it can be further processed. However I dont know how to create a condition to use the IF command with a group of variables. This is the code of what I'm trying to acheive.

@echo off
title=%~n0
setlocal enabledelayedexpansion

REM Scan folder for card avaliable txt files prefexied with a and create pseudo-array
for /f "tokens=*" %%a in ('dir/b "a*.txt"') do (
    set text=%%a
    set text=!text:~1!
    set _cards!text!=!text!
    )

REM Bring the card files avaliable into a choice menu prefixed with number
echo --=== Chose a Card Text File ===--
for /f "usebackq tokens=2* delims=_=" %%a in (`set _cards`) do (
    echo !_count!>nul & set /a _count+=1
    set !_count!=%%a
    echo (!_count!^) %%a
    )
:menu
set /p _choice=Choose a number: 
if _choice neq (to one of the _count variables) goto :menu

cmd, vbs, ps, bash
autoit, python, swift

Offline

#2 04 Jul 2007 11:12

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: IF condition NEQ groupofvariables - question

My preferred way is by reusing the for loop we already have in place for displaying the menu.

I'd do it something like this:

@echo off
setlocal enabledelayedexpansion

set count=0

for /f "tokens=*" %%a in ('dir/b "a*.txt"') do (
    set text=%%a
    set text=!text:~1!
    set /a count+=1
    set _cards/!text!=!count!
)

:top
cls
call :menu/dis

set _choice=
set /p "_choice=Choose a number: "

if /i "%_choice%" EQU "Q" goto :EOF

call :menu/chk

if not defined action goto :top

echo:You chose: %action%

REM whatever other actions you want to do would go here

pause

goto :EOF

:menu/dis
:menu/chk

set action=

for /f "tokens=2* delims=/=" %%a in ('set _cards/') do (
    if /i "%0" EQU ":menu/dis" echo:^(%%~b^) %%~a
    if /i "%0" EQU ":menu/chk" (
        if /i "%_choice%" EQU "%%~b" (
            set action=%%a
        )
    )
)

goto :EOF

Last edited by bluesxman (04 Jul 2007 11:24)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by