【问题标题】:Directory traversal and file selection with .bat使用 .bat 进行目录遍历和文件选择
【发布时间】:2014-04-04 22:40:30
【问题描述】:

我正在尝试编写一个 .bat 文件,该文件允许我遍历目录(向上或向下)并让我从当前目录中选择一个文件,并在例程结束时将该文件名传递出去。理想情况下,如果它位于驱动器的根目录(即 C:) 或没有更多子目录,它将处理。

(如果有更优雅的方式来完成我的要求,请随时提出建议!)

@echo off

setlocal enabledelayedexpansion

set FVAR=

:start
    ::-------------------------------------------------------
    ::  LIST - Lists all files in the current folder
    ::-------------------------------------------------------
    :LIST
    echo.
    if exist . echo ^<DIR^> .
if exist .. echo ^<DIR^> ..
for /f "tokens=* delims=" %%a in ('dir /b /ad') do (
    echo ^<DIR^> %%a
)
for /f "tokens=* delims=" %%a in ('dir /b /a-d') do (
    echo       %%a
)

::-------------------------------------------------------
::  INPUT - Requests filename as input from user
::-------------------------------------------------------
:INPUT
    echo.
    set /p FVAR="Choose your file [HINT: hit <TAB> to cycle the current folder contents]: "
    echo.

    echo %FVAR%

    if not defined FVAR (goto TRYAGAIN)

    set FVARFLAG1=0
    set FVARFLAG2=0
    set FVARFLAG=%FVARFLAG1%%FVARFLAG2%

    echo %FVARFLAG%

    if exist %FVAR%\ set "%FVARFLAG1%"=="1"
    if exist %FVAR% set "%FVARFLAG2%"=="1"

    set FVARFLAG=%FVARFLAG1%%FVARFLAG2%

    echo %FVARFLAG%

    if "%FVARFLAG%"=="00" goto TRYAGAIN
    if "%FVARFLAG%"=="01" goto FILE
    if "%FVARFLAG%"=="10" goto DIR
    if "%FVARFLAG%"=="11" goto TRYAGAIN

    goto TRYAGAIN

    :DIR
    if exist %FVAR%\ (
        echo Successfully set dir name!
        goto END
    )
    goto TRYAGAIN

    :FILE
    if exist %FVAR% (
        echo Successfully set file name!
        goto END
    )
    goto TRYAGAIN

    rem if /i "%option:"=%"=="Y" goto YES  //This line left in for future use
    rem if /i "%option:"=%"=="N" goto NO   //This line left in for future use
goto END

::-------------------------------------------------------
::  TRYAGAIN - Returns user to input menu on invalid entry
::-------------------------------------------------------
:TRYAGAIN
    echo ------------------------------
    echo Invalid selection...try again
    echo ------------------------------
goto INPUT

:END
goto :EOF

【问题讨论】:

    标签: file batch-file directory traversal


    【解决方案1】:

    我喜欢这个应用程序!使用arrays 可以让您编写更简单、更强大的代码。这是我的版本:

    @echo off
    setlocal EnableDelayedExpansion
    
    rem Select a file browsing a directory tree
    rem Antonio Perez Ayala
    
    set pageSize=30
    
    rem Load current directory contents
    :ProcessThisDir
    for /F "delims==" %%a in ('set name[ 2^>NUL') do set "%%a="
    set numNames=0
    for /D %%a in (*) do (
       set /A numNames+=1
       set "name[!numNames!]=<DIR>   %%a"
    )
    for %%a in (*.*) do (
       set /A numNames+=1
       set "name[!numNames!]=        %%a"
    )
    
    rem Show directory contents, one page at a time
    set start=1
    :ShowPage
    if %start% equ 1 (
       set "less="
    ) else (
       set "less=-=Previous page, "
    )
    set /A end=start+pageSize-1
    if %end% gtr %numNames% (
       set end=%numNames%
       set "more="
    ) else (
       set "more=+=Next page, "
    )
    cls
    echo Directory: %CD%
    echo/
    for /L %%i in (%start%,1,%end%) do echo     %%i- !name[%%i]!
    echo/
    :GetOption
    set "option="
    set /P "option=Enter desired item (%less%%more%Nothing=..): "
    if not defined option (
       cd ..
       goto ProcessThisDir
    ) else if "%option%" equ "-" (
       set /A start-=pageSize
       if !start! lss 1 set start=1
       goto ShowPage
    ) else if "%option%" equ "+" (
       if defined more set /A start+=pageSize
       goto ShowPage
    ) else if not defined name[%option%] (
       goto GetOption
    ) else if "!name[%option%]:~0,5!" equ "<DIR>" (
       cd "!name[%option%]:~8!"
       goto ProcessThisDir
    )
    
    rem Return selected file
    cls
    for %%a in ("!name[%option%]:~8!") do set "result=%%~Fa"
    echo Result="%result%"
    

    【讨论】:

    • 谢谢!这就是我想要做的(不过,我希望有一种更优雅的方式来显示/选择目录/文件,而不是必须为所有内容编号)。
    • 当你到达驱动器的根目录时,有什么方法可以制作驱动器选择器?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    相关资源
    最近更新 更多