2

我最近开始学习如何制作批处理文件。我有一个文件夹,其中包含一堆与 Internet 相关的日志文件。当我运行 .cmd 文件(位于同一文件夹中)时,我希望它能够找出文件夹中有多少日志文件并从中创建一个编号菜单。假设文件夹中有二十个文件,那么用户必须能够从 1 到 21 进行选择。21 将关闭批处理文件。这是我到目前为止所做的:

@echo off
setlocal enableextensions enabledelayedexpansion
set RawData1=TempData%random%.tmp
set FileCtr=0    

:MAIN
dir *.log /b | findstr /i /n ".log" > %RawData1%

for /f "tokens=1 delims=:" %%a in (%RawData1%) do set FileCtr=%%a
set /a ExitCode=%FileCtr% + 1
set UserChoice=%ExitCode%

echo.
echo +++++++++++++++++++++++++++
echo     Weblog File Viewer                              
echo +++++++++++++++++++++++++++    
for /f "tokens=1-2 delims=:." %%a in (%RawData1%) do echo %%a. %%b
echo %Exitcode%. To Quit.
set /p UserChoice= Choose item number from menu (%UserChoice%):    

echo\
echo user entered: %UserChoice%
pause
:THEEND
del /q %RawData1%

所以这个批处理文件现在可以做的是它计算出日志文件的数量并从中创建一个编号的菜单。当然它不会显示我想要的文件类型。因此,例如“Kelley-Blue-Book.log”仅显示为“Kelley-Blue-Book”。但是,如果用户从列表中选择数字 4,程序将终止,因为我无法弄清楚如何使用记事本实际打开所需的日志文件。

4

2 回答 2

2

这应该做你想要的:

@echo Off
setlocal EnableDelayedExpansion
set "Count=0"
pushd "%~dp0"

echo.
echo +++++++++++++++++++++++++++
echo     Weblog File Viewer                              
echo +++++++++++++++++++++++++++  
for %%A in (*.log) do (
    set /a "Count+=1"
    set "Menu[!Count!]=%%~fA"
    set "Number=   !Count!"
    echo !Number:~-3!. %%~nA
)
set /a "Count+=1"
set "Number=   %Count%"
echo %Number:~-3%. To Quit.

:Prompt
set "UserChoice="
set /p "UserChoice= Choose item number from menu (%Count%):"
if not defined UserChoice goto Prompt
set "UserChoice=%UserChoice:"=%"

if "%UserChoice%"=="%Count%" goto Done
for /f "tokens=1,* delims==" %%A in ('set Menu') do (
    if /i "Menu[%UserChoice%]"=="%%~A" (
        notepad "%%~fB"
        set "UserChoice="
    )
)
if defined UserChoice echo Invalid Choice.
goto Prompt

:Done
popd
endlocal
exit /b 0

如果您需要任何解释,请告诉我。

于 2013-10-18T16:28:00.423 回答
1
@echo off
    setlocal enableextensions 

    set RawData1=TempData%random%.tmp

    rem Get numbered list of files
    dir /b "*.log" | findstr /i /n ".log" > %RawData1%

    rem We could use 0 as exitCode, 
    rem but to keep original behaviour
    rem lets count the number of files
    for /F "tokens=*" %%f in ('type %RawData1% ^| find /c /v "" ') do set /A ExitCode=%%f + 1

    if %ExitCode%==0 (
        echo No log files
        goto endProcess
    )

    rem show menu
    for /f "tokens=1-2 delims=:." %%a in (%RawData1%) do echo %%a. %%b
    echo %Exitcode%. To Quit.
    set UserChoice=%ExitCode%
    set /p UserChoice= Choose item number from menu (%UserChoice%):

    if "%UserChoice%"=="" goto :EOF
    if "%UserChoice%"=="%ExitCode%" goto endProcess

    rem Search indicated file in list
    set SelectedFile=
    for /f "tokens=2 delims=:" %%f in ('findstr /B "%UserChoice%:" %RawData1%') do set SelectedFile=%%f

    if "%SelectedFile%"=="" (
        echo Incorrect selection
        goto endProcess
    )

    if not exist %SelectedFile% (
        echo File deleted
        goto endProcess
    )

    notepad %SelectedFile%

:endProcess

    del /q %RawData1%
于 2013-10-18T16:34:49.150 回答