从任何地方删除未知文件夹下的特定子文件夹
Delete specific subfolder under unknown folders from anywhere
我有一个名为 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)
问题是我只想从任何地方双击-运行 batch/powershell 文件,可能是从另一个分区或驱动器(甚至是网络计算机)。
我试过了RMDIR G:\Project\Projects\*\SSS
它似乎没有用。
请尽可能解释您的答案,同时向我展示回收站删除和 parma 删除的命令。虽然我认为没有 powershell 是不可能发送到回收站的。
删除非系统分区的文件夹不需要管理员权限吧?
Windows 10.powershell 版本 5
类似于
for /f %A in ('dir "c:\somewhere\deleteme.doc"') Do echo rd "%~dpA"
Administrator privilege is not necessary to delete folders in non-system partition right?
我们如何知道您拥有哪些权限。键入 icacls c:\somewhere
以查看。
你需要知道你的问题。
@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 命令仅 ECHO
ed 用于测试目的。 确认命令正确后,将ECHO(RD
更改为RD
以实际删除目录。
所需的 DEL 命令仅 ECHO
ed 用于测试目的。 确认命令正确后,将ECHO(DEL
更改为DEL
以实际删除文件。
第一部分分析提供给程序的参数和开关。
已显示默认值,需要根据您的情况进行调整。
命令可能运行为
*thisname* from=c:\new\location dir=xxx file=hello.txt /d
where from 指定树遍历的起点,fie 要定位的文件名和 dir 要删除的目录名。
还列出了开关。
该程序只是检查命令行中感兴趣的元素并根据需要设置值。
然后我们遍历树,查找目录名,然后根据开关设置做出决定。
然后再次遍历树,寻找文件名。
我使用 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"
我有一个名为 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)
问题是我只想从任何地方双击-运行 batch/powershell 文件,可能是从另一个分区或驱动器(甚至是网络计算机)。
我试过了RMDIR G:\Project\Projects\*\SSS
它似乎没有用。
请尽可能解释您的答案,同时向我展示回收站删除和 parma 删除的命令。虽然我认为没有 powershell 是不可能发送到回收站的。
删除非系统分区的文件夹不需要管理员权限吧?
Windows 10.powershell 版本 5
类似于
for /f %A in ('dir "c:\somewhere\deleteme.doc"') Do echo rd "%~dpA"
Administrator privilege is not necessary to delete folders in non-system partition right?
我们如何知道您拥有哪些权限。键入 icacls c:\somewhere
以查看。
你需要知道你的问题。
@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 命令仅 ECHO
ed 用于测试目的。 确认命令正确后,将ECHO(RD
更改为RD
以实际删除目录。
所需的 DEL 命令仅 ECHO
ed 用于测试目的。 确认命令正确后,将ECHO(DEL
更改为DEL
以实际删除文件。
第一部分分析提供给程序的参数和开关。
已显示默认值,需要根据您的情况进行调整。
命令可能运行为
*thisname* from=c:\new\location dir=xxx file=hello.txt /d
where from 指定树遍历的起点,fie 要定位的文件名和 dir 要删除的目录名。
还列出了开关。
该程序只是检查命令行中感兴趣的元素并根据需要设置值。
然后我们遍历树,查找目录名,然后根据开关设置做出决定。
然后再次遍历树,寻找文件名。
我使用 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"