代码之家  ›  专栏  ›  技术社区  ›  acgbox

使用某些变量删除带有通配符的文件夹

  •  0
  • acgbox  · 技术社区  · 6 年前

    在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中是否发生了同样的事情

    我怎么解决这个问题?提前谢谢

    1 回复  |  直到 6 年前
        1
  •  1
  •   LotPings    6 年前

    要为您的任务找到合适的环境变量,您可以在命令窗口中发出(路径长度不包括在内):

    > 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 可能会重新定位到其他驱动器/文件夹。 您需要一个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