提示用户选择批处理文件中的目录 [英] Prompting user to select directory in batch file

查看:90
本文介绍了提示用户选择批处理文件中的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我试图让用户从指定目录中选择要选择的文件夹,然后将该文件夹添加到地址中以执行程序

I am attempting to have users select which folder to choose from within a specified directory and then add that folder to the address in order to execute a program within that directory.

例如,!acct!是我要询问最终用户的变量他们想要访问哪个帐户。此帐户是所有其他文件夹的起始或根文件夹。除文件夹的第二个令牌外,所有帐户的文件夹结构均相同。即) 123456789\ * \program\setup\

For example, !acct! is the variable that I am using to ask the end user which account they want to access. This account is the starting or root folder for all other folders. The folder structure remains that same across all accounts EXCEPT for the second token of the folder. i.e) 123456789\*\program\setup\

* 是我希望最终用户从中选择多个文件夹的文件夹。

The * is the folder where multiple folders exist that I would like the end user to choose from.

一旦选择,我希望脚本添加该目录,以便可以执行程序。

Once chosen, I would like the script to add that directory so that the program could be executed.

我想通过设置变量 acctDir = C:\Users\jdoe\Desktop\!acct!\ 将允许我为根目录添加acct号,然后运行 FOR 循环,该循环将允许我在集合<$ c中使用通配符$ c>用于(!acctdir!* media1\setup)中的/ d %% i做(启动!app!)

I thought by setting the variable acctDir=C:\Users\jdoe\Desktop\!acct!\ would allow me to add the acct number for the root directory, then run a FOR loop that would allow me to use a wild card in the set for /d %%i in ( !acctdir!*media1\setup ) do ( start !app! )

任何

@echo off
setlocal enabledelayedexpansion

:beginning
echo.
echo.
echo ===========================================================
echo Starting on !date! !time! on !Computername!
echo ===========================================================
echo.

goto :main


:main
setlocal

    set /P acct=Please type the 9 digit account number you would like to restore: 
    set acctDir=C:\Users\jdoe\Desktop\!acct!\
    set app=setup.exe /cd
    set log=c:\logs.txt


    echo. Starting on !date! !time! on !Computername! >> !log!
    echo.
    echo The account number you selected is: !acct!
    echo.

    goto :user

:user

    set /p answer=Is this correct (Y/N)? 
    echo.

    if /i !answer!==y goto :yes (
    ) else ( 
        echo.
        echo Ok. Let's try again^^!
        echo.
        Pause
        cls
        goto :beginning
        )
    )

:yes

    for /d %%i in ( !acctdir!*media1\setup ) do (
        start !app!
    )

endlocal
goto :eof


推荐答案

您可以尝试以下操作:

set c=0
For /f %%a in ('dir !acctDir! /B /A D') do (
    set /a c+=1
    echo !c!    %%a
    set dir!c!=%%a
)

echo. 
set /p uin="Select a directory [1-!c!]: "
set udir=!dir%uin%!

echo Selected - %udir%

这会循环遍历以下目录!acctDir!,并提示用户根据分配给它的号码选择目录。

This loops through a directory listing of !acctDir! and prompts the user to select a directory based on the number assigned to it.

!acctDir!\%udir%将成为目录的位置。

这篇关于提示用户选择批处理文件中的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