批处理:仅删除指定文件的命令

时间:2021-08-05 02:23:48

I want to make a batch file that will delete a certain file from a certain (server) path. The following code will delete 123.pdf but if I do not mention the file name specifically it asks me if I want to delete all contents of the folder.

我想制作一个批处理文件,它将从某个(服务器)路径中删除某个文件。以下代码将删除123.pdf,但如果我没有具体提到文件名,它会询问我是否要删除该文件夹的所有内容。

The paths and file names are auto inserted by another script and sometimes the result will be null, that means that no file name will be inserted. I will set it to not prompt for yes/no permission and this is dangerous because if no file name is given it will delete the whole folders content.

路径和文件名由另一个脚本自动插入,有时结果为null,这意味着不会插入文件名。我将其设置为不提示是/否许可这是危险的,因为如果没有给出文件名,它将删除整个文件夹内容。

I only want it to delete only that one exact named file and when/if the command line contains only the path, without the file name - skip and do not delete anything.

我只希望它只删除一个确切命名的文件,当/如果命令行只包含路径,没有文件名 - 跳过并且不删除任何内容。

Exemple:

del "\\tbiscan\SCAN\Dosare de executare scanate\Curtea de Apel Bacau\BACAU\280555A\123.pdf"

Please help

1 个解决方案

#1


1  

You can try with

你可以试试

2>nul ( <"%targetFile%" break ) && del "%targetFile%"

Asumming the %targetFile% variable contains the reference to the file (or to a folder) to delete this code:

对%targetFile%变量进行填充包含对文件(或文件夹)的引用以删除此代码:

  • Tries to open the file for reading using a input redirection operator (the break command will do nothing with the file, but we need a command that does not raise an errorlevel but it is not necessary to use break, something as call; or type nul can be used).
    • If the file can be opened there will be not any error and the conditional operator && (execute next command if the previous did not fail) will execute the del command.
    • 如果文件可以打开则不会出现任何错误,条件运算符&&(如果前一个命令没有失败则执行next命令)将执行del命令。

    • If the file can not be opened (not readable, it is a folder, it does not exist), the redirection can not be executed and an error is generated (hidden by the 2>nul) and the command after the && conditional operator is not executed.
    • 如果文件无法打开(不可读,它是一个文件夹,它不存在),则无法执行重定向并生成错误(由2> nul隐藏)和&&条件运算符后的命令没有执行。

  • 尝试使用输入重定向操作符打开文件进行读取(break命令对文件不起作用,但是我们需要一个不会引发错误级别的命令,但是没有必要使用break,作为调用;或者键入nul可以使用)。如果文件可以打开则不会出现任何错误,条件运算符&&(如果前一个命令没有失败则执行next命令)将执行del命令。如果文件无法打开(不可读,它是一个文件夹,它不存在),则无法执行重定向并生成错误(由2> nul隐藏)和&&条件运算符后的命令没有执行。

#1


1  

You can try with

你可以试试

2>nul ( <"%targetFile%" break ) && del "%targetFile%"

Asumming the %targetFile% variable contains the reference to the file (or to a folder) to delete this code:

对%targetFile%变量进行填充包含对文件(或文件夹)的引用以删除此代码:

  • Tries to open the file for reading using a input redirection operator (the break command will do nothing with the file, but we need a command that does not raise an errorlevel but it is not necessary to use break, something as call; or type nul can be used).
    • If the file can be opened there will be not any error and the conditional operator && (execute next command if the previous did not fail) will execute the del command.
    • 如果文件可以打开则不会出现任何错误,条件运算符&&(如果前一个命令没有失败则执行next命令)将执行del命令。

    • If the file can not be opened (not readable, it is a folder, it does not exist), the redirection can not be executed and an error is generated (hidden by the 2>nul) and the command after the && conditional operator is not executed.
    • 如果文件无法打开(不可读,它是一个文件夹,它不存在),则无法执行重定向并生成错误(由2> nul隐藏)和&&条件运算符后的命令没有执行。

  • 尝试使用输入重定向操作符打开文件进行读取(break命令对文件不起作用,但是我们需要一个不会引发错误级别的命令,但是没有必要使用break,作为调用;或者键入nul可以使用)。如果文件可以打开则不会出现任何错误,条件运算符&&(如果前一个命令没有失败则执行next命令)将执行del命令。如果文件无法打开(不可读,它是一个文件夹,它不存在),则无法执行重定向并生成错误(由2> nul隐藏)和&&条件运算符后的命令没有执行。