我需要帮助,我编写了这段代码,它查找文件“ filename.txt,如果找到了另一个副本” filemame2.txt,但我想问我在哪个驱动器c:\或d:\我想搜索和更改找到的文件。你能帮我吗?
@echo off
echo search...
if exist "filename.txt" (
echo file filename.txt found.
xcopy c:\filename.txt /y filename2.txt
) else (
echo file filemame.txt does not found.
)
echo finished.
echo press any key to exit...
pause >nul
答案 0 :(得分:1)
有许多不同的方法可以同时使用choice
或set /p
一种方法是简单地使用goto,根据choice
在选择实例中设置的错误级别,我们仅使用字母C
和D
,并且不需要输入,您只要按下键盘上的C
或D
,就会执行选择命令。
@echo off
echo select the drive where the file should exist..
choice /C CD
if %errorlevel%==2 call goto :ddrive
if %errorlevel%==1 call goto :cdrive
:cdrive
if exist "c:\filename.txt" (xcopy /y "c:\filename.txt" /y "c:\filename2.txt") else (echo C:\filename.txt does not exist)
goto :EOF
:ddrive
if exist "d:\filename.txt" (xcopy /y "d:\filename.txt" "d:\filename2.txt") else (echo D:\filename.txt does not exist)
echo finished.
pause
由于您只有2个选项,第二个if可以删除,但是我保留了,因为您可能想添加更多驱动器。
要获取每一个方面的帮助,请打开cmd.exe
和ru choice /?
,set /?
和if /?
根据您的评论,如果要在提示符中包括完整路径(即在提示符下),则必须键入驱动器号和认为文件存在的路径。即c:\some dir
:
@echo off
set /p "source=Please enter path to search file :"
set /p "dest=Please enter path to copy file to :"
if exist "%source%\filename.txt" (xcopy /y "%source%\filename.txt" /y "%dest%\filename2.txt") else (echo C:\filename.txt does not exist)
echo finished.
pause
如果要搜索文件,则需要使用dir /a-d /b /s filename.txt
并将其设置为变量。