简体   繁体   English

在命令提示符下使用 DEL 批处理文件命令删除后文件 go 在哪里?

[英]Where does the file go after deleting using DEL batch file command in the command prompt?

I have a simple batch file that runs and deletes itself after running.我有一个简单的批处理文件,它运行并在运行后自行删除。 This app was put on the desktop and will be executed using registry RUNONCE.这个应用程序放在桌面上,将使用注册表 RUNONCE 执行。 To self delete it, I'm using this command below.要自行删除它,我在下面使用此命令。

goto 2>nul & del /Q "%APP_PATH%"

Unlike this command below, the command above doesn't print any errors in the console.与下面的命令不同,上面的命令不会在控制台中打印任何错误。

del /Q "%APP_PATH%"

The batch file was deleted in the desktop.批处理文件已在桌面中删除。 When we install our internal application (not recovery app), the batch file that was deleted using DEL command will be restored to its previous location.当我们安装我们的内部应用程序(不是恢复应用程序)时,使用DEL命令删除的批处理文件将恢复到之前的位置。 I haven't got a chance to check the internal application source code yet.我还没有机会检查内部应用程序源代码。 By this level of information, does anyone has an idea of why this weird behavior happens?通过这个级别的信息,有没有人知道为什么会发生这种奇怪的行为?

The Windows command processor cmd.exe processes a batch file line by line. Windows 命令处理器cmd.exe处理批处理文件。 This means it opens a batch file, reads the next line, parses it, and if there is no more line to read, it closes the batch file.这意味着它打开一个批处理文件,读取下一行,解析它,如果没有更多行要读取,它关闭批处理文件。 Then it executes the command(s) on the parsed line.然后它在解析的行上执行命令。 See How does the Windows Command Interpreter (CMD.EXE) parse scripts?请参阅Windows 命令解释器 (CMD.EXE) 如何解析脚本?

The Windows command processor exits the processing of a batch file if there is a serious syntax here.如果这里有严重的语法,Windows 命令处理器将退出批处理文件的处理。 This error condition handling is used here.此处使用此错误条件处理。

Create a batch file with following lines:使用以下行创建一个批处理文件:

@echo off
goto
echo Third line!

Open a command prompt and run this batch file by typing its full qualified file name, ie drive + path + name + extension enclose in double quotes.打开命令提示符并通过键入其完整限定文件名来运行此批处理文件,即驱动器+路径+名称+扩展名用双引号括起来。

There is the following error message output by cmd.exe because of batch label is missing on second line.由于第二行缺少批处理 label, cmd.exe出现以下错误消息 output。

No batch label specified to GOTO command.没有为 GOTO 命令指定批次 label。

The third line is not executed anymore by cmd.exe because of batch file processing is exited after execution of second command line because of this serious syntax error on which it is unclear for cmd.exe where to continue the batch file processing.第三行不再由cmd.exe执行,因为在执行第二个命令行后退出批处理文件处理,这是因为cmd.exe不清楚在何处继续批处理文件处理的严重语法错误。

Now let us look on the command line:现在让我们看一下命令行:

goto 2>nul & del /Q "%APP_PATH%"

This is a command line with two commands on one line as described in detail by my answer on Single line with multiple commands using Windows batch file .这是一个命令行,在一行上有两个命令,正如我在Single line with multiple commands using Windows batch file上的回答中详细描述的那样。

The first command goto with the missing batch label results in exiting the batch file processing.缺少批处理 label 的第一个命令goto导致退出批处理文件处理。 The error message output by cmd.exe in this case is redirected with 2>nul to device NUL to suppress it.在这种情况下, cmd.exe的错误消息 output 将使用2>nul重定向到设备NUL以抑制它。 But the second command del /Q "%APP_PATH%" with the already expanded environment variable %APP_PATH% is already in the commands queue.但是第二个命令del /Q "%APP_PATH%"已经扩展了环境变量%APP_PATH%已经在命令队列中。 For that reason this command is executed next.出于这个原因,接下来会执行该命令。

