Directory traversal and file selection with .bat

ぐ巨炮叔叔 提交于 2020-01-16 19:49:13

问题


I am trying to write a .bat file that allows me to traverse through directories (up or down) and let me select a file from the current directory, passing that filename out at the end of the routine. Ideally, it would handle if it is at the root of a drive (i.e. C:) or that there are no more sub directories.

(If there are more elegant ways of doing what I am asking, please feel free to suggest them!)

@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:


I like this application! The use of arrays allows you to write simpler and more powerful code. This is my version:

@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%"


来源:https://stackoverflow.com/questions/22874194/directory-traversal-and-file-selection-with-bat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!