acgbox
acgbox

Reputation: 324

delete folder with wildcard using some variables

in Windows, when I want to delete several similar directories (with wildcard), I delete them with these commands:

Example folder:

c:\Users\user\folder\
   test1
   test2
   personalinfo

Example commands to delete folders with wildcard:

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"

Result:

c:\Users\User\folder\
   personalinfo

But doesn't work with %HOMEPATH% variable

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"

But with %APPDATA% (or %HOMEDRIVE%\Users\User\etc etc) works fine... very rare:

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

Note: Test in Win 7. I'm not sure if the same thing happens in Windows 8/10

how can I solve that? Thanks in advance

Upvotes: 0

Views: 1610

Answers (1)

user6811411
user6811411

Reputation:

To find the appropriate environment variable for your task you can issue in a cmd window (Path excluded for it's length):

> 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

But take care, some special folders might be relocated to other drives/folders. You'll need a vb-/Jscript or PowerShell to evaluate these locations.

At a PowerShell prompt:

PS> [environment]::getfolderpath("mydocuments")
C:\Users\LotPings\Documents

To enumerate the special folders names:

PS> [Environment+SpecialFolder]::GetNames([Environment+SpecialFolder])

And to resolve all special folders to their current values:

[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

Upvotes: 1

Related Questions