【问题标题】:Delete specific subfolder under unknown folders from anywhere从任何地方删除未知文件夹下的特定子文件夹
【发布时间】:2016-03-23 12:51:40
【问题描述】:

我有一个名为G:\Project\Projects的文件夹

这个文件夹有一些(大约 20 个)未知的子文件夹。一些子文件夹有一个名为SSS 的文件夹和一个名为deleteme.doc 的文件

我想使用批处理文件或 powershell 文件删除所有这些 SSS 文件夹(包含内容)和 deleteme.doc 文件。我不想删除这些深深嵌套在子文件夹中的文件或文件夹,只删除那些直接位于子文件夹下的文件或文件夹。

比如我要删除这个文件夹

G:\Project\Projects\proj 1\sss

但不是这些 3-

G:\Project\Projects\proj 1\other\sss

G:\Project\Projects\sss

G:\Project\Projects\proj 1\sss (it is a file)

问题是我想简单地从任何地方双击运行批处理/powershell文件,可能来自另一个分区或驱动器(甚至网络计算机)。

我试过RMDIR G:\Project\Projects\*\SSS 似乎没有用。

如果可能的话,请解释你的答案,同时告诉我回收站删除和帕尔马删除的命令。虽然我认为没有 powershell 就无法发送到回收站。

删除非系统分区中的文件夹不需要管理员权限吧?

Windows 10.powershell 版本 5

【问题讨论】:

  • 你有多少个名为 SSS 的文件夹?

标签: windows powershell batch-file windows-10 delete-file


【解决方案1】:
@ECHO Off
SETLOCAL
:: remove variables starting $
FOR  /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="

:: set defaults
SET "$dirname=sss"
SET "$filename=deleteme.doc"
SET "$from=c:\wherever\you\like"

:: analyse command line
:: syntax file="filename" " dir=dirname" from="dirname" (/switches)
:: each is optional. default is as established above
:: /d  - delete directory only if it is completely empty
:: /dn - delete directory only if it is not completely empty
:: /ds - delete directory only if it is completely empty or if no files in its subdirectory tree
:: /fe - delete file if empty
:: /fn - delete file if not empty
:: /b  - both file and directory must exist to make deletions
::
FOR %%a IN (%*) DO IF DEFINED $twopart (CALL :parttwo %%a) ELSE (
 SET "$processed="
 IF /i "%%a"=="dir"     SET "$processed=Y"&SET "$twopart=$dirname"
 IF /i "%%a"=="file"    SET "$processed=Y"&SET "$twopart=$filename"
 IF /i "%%a"=="from"    SET "$processed=Y"&SET "$twopart=$from"
 IF /i "%%a"=="/b"      CALL :setswitch b
 IF /i "%%a"=="/d"      CALL :setswitch d
 IF /i "%%a"=="/dn"     CALL :setswitch dn
 IF /i "%%a"=="/ds"     CALL :setswitch ds
 IF /i "%%a"=="/fe"     CALL :setswitch fe
 IF /i "%%a"=="/fn"     CALL :setswitch fn
 IF NOT DEFINED $processed CALL :extras %%a
)

IF DEFINED $extras ECHO(%$extras% NOT understood)&pause&GOTO :EOF 

:: resolve switch compatibility

IF DEFINED $d  IF DEFINED $dn SET "$incompatible=/d /dn"
IF DEFINED $fe IF DEFINED $fn SET "$incompatible=%$incompatible% /f /fn"

IF DEFINED $incompatible ECHO(%$incompatible% incompatible)&pause&GOTO :EOF 

:: if $from is set, make it quoted.
IF DEFINED $from SET "$from="%$from:"=%""

:: Now search for the directory
FOR /d /r %$from% %%a IN ("%$dirname%") DO IF EXIST "%%a\." (
 SET "$victim=%%a"
 CALL :deldir
)
IF NOT DEFINED $b FOR /r %$from% %%a IN ("%$filename%") DO IF EXIST "%%a" (
 SET "$victim=%%~dpa%$filename%"
 CALL :delfile
)

GOTO :eof

