简体   繁体   English

使用 Windows 命令删除文件并显示已删除文件的列表

[英]Delete files using a Windows command and display the list of deleted files

I want to delete files from a folder with pattern.我想从带有模式的文件夹中删除文件。 I am using a C# program which calls a Windows command and prints the log of the command.我正在使用调用 Windows 命令并打印命令日志的 C# 程序。 Currently I am using this command:目前我正在使用这个命令:

del Test\*xyz* /s

Using /s switch, I get the list of files which are deleted in the log.使用 /s 开关,我获得了在日志中删除的文件列表。 But /s switch also deletes the files from the subdirectory which I don't want.但是 /s 开关也会从我不想要的子目录中删除文件。 Is there a way to delete files and get list of files?有没有办法删除文件并获取文件列表?

If I understood your question correct, you want to delete files within a folder (without files in subdirectories).如果我理解您的问题是正确的,您想删除文件夹中的文件(子目录中没有文件)。 You can achieve this by using a for -loop within a Batch-File.您可以通过在批处理文件中使用for循环来实现此目的。

For your use case you would write something like the following into a batch ( .bat ) file:对于您的用例,您可以将以下内容写入批处理 ( .bat ) 文件中:

@ECHO OFF

for /f %%i in ('dir Test\*xyz* /b /a-d') do (
  echo %%i
  del %%i
)

The loop iterates through every line in the result of the dir command and prints the name of the file into the console.循环遍历dir命令结果中的每一行,并将文件名打印到控制台。 Afterwards it gets deleted.之后它会被删除。

As the documentation of the for -loop states:正如for循环的文档所述:

  • Iterating and file parsing迭代和文件解析

    Use file parsing to process command output, strings, and file content.使用文件解析来处理命令输出、字符串和文件内容。 Use iterative variables to define the content or strings that you want to examine, and use the various ParsingKeywords options to further modify the parsing.使用迭代变量定义要检查的内容或字符串,并使用各种ParsingKeywords选项进一步修改解析。 Use the ParsingKeywords token option to specify which tokens should be passed as iterative variables.使用ParsingKeywords标记选项指定应作为迭代变量传递哪些标记。 Note that when used without the token option, /f will only examine the first token .请注意,在不带 token 选项的情况下使用时, /f 将仅检查第一个 token

    File parsing consists of reading the output, string, or file content, and then breaking it into individual lines of text and parsing each line into zero or more tokens.文件解析包括读取输出、字符串或文件内容,然后将其分解为单独的文本行并将每一行解析为零个或多个标记。 The for loop is then called with the iterative variable value set to the token.然后使用设置为标记的迭代变量值调用 for 循环。 By default, /f passes the first blank separated token from each line of each file.默认情况下,/f 从每个文件的每一行传递第一个空格分隔的标记。 Blank lines are skipped.空行被跳过。

    The syntaxes are:语法是:

     for /f ["<ParsingKeywords>"] {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>] for /f ["ParsingKeywords"] {%%|%}<Variable> in ("<LiteralString>") do <Command> [<CommandLineOptions>] for /f ["<ParsingKeywords>"] {%%|%}<Variable> in ('<Command>') do <Command> [<CommandLineOptions>]

So in the above example we used the last version (used output from a command).所以在上面的例子中,我们使用了最后一个版本(使用了命令的输出)。 The specified command is the dir command.指定的命令是dir命令。

As the documentation of the dir command states:正如dir命令的文档所述:

/b Displays a bare list of directories and files, with no additional information. /b显示目录和文件的空列表,没有附加信息。 /b overrides /w. /b 覆盖 /w。

and

/a[[:]<Attributes>] Displays only the names of those directories and files with the attributes that you specify. /a[[:]<Attributes>]仅显示具有您指定的属性的那些目录和文件的名称。 If you omit /a, dir displays the names of all files except hidden and system files.如果省略 /a,则 dir 将显示除隐藏文件和系统文件之外的所有文件的名称。 If you use /a without specifying Attributes, dir displays the names of all files, including hidden and system files.如果使用 /a 而不指定 Attributes,dir 将显示所有文件的名称,包括隐藏文件和系统文件。

The following list describes each of the values that you can use for Attributes.以下列表描述了可用于属性的每个值。 Using a colon (:) is optional.使用冒号 (:) 是可选的。 Use any combination of these values, and do not separate the values with spaces.使用这些值的任意组合,并且不要用空格分隔这些值。

d Directories d目录
h Hidden files h隐藏文件
s System files s系统文件
l Reparse points l解析点
r Read-only files r只读文件
a Files ready for archiving a准备存档a文件
i Not content indexed files i不是内容索引文件
- Prefix meaning "not" -前缀意思是“不是”

so you would need to use /b for the bare list of filenames and /ad to exclude possible directories in the result list.所以你需要使用/b作为文件名的裸列表和/ad来排除结果列表中可能的目录。

On a side note: You can also achive these things using plain C#.附带说明:您也可以使用普通的 C# 来实现这些功能。 If you need help, just let me know ;-)如果您需要帮助,请告诉我 ;-)

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

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