dos batch script to track user input

Microsoft Xbox 360 premium system
December 9, 2009 at 19:18:30
Specs: Windows XP
Following is the code i worked out to display the sub-directories from two different directories with initial as 1,2,3 ...

set /A Counter=1
if exist "c:\test\abc" (
FOR /f "tokens=*" %%G IN (' dir/b c:\test\abc\*.*') do (
echo !Counter!. %%G "c:\test\abc"
set /A Counter+=1
)
)

if exist "c:\test1" (
FOR /f "tokens=*" %%G IN (' dir/b c:\test1\*.*') do (
echo !Counter!. %%G "c:\test1"
set /A Counter+=1
)
)

echo %Counter%. Other (manually type the path )
echo.

I prompt user to enter the number i.e related with counter value(1,2,3..) displayed .
set /p usrin=Which number do you want to use?

Now i need a batch script such that
if user input is the last number(i.e counter value) echo "last number" and associated directory name
if user input is 1 to before last number echo "number" and and associated directory name .

Your help is highly appreciated.


See More: dos batch script to track user input

Report •


#1
December 10, 2009 at 00:36:30
not-a-prob?

set /a ttl=0
change:
set /A Counter=1
to:
set /A Counter=0

:: this eliminates having to test for "one lessthan", i think...
:: also move the incrementation, as shown
if exist "c:\test\abc" (
FOR /f "tokens=*" %%G IN (' dir/b c:\test\abc\*.*') do (
set /A Counter+=1
echo !Counter!. %%G "c:\test\abc"

at end of each loop:
set ttl=!ttl!+!Counter!

then:
set /p usrin=Which number do you want to use?
if !usrin! gtr !ttl! (
echo exceeds menu, not accepted: !usrin!
) else (
echo ok, here we go with !usrin!...)

none of this addresses "validation of user input" however, which is always the hardest part of any system, believe me!


Report •

#2
December 10, 2009 at 01:13:04
What happened to this thread.

Edit: script removed see below:


Report •

#3
December 10, 2009 at 02:44:41
I fixed a couple of bugs.....

@ECHO OFF
setlocal enabledelayedexpansion
set counter=0
set dir1="c:\test\abc"
set dir2="c:\test1"
for /f %%a in (' 2^>nul dir/b/a "%dir1:"=%\*" ^| find /c /v ""') do set fd=%%a
FOR /f "tokens=*" %%G IN (' 2^>nul dir/b/a "%dir1:"=%\*" ^& 2^>nul dir/b/a "%dir2:"=%\*"') do (
    set /A Counter+=1
    if !counter! leq !fd! (
        echo !Counter!. "%dir1:"=%\%%~G"
    ) else (
        echo !Counter!. "%dir2:"=%\%%~G"
    )
)
if %counter%==0 echo No files found, manual selection only.

:select
set usrin=
set /p usrin=Which number do you want to use, 0 exits? Alternatively you can type in the path:
if defined usrin (
    if "!usrin!"=="0" exit /b
    set test=!usrin!
    for /l %%a in (0 1 9) do if defined test set test=!test:%%a=!
    if not defined test (
        if !usrin! leq !counter! (
            goto valid
        )
    )        
)
set usrin=!usrin:"=!
if exist "!usrin!" (
    for %%a in ("!usrin!") do (
        echo Manual input: "%%~fa"
        set selection="%%~fa"
    )
    goto break
)

echo Bad input.
goto select

:valid
FOR /f "skip=%usrin% tokens=*" %%G IN (' echo d ^& 2^>nul dir/b/a "%dir1:"=%\*" ^& 2^>nul dir/b/a "%dir2:"=%\*" ') do (
    if %usrin% leq %fd% (
        echo Selection %usrin%: "%dir1:"=%\%%~G"
        set selection="%dir1:"=%\%%~G"
    ) else (
        echo Selection %usrin%: "%dir2:"=%\%%~G"
        set selection="%dir2:"=%\%%~G"
    )
    goto break

)

:break
echo You chose !selection!
pause


Batch Variable how to


Report •

Related Solutions

#4
December 10, 2009 at 19:25:04
Thank you , your time is highly appreciated .

Report •

#5
December 10, 2009 at 19:39:19
To: Response number 3 .
Currently, it list not only the subdirectories but files too. If i need to display the subdirectories starting from test, what changes do i need to make?
I did something like set dir1="c:\test\abc\test*.*"
set dir2="c:\test1\test*.*" to list only the sub-directories starting with word 'test' but after making these changes in above code. It results saying " the file could not be found".
Please help me to list only the selected sub directories.

Report •

#6
December 10, 2009 at 22:08:49
Only folder that start with test:

@ECHO OFF
setlocal enabledelayedexpansion
set counter=0
set dir1="c:\test\abc"
set dir2="c:\test1"
for /f %%a in (' 2^>nul dir/b/ad "%dir1:"=%\test*" ^| find /c /v ""') do set fd=%%a
FOR /f "tokens=*" %%G IN (' 2^>nul dir/b/ad "%dir1:"=%\test*" ^& 2^>nul dir/b/ad "%dir2:"=%\test*"') do (
    set /A Counter+=1
    if !counter! leq !fd! (
        echo !Counter!. "%dir1:"=%\%%~G"
    ) else (
        echo !Counter!. "%dir2:"=%\%%~G"
    )
)
if %counter%==0 echo No folders found, manual selection only.

:select
set usrin=
set /p usrin=Which number do you want to use, 0 exits? Alternatively you can type in the path:
if defined usrin (
    if "!usrin!"=="0" exit /b
    set test=!usrin!
    for /l %%a in (0 1 9) do if defined test set test=!test:%%a=!
    if not defined test (
        if !usrin! leq !counter! (
            goto valid
        )
    )        
)
set usrin=!usrin:"=!
if exist "!usrin!" (
    for %%a in ("!usrin!") do (
        echo Manual input: "%%~fa"
        set selection="%%~fa"
    )
    goto break
)

echo Bad input.
goto select

:valid
FOR /f "skip=%usrin% tokens=*" %%G IN (' echo d ^& 2^>nul dir/b/ad "%dir1:"=%\test*" ^& 2^>nul dir/b/ad "%dir2:"=%\test*" ') do (
    if %usrin% leq %fd% (
        echo Selection %usrin%: "%dir1:"=%\%%~G"
        set selection="%dir1:"=%\%%~G"
    ) else (
        echo Selection %usrin%: "%dir2:"=%\%%~G"
        set selection="%dir2:"=%\%%~G"
    )
    goto break

)

:break
echo You chose !selection!
pause


Batch Variable how to


Report •

#7
December 24, 2009 at 19:56:27
hi , the above code does not work when the dir c:\test\abc is not found and gives:

The following usage of the path operator in batch-parameter
substitution is invalid: %~G

For valid formats type CALL /? or FOR /?
The syntax of the command is incorrect.

Could you please give the solution such that if it does not find one of the dir, it should display from another dir with out problem. If none of the two of the two directories are available it should display message that both the directories are not available.


Report •

#8
December 24, 2009 at 20:19:59

The following usage of the path operator in batch-parameter substitution is invalid: %~G

This means that one of the occurrences of %%~G is missing a percentage sign (ie %~G). I have had a good look and can't find the problem, please check that a percentage sign hasn't been deleted along the way. I can't reproduce this problem without removing anything.

If none of the two of the two directories are available it should display message that both the directories are not available.

It already does this, although not in so many words.


Could you please give the solution such that if it does not find one of the dir, it should display from another dir with out problem.

Make up your mind! How would I know which dir? Pick one an random? esp?


Batch Variable how to


Report •


Ask Question