使用一些变量删除带有通配符的文件夹
delete folder with wildcard using some variables
在Windows中,当我想删除几个类似的目录(带通配符)时,我用这些命令删除它们:
示例文件夹:
c:\Users\user\folder\
test1
test2
personalinfo
使用通配符删除文件夹的示例命令:
for /d %x in ("c:\Users\User\folder\test*") do rd /s /q "%x"
or
forfiles /P c:\Users\User\folder /M test* /C "cmd /c if @isdir==TRUE rmdir /s /q @file"
结果:
c:\Users\User\folder\
personalinfo
但不适用于 %HOMEPATH% 变量
for /d %x in ("%HOMEPATH%\folder\test*") do rd /s /q "%x"
or
forfiles /P %HOMEPATH%\folder /M test* /C "cmd /c if @isdir==TRUE rmdir /s /q @file"
但是使用 %APPDATA%(或 %HOMEDRIVE%\Users\User\etc 等)工作正常...非常罕见:
c:\Users\user\AppData\Roaming\folder\
test1
test2
personalinfo
for /d %x in ("%APPDATA%\folder\test*") do rd /s /q "%x"
or
forfiles /P %APPDATA%\folder /M test* /C "cmd /c if @isdir==TRUE rmdir /s /q @file"
c:\Users\user\AppData\Roaming\folder\
personalinfo
注意:在Win 7中测试。我不确定在Windows8/10
中是否会发生同样的事情
我该如何解决?提前致谢
要为您的任务找到合适的环境变量,您可以在 cmd 中发出 window(路径因长度而被排除):
> set |find /i "%USERNAME%"|find /i /V "Path"
APPDATA=C:\Users\UserName\AppData\Roaming
LOCALAPPDATA=C:\Users\UserName\AppData\Local
OneDrive=C:\Users\UserName\OneDrive
TEMP=C:\Users\UserName\AppData\Local\Temp
TMP=C:\Users\UserName\AppData\Local\Temp
USERNAME=UserName
USERPROFILE=C:\Users\UserName
但请注意,一些 special folders
可能会被重新定位到其他 drives/folders。
您需要 vb-/Jscript 或 PowerShell 来评估这些位置。
在 PowerShell 提示符下:
PS> [environment]::getfolderpath("mydocuments")
C:\Users\LotPings\Documents
枚举特殊文件夹名称:
PS> [Environment+SpecialFolder]::GetNames([Environment+SpecialFolder])
并将所有特殊文件夹解析为其当前值:
[Environment+SpecialFolder]::GetNames(
[Environment+SpecialFolder])| Sort-Object | ForEach-Object{
"{0,22} {1}" -f $_,[Environment]::GetFolderPath($_)}
AdminTools C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools
ApplicationData C:\Users\LotPings\AppData\Roaming
CDBurning C:\Users\LotPings\AppData\Local\Microsoft\Windows\Burn\Burn
CommonAdminTools C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools
CommonApplicationData C:\ProgramData
CommonDesktopDirectory C:\Users\Public\Desktop
CommonDocuments C:\Users\Public\Documents
CommonMusic C:\Users\Public\Music
CommonOemLinks
CommonPictures C:\Users\Public\Pictures
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFilesX86 C:\Program Files (x86)\Common Files
CommonPrograms C:\ProgramData\Microsoft\Windows\Start Menu\Programs
CommonStartMenu C:\ProgramData\Microsoft\Windows\Start Menu
CommonStartup C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
CommonTemplates C:\ProgramData\Microsoft\Windows\Templates
CommonVideos C:\Users\Public\Videos
Cookies C:\Users\LotPings\AppData\Local\Microsoft\Windows\INetCookies
Desktop C:\Users\LotPings\Desktop
DesktopDirectory C:\Users\LotPings\Desktop
Favorites C:\Users\LotPings\Favorites
Fonts C:\WINDOWS\Fonts
History C:\Users\LotPings\AppData\Local\Microsoft\Windows\History
InternetCache C:\Users\LotPings\AppData\Local\Microsoft\Windows\INetCache
LocalApplicationData C:\Users\LotPings\AppData\Local
LocalizedResources
MyComputer
MyDocuments C:\Users\LotPings\Documents
MyMusic C:\Users\LotPings\Music
MyPictures C:\Users\LotPings\Pictures
MyVideos C:\Users\LotPings\Videos
NetworkShortcuts C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Network Shortcuts
Personal C:\Users\LotPings\Documents
PrinterShortcuts C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
ProgramFiles C:\Program Files
ProgramFilesX86 C:\Program Files (x86)
Programs C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
Recent C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Recent
Resources C:\WINDOWS\resources
SendTo C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\SendTo
StartMenu C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Start Menu
Startup C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
System C:\WINDOWS\system32
SystemX86 C:\WINDOWS\SysWOW64
Templates C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Templates
UserProfile C:\Users\LotPings
Windows C:\WINDOWS
在Windows中,当我想删除几个类似的目录(带通配符)时,我用这些命令删除它们:
示例文件夹:
c:\Users\user\folder\
test1
test2
personalinfo
使用通配符删除文件夹的示例命令:
for /d %x in ("c:\Users\User\folder\test*") do rd /s /q "%x"
or
forfiles /P c:\Users\User\folder /M test* /C "cmd /c if @isdir==TRUE rmdir /s /q @file"
结果:
c:\Users\User\folder\
personalinfo
但不适用于 %HOMEPATH% 变量
for /d %x in ("%HOMEPATH%\folder\test*") do rd /s /q "%x"
or
forfiles /P %HOMEPATH%\folder /M test* /C "cmd /c if @isdir==TRUE rmdir /s /q @file"
但是使用 %APPDATA%(或 %HOMEDRIVE%\Users\User\etc 等)工作正常...非常罕见:
c:\Users\user\AppData\Roaming\folder\
test1
test2
personalinfo
for /d %x in ("%APPDATA%\folder\test*") do rd /s /q "%x"
or
forfiles /P %APPDATA%\folder /M test* /C "cmd /c if @isdir==TRUE rmdir /s /q @file"
c:\Users\user\AppData\Roaming\folder\
personalinfo
注意:在Win 7中测试。我不确定在Windows8/10
中是否会发生同样的事情我该如何解决?提前致谢
要为您的任务找到合适的环境变量,您可以在 cmd 中发出 window(路径因长度而被排除):
> set |find /i "%USERNAME%"|find /i /V "Path"
APPDATA=C:\Users\UserName\AppData\Roaming
LOCALAPPDATA=C:\Users\UserName\AppData\Local
OneDrive=C:\Users\UserName\OneDrive
TEMP=C:\Users\UserName\AppData\Local\Temp
TMP=C:\Users\UserName\AppData\Local\Temp
USERNAME=UserName
USERPROFILE=C:\Users\UserName
但请注意,一些 special folders
可能会被重新定位到其他 drives/folders。
您需要 vb-/Jscript 或 PowerShell 来评估这些位置。
在 PowerShell 提示符下:
PS> [environment]::getfolderpath("mydocuments")
C:\Users\LotPings\Documents
枚举特殊文件夹名称:
PS> [Environment+SpecialFolder]::GetNames([Environment+SpecialFolder])
并将所有特殊文件夹解析为其当前值:
[Environment+SpecialFolder]::GetNames(
[Environment+SpecialFolder])| Sort-Object | ForEach-Object{
"{0,22} {1}" -f $_,[Environment]::GetFolderPath($_)}
AdminTools C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools
ApplicationData C:\Users\LotPings\AppData\Roaming
CDBurning C:\Users\LotPings\AppData\Local\Microsoft\Windows\Burn\Burn
CommonAdminTools C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools
CommonApplicationData C:\ProgramData
CommonDesktopDirectory C:\Users\Public\Desktop
CommonDocuments C:\Users\Public\Documents
CommonMusic C:\Users\Public\Music
CommonOemLinks
CommonPictures C:\Users\Public\Pictures
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFilesX86 C:\Program Files (x86)\Common Files
CommonPrograms C:\ProgramData\Microsoft\Windows\Start Menu\Programs
CommonStartMenu C:\ProgramData\Microsoft\Windows\Start Menu
CommonStartup C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
CommonTemplates C:\ProgramData\Microsoft\Windows\Templates
CommonVideos C:\Users\Public\Videos
Cookies C:\Users\LotPings\AppData\Local\Microsoft\Windows\INetCookies
Desktop C:\Users\LotPings\Desktop
DesktopDirectory C:\Users\LotPings\Desktop
Favorites C:\Users\LotPings\Favorites
Fonts C:\WINDOWS\Fonts
History C:\Users\LotPings\AppData\Local\Microsoft\Windows\History
InternetCache C:\Users\LotPings\AppData\Local\Microsoft\Windows\INetCache
LocalApplicationData C:\Users\LotPings\AppData\Local
LocalizedResources
MyComputer
MyDocuments C:\Users\LotPings\Documents
MyMusic C:\Users\LotPings\Music
MyPictures C:\Users\LotPings\Pictures
MyVideos C:\Users\LotPings\Videos
NetworkShortcuts C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Network Shortcuts
Personal C:\Users\LotPings\Documents
PrinterShortcuts C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
ProgramFiles C:\Program Files
ProgramFilesX86 C:\Program Files (x86)
Programs C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
Recent C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Recent
Resources C:\WINDOWS\resources
SendTo C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\SendTo
StartMenu C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Start Menu
Startup C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
System C:\WINDOWS\system32
SystemX86 C:\WINDOWS\SysWOW64
Templates C:\Users\LotPings\AppData\Roaming\Microsoft\Windows\Templates
UserProfile C:\Users\LotPings
Windows C:\WINDOWS