Then cmd.exe does not even try to open the batch file once more to read the third line as the invalid goto command results in exiting batch file processing which in this case does not even exist anymore if %APP_PATH% references the name of the batch file.然后cmd.exe甚至不再尝试打开批处理文件以读取第三行,因为无效的goto命令导致退出批处理文件处理,如果%APP_PATH%引用批处理的名称,在这种情况下甚至不再存在文件。

BTW: APP_PATH is a strange because of misleading environment variable name for a file name with full path.顺便说一句: APP_PATH很奇怪,因为具有完整路径的文件名具有误导性的环境变量名称。

Here is an even better example:这是一个更好的例子:

Create first a batch file with name Test1.cmd with following lines:首先使用以下行创建一个名为Test1.cmd的批处理文件:

@echo off
echo %~nx0 is calling Test2.cmd.
call "%~dp0Test2.cmd"
echo %~nx0 is processed further.

Create second a batch file with name Test2.cmd with following lines:使用以下行创建第二个名为Test2.cmd的批处理文件:

@echo off
set "BatchFile=%~f0"
echo %~nx0 is deleting itself now.
goto 2>nul & del /A /F "%BatchFile%"

Then run from within a command prompt window Test1.cmd .然后从命令提示符 window Test1.cmd中运行。 The output is: output 是:

Test1.cmd is calling Test2.cmd.
Test2.cmd is deleting itself now.
Test1.cmd is processed further.

It can be seen that Test2.cmd deleted itself without showing any error message by cmd.exe processing Test2.cmd .通过cmd.exe处理Test2.cmd可以看出Test2.cmd删除了自己而没有显示任何错误消息。 But processing of Test1.cmd is continued after Test2.cmd deleted itself.但是在Test2.cmd删除自身之后继续处理Test1.cmd

The option /Q is not really necessary on deleting a single file and the file definitely exists on running the command. /Q选项在删除单个文件时并不是真正需要的,并且该文件在运行命令时肯定存在。 The option /A is useful to delete the batch file even if it has the hidden attribute set as otherwise del would not delete it.选项/A对于删除批处理文件很有用,即使它设置了隐藏属性,否则del不会删除它。 The option /F is useful to delete the batch file even if the read-only attribute is set.即使设置了只读属性,选项/F也可用于删除批处理文件。

Another command method is using as last command line in a batch file which should delete itself the command line:另一种命令方法是使用批处理文件中的最后一个命令行,它应该删除自己的命令行:

del /A /F "%~f0" & exit

But it has a disadvantage as it can be seen on creating a Test2.cmd with the lines:但它有一个缺点,因为它可以在创建Test2.cmd时看到:

@echo off
echo %~nx0 is deleting itself now.
del /A /F "%~f0" & exit

Run Test1.cmd from within the command prompt window.从命令提示符 window 中运行Test1.cmd The batch file Test2.cmd deletes itself, but command exit results in exiting the cmd.exe opened with opening the command prompt window and processing both batch files.批处理文件Test2.cmd会自行删除,但命令exit会导致退出cmd.exe并打开命令提示符 window 并处理这两个批处理文件。 For that reason Test1.cmd is not further processed and the console window is closed immediately.出于这个原因, Test1.cmd没有进一步处理,控制台 window 立即关闭。

Appending the option /B after command exit would result in not exiting cmd.exe , but two error messages would be output by cmd.exe because of not finding anymore Test2.cmd . Appending the option /B after command exit would result in not exiting cmd.exe , but two error messages would be output by cmd.exe because of not finding anymore Test2.cmd .

For that reason the usage of goto 2>nul & del /A /F "%~f0" is a best practice example for self-deletion of a batch file.因此,使用goto 2>nul & del /A /F "%~f0"是自删除批处理文件的最佳实践示例。

BTW: The order or the two commands matters.顺便说一句:顺序或两个命令很重要。 The usage of del /A /F "%~f0" & goto 2>nul would result in an error message output by cmd.exe as in this case it tries to open the already deleted batch file once more.使用del /A /F "%~f0" & goto 2>nul会导致 cmd.exe 出现错误消息cmd.exe ,因为在这种情况下,它会尝试再次打开已删除的批处理文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM