使用.bat进行目录遍历和文件选择

时间:2014-04-04 22:40:30

标签: file batch-file directory traversal

我正在尝试编写一个允许我遍历目录(向上或向下)的.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

1 个答案:

答案 0 :(得分:0)

我喜欢这个应用程序!使用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%"