Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 7 删除失败时cmd终止_Windows 7_Batch File - Fatal编程技术网

Windows 7 删除失败时cmd终止

Windows 7 删除失败时cmd终止,windows-7,batch-file,Windows 7,Batch File,我有以下bat文件mybat.bat: 1) 停止服务 2) 删除一些日志文件 3) 再次启动服务: @echo off net stop "myservice" if ERRORLEVEL 1 goto error exit :error echo There was a problem...maybe it was alreay stopped rem sometimes the terminal simply closes when trying to delete the logfi

我有以下bat文件mybat.bat:

1) 停止服务

2) 删除一些日志文件

3) 再次启动服务:

@echo off

net stop "myservice"
if ERRORLEVEL 1 goto error
exit
:error
echo There was a problem...maybe it was alreay stopped

rem sometimes the terminal simply closes when trying to delete the logfiles :-(
set folder="C:\stuff\logs"
del %folder%\*.*   /s /f  /q


net start "myservice"
if %errorlevel% == 2 echo Could not start service.
if %errorlevel% == 0 echo Service started successfully.
echo Errorlevel: %errorlevel%
我手动打开一个cmd.exe实例并运行mybat.bat,但有时它只是在试图删除日志文件且stuff\logs的内容未被删除时关闭。关于为什么会发生这种情况,以及如何在删除失败的情况下保持cmd实例的活动性,有什么想法吗


如果我等待一段时间,然后再次执行mybat,它通常会工作。

我看到一些问题。最大的问题是,你知道你的
出口就在你停止服务的下方吗?您是否打算改为使用
goto:label

另外,尝试将引号从
set folder=
行移动到
del%folder%
行,如下所示:

set folder=C:\stuff\logs
del /s /f /q "%folder%\*.*"
:: (leave the carat in the emoticon.  It escapes the parenthesis.)
(net start "myservice" && echo Great success.) || echo Fail. :^(

echo Errorlevel: %errorlevel%
或者,要同时删除子文件夹

set folder=C:\stuff\logs
rmdir /q /s "%folder%" && md "%folder%"
来,试试这个

@echo off

net stop "myservice" || echo There was a problem...maybe it was alreay stopped

:: Now that "exit" is gone, the console probably won't close any more.

set folder=C:\stuff\logs
rmdir /q /s "%folder%" && md "%folder%"

net start "myservice"

:: "if ERRORLEVEL x" checks if %errorlevel% is greater than or equal to x

if ERRORLEVEL 1 (
    echo Could not start service.
) else (
    echo Service started successfully.
)

echo Errorlevel: %errorlevel%

注意:
net start“myservice”
代码可以压缩如下:

set folder=C:\stuff\logs
del /s /f /q "%folder%\*.*"
:: (leave the carat in the emoticon.  It escapes the parenthesis.)
(net start "myservice" && echo Great success.) || echo Fail. :^(

echo Errorlevel: %errorlevel%

有关更多信息,请参阅。

我无法回答不断变化的问题:-(以上是最终版本:-)