简体   繁体   English

Windows等效find -delete

[英]Windows equivalent find -delete

What is the Windows/DOS equivalent to this Linux command? 与此Linux命令等效的Windows / DOS是什么?

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete

I know how to delete all the files but not how to specify an exception (ie: not deleting __init__.py ) 我知道如何删除所有文件,但不知道如何指定异常(即:不删除__init__.py

cmd.exe doesn't support wildcards in several levels of a path (PowerShell does) so you've to emulate this somehow. cmd.exe在路径的多个级别中不支持通配符(PowerShell则支持),因此您必须以某种方式进行模拟。

Cmd line: Cmd行:

for /f "delims=" %F in ('Dir /B /S .\*.py ^|findstr /IE "\\migrations\\[^\\]*.py"^|findstr /IEV "\\__init__.py" ') Do @echo del "%F"

If the output looks OK, remove the echo 如果输出看起来正常,请删除echo
In a batch file double the percent signs %F => %%F 在批处理文件中,将百分号%F => %%F加倍

PowerShell 电源外壳

Get-ChildItem .\*\migrations\*.py -exclude __init__.py | Remove-Item -WhatIf

If the output looks OK, remove the -WhatIf 如果输出看起来正常,请删除-WhatIf

In this sample tree 在这个样本树中

> tree /F
A:.
└───test
    │   alpha.py
    │   bravo.py
    │
    └───migrations
            alpha.py
            bravo.py
            __init__.py

the output will be 输出将是

del "A:\test\migrations\alpha.py"
del "A:\test\migrations\bravo.py"

WhatIf: Ausführen des Vorgangs "Datei entfernen" für das Ziel "A:\test\migrations\alpha.py".
WhatIf: Ausführen des Vorgangs "Datei entfernen" für das Ziel "A:\test\migrations\bravo.py".

Exceptions are not possible (as far as I know) in Windows/DOS search, but they are possible in xcopy /exclude and robocopy /X... . (据我所知)在Windows / DOS搜索中是不可能的,但是在xcopy /excluderobocopy /X...中可能是例外。 Therefore I'd advise you to copy all but the exceptions to a kind of backup folder, remove all the original ones (including the exceptions) and put everything back. 因此,我建议您将除例外之外的所有内容复制到一种备份文件夹中,删除所有原始文件(包括例外),然后将所有内容放回原处。

You can probably use for /R and if , something like this: 您可能会使用for /Rif ,如下所示:

for /r %i in (*.py) do (
  if "%~ni"=="__init__" (
    echo Skipping %i
  ) else (
    echo del "%i"
  )
)

Here I've prefixed the del command with echo so it doesn't actually delete it. 在这里,我给del命令加上了echo前缀,因此它实际上并没有将其删除。 Once it looks like it's doing what you want, then take away that echo. 一旦看起来像在做您想要的事情,然后消除回声。

If you do this in a batch file, you'll need to double up the % signs. 如果在批处理文件中执行此操作,则需要将%符号加倍。

The for /R is a recursive for, and in that format will work from the current directory. for /R是for的递归,并且该格式将在当前目录中起作用。

The %~ni says "give me the filename part only, of %i" %~ni说:“只给我%i的文件名部分”

(I'm running Linux at the moment so I can't verify exact behaviour, but you could start with this). (我目前正在运行Linux,因此无法验证确切的行为,但是您可以从此开始)。

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

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