:: delete the victim directory if all other conditions are met
:: take care of file as well if required.
:deldir
SET "$victim=%$victim:"=%"
IF DEFINED $b IF NOT EXIST "%$victim%\..\%$filename%" GOTO :eof
:: if the directory has any contents and "/d" was specified, skip deletion
IF DEFINED $d (
 FOR /f %%z IN ('dir /b "%$victim%" 2^>nul') DO GOTO :eof
)
:: if the directory has files in its subtree and "/ds" was specified, skip deletion
IF DEFINED $ds (
 FOR /f %%z IN ('dir /s /b /a-d "%$victim%"  2^>nul') DO GOTO :eof
)
:: if the directory has no files and none in its subtree and "/dn" was specified, don't skip deletion
IF DEFINED $dn (
 FOR /f %%z IN ('dir /b "%$victim%"  2^>nul') DO GOTO deldir1
 GOTO :eof
)
:: delete the directory
:deldir1

ECHO(DEL /s /f /q "%$victim%"
SET "$victim=%$victim%\..\%$filename%"
IF EXIST "%$victim%" GOTO delfile 
GOTO :eof

:delfile
FOR %%z IN ("%$victim%") DO (
 IF DEFINED $fn IF "%%~zz"=="0" GOTO :EOF 
 IF DEFINED $fe IF "%%~zz" neq "0" GOTO :EOF 
)
ECHO(DEL /f /q "%$victim%"
GOTO :eof



:parttwo
SET "%$twopart%=%~1"
SET "$twopart="
GOTO :eof

:extras
SET "$extras=%$extras% %~1"
GOTO :eof

:setswitch
SET "$processed=Y"
SET "$%1=Y"
SHIFT
IF "%1" neq "" GOTO setswitch
GOTO :EOF

这是一个通用解决方案,您需要在释放之前对其进行测试。

所需的 RD 命令仅用于测试目的ECHOed。 验证命令正确后,将ECHO(RD 更改为RD 以实际删除目录。

所需的 DEL 命令仅用于测试目的ECHOed。 验证命令正确后,将ECHO(DEL更改为DEL以实际删除文件。

第一部分分析提供给程序的参数和开关。

显示默认值,需要根据您的情况进行调整。

命令可以运行为

*thisname* from=c:\new\location dir=xxx file=hello.txt /d

where from 指定 tree-walk 开始的位置,指定要定位的文件名和要删除的目录名。

开关也列出来了。

程序只需检查命令行中感兴趣的元素并根据需要设置值。

然后我们遍历树,寻找目录名,然后根据开关设置做出决定。

然后再次遍历树,寻找文件名。

【讨论】:

  • 哇。您确实付出了一些努力来制作一个灵活且易于使用的实用程序。这是一种权衡,可以让您轻松理解如何满足各种标准 - 基本机制在某种程度上被您打包到一个脚本中的光滑选项处理所掩盖。
【解决方案2】:

有点像

for /f %A in ('dir "c:\somewhere\deleteme.doc"') Do echo rd "%~dpA"

删除非系统分区中的文件夹不需要管理员权限吧?

我们如何知道您拥有哪些权限。输入icacls c:\somewhere 即可查看。

你需要知道你的问题。

【讨论】:

    【解决方案3】:

    我使用 this-

    创建了一个简短的 powershell 解决方案
    $targetName='ss s';
    foreach ($file in Get-Childitem "G:\Project\Projects\" )
    {
       $fname=$file.name;
       if (Test-Path "G:\Project\Projects\$fname\$targetName\")
            { 
                $shell = new-object -comobject "Shell.Application"
                $item = $shell.Namespace(0).ParseName("G:\Project\Projects\$fname\$targetName")
                $item.InvokeVerb("delete")
    
            }
    }
    

    此 powershell 脚本会在弹出确认消息后将该 文件夹 发送到回收站。 (这不会发送任何名为 'ss s' 的文件)

    这似乎适用于批处理文件脚本-

    set "b=ss s"
    for /d %%a in (*) do  IF EXIST "%%a\%b%\*" (rmdir /s "%%a\%b%")
    

    如果目标文件夹被命名为“$ss s”,那么你必须将变量设置为"b=/$ss s"

    【讨论】:

    • 我稍后会回复你的答案,伙计们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 2015-10-15
    • 2012-12-15
    • 2012-01-02
    • 2012-11-19
    相关资源
    最近更新 更多