当前目录不存在:批处理文件 [英] Current Directory does not exist: batch files
问题描述
最近,我一直在尝试使用批处理文件为我做一些重复的工作,并且检查当前目录中是否存在文件至关重要.过去,我经常使用批处理文件,但是现在使用的是新计算机,从Dropbox获取的旧批处理文件不起作用,我不确定为什么.考虑一个批处理脚本:
recently I have been trying to use batch files to do some repetitive work for me, and checking whether a file exist within a current directory is crucial. I have worked with batch files often in the past, but I am now using a new computer, and the old batch files I obtain from dropbox does not work, and I am unsure why. Consider a batch script:
@echo off
echo Build Verification...
if not exist "%cd%\build.bat" GOTO buildFail
echo Success!
pause
exit
:buildFail
echo Building of mod failed!
pause
exit
build.bat在当前目录中确实存在,但这是cmd给我的:
build.bat DOES exist, in the current directory, but this is what the cmd gave me:
Build Verification...
The syntax of the command is incorrect.
Building of mod failed!
Press any key to continue...
对于为什么会发生这种情况感到困惑,我尝试了一些变化:
Baffled at why this would happen, I tried a few variation:
(如果动态目录有问题)
(In case dynamic directory has problems)
@echo off
echo Build Verification...
if not exist "%~dp0\build.bat" GOTO buildFail
echo Success!
pause
exit
:buildFail
echo Building of mod failed!
pause
exit
但是它会产生相同的结果
But it yield same results
直接复制目录批发而不使用%dp〜0或%cd%也会产生相同的结果.
Directly copying the directory wholesale without using %dp~0 or %cd% also yields the same result.
通过将目录设置为变量也不起作用:
By setting the directory as a variable does not work either:
set randdir = "%cd%\build.bat"
if not exist "%randdir%" GOTO buildFail
但这是最奇怪的部分: 调用catch文件有效
But here is the weirdest part: Calling the catch file works
@echo off
echo Build Verification...
call "%cd%\build.bat"
这将导致build.bat的激活
This would result in the activation of build.bat
请帮助!
推荐答案
此处进行了一些小的修改:
Heres a small modification:
@Echo Off
SetLocal EnableExtensions
If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B 2
Echo=Build Verification...
If Not Exist "build.bat" GoTo buildFail
Echo=Success!
Timeout -1
Exit/B 0
:buildFail
Echo=Building of mod failed!
Timeout -1
Exit/B 1
请让我们知道它的进展情况.
Just let us know how it goes.
这篇关于当前目录不存在:批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!