This would be quite simple if the 'My Documents' folder were named the same in all Windows operating systems with environment variables: %USERPROFILE%
always points to the user's folder, and "%USERPROFILE%\My Documents"
would do it. Unfortunately in Windows XP, it is named My Documents
and in Windows Vista and 7, it is named Documents
. If all the computers you mentioned name My Documents as the same name, this can be used.
There is a way out without checking for the Windows operating system though, but it requires administrative access. I got this from an answer on Stack Overflow that it can be found in the registry at "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
under Personal
. reg query
will find the value and this can be extracted with a for
loop:
for /f "skip=1 tokens=1,2* delims= " %%g in (`reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Personal') do set "documents=%%i"
echo %documents%
del "%documents%\etc"
EDIT: Alternatively, you could also search for the presence of a Documents
folder, though this might get thrown off by a Documents
folder that may exist in someone's Windows XP user profile directory, for example.
if exist "%USERPROFILE%\Documents" (
del "%USERPROFILE%\Documents\etc"
) else (
del "%USERPROFILE%\My Documents\etc"
)
EDIT 2: To delete all files and subfolders in My Documents (if the name is called My Documents
) but not the folder itself, use this:
del "%USERPROFILE%\My Documents\*"
for /d %%i in ("%USERPROFILE%\My Documents\*") do rd /s /q "%USERPROFILE%\My Documents\%%i"
(first one is to delete all files and second one for all subfolders)
EDIT 3: To do the finding out if Documents
exist and delete all:
if exist "%USERPROFILE%\Documents" (
rd /s /q "%USERPROFILE%\Documents"
md "%USERPROFILE%\Documents"
) else (
rd /s /q "%USERPROFILE%\My Documents"
md "%USERPROFILE%\My Documents"
)
/s
is to delete all files and subfolders, and /q
is for quiet mode where they will not prompt you whether to delete anything. Then md
to make the folder again.