使用.bat遍历目录和选择文件 [英] Directory traversal and file selection with .bat

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

问题描述

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

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


推荐答案

我喜欢这个应用程序! 数组的使用允许您编写更简单,更强大的代码。这是我的版本:

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

这篇关于使用.bat遍历目录和选择文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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