批处理脚本,使编号菜单可以查看日志文件

时间:2013-10-18 14:37:08

标签: batch-file scripting cmd

我最近开始学习如何制作批处理文件。我有一个包含大量互联网相关日志文件的文件夹。当我运行.cmd文件(位于同一文件夹中)时,我希望它能够找出文件夹中有多少个日志文件,并从中创建一个带编号的菜单。因此,假设文件夹中有20个文件,那么用户必须能够从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号,程序将终止,因为我无法弄清楚如何使用记事本实际打开所需的日志文件。

2 个答案:

答案 0 :(得分: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

如果您需要任何解释,请与我们联系。

答案 1 :(得分: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%