在findstr找到它之后打开文件

时间:2012-11-19 02:20:15

标签: file file-io findstr

我正在为我的命令行类编写一个批处理。这一点非常简单。所以它在大多数情况下运行良好,直到它结束。我不知道我是否正确使用它。在findstr命令之后,我有一个if exist来打开file,但即使findstr找到了一个文件,它仍然无法打开它。

@echo off 
rem this file is a simple batch file with 4 options.
rem option 1 will display the date.
rem option 2 display the current directory you are in
rem open a file in notedpad.

:main
echo 1) display the current date.
echo 2) display the curent directory you are in.
echo 3) open a file in notepad.
echo 4) exit
echo Please enter a option; 1, 2, 3, 4.

set option=
set /p option=

if %option%//==//  GOTO main
if %option%==1 GOTO First
if %option%==2 GOTO Second
if %option%==3 GOTO Third
if %option%==4 GOTO END
if %option%==GEQ 5 
echo You have entered an invalid option.
GOTO main
:First
echo Today is %date%
echo Do you want to go to the main menu or, exit? Enter start, or end.
set /p option2= "Please Enter start, or end."
if %option2%==start GOTO main
if %option2%==end GOTO end
:Second
echo The current directory you are in is %CD%
:Third
echo Please Enter a file with extension; (ie. hello.txt)

set /p filename1=

echo Enter (y/n)
set /p option4=" You typed %filename1%. Is this correct? "

if exist %homedrive%%homepath%\%filename1% (
echo %homedrive%%homepath%\%filename1% FOUND.
start %homedrive%%homepath%\%filename1% 
) else (
if exist %CD%\%filename1% (
echo %CD%\%filename1% FOUND
start %CD%\%filename1%
GOTO restart
) else (
echo %filename1% COULD NOT BE FOUND.
GOTO drive
)
)
:restart
echo Would you like to return to the menu?
echo enter menu, or close.

set /p restart=

if %restart%==menu GOTO main
if %restart%==close (
exit
) else (
echo you have entered an invalid option.
GOTO restart
) 
:drive
echo Enter the drive the file is located on?

set /p drive1=

if exist %drive1% (
echo %drive1% FOUND.
GOTO location
) else (
echo %drive1% NOT FOUND
GOTO drive
) 

:location
%drive1%
cd \
dir /s /b | findstr /i /s "%filename1%"

if exist %filename1% (
start %filename1%
) else (
echo %filename1% NOT FOUND
)
)
:END
pause

0 个答案:

没有答案