0

I am trying to find a way of deleting selected files/folders on Windows 7 machines attached to my local network.

The problem I have is that the location of ''my Documents'' is different on ALL machines as in Windows 7 the path = C:\Users**username**\Documents.

I would like to place a batch file in a shared directory so it can be run locally on machines at the required time.

Any help or advice is most appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Steve.C
  • 3
  • 1
  • 2
  • Where are the files you want to delete? Why is it a Problem that the "my documents" Folders are different? – Werner Henze May 07 '13 at 08:15
  • Hi Werner, The folders are located within ''My Documents'' And it is a problem because I want to have a single batch file that can be used by each machine, run manually, locally. Please tell me if I am being naive and missing something obvious here?.?.? Is there an easier option other than using a batch file? Or is there script that conquers this problem? – Steve.C May 07 '13 at 08:28
  • So "my documents" resides on a Network drive? It would be nice if you wrote your question more clearly. – Werner Henze May 07 '13 at 08:41
  • No ''My Documents'' is a local library on the Windows 7 machine. – Steve.C May 07 '13 at 09:38

2 Answers2

0

If you want to write a bat file that works for different users and deletes files from the user's "my documents" folder, then you can write a bat file with a command like that:

del %USERPROFILE%\Documents\file_to_delete
rd /qs %USERPROFILE%\Documents\folder_to_delete

Windows sets the variable USERPROFILE to the right Location for each user. You can check the value of USERPROFILE and other environment variables in cmd.exe with

set
Werner Henze
  • 16,404
  • 12
  • 44
  • 69
0

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.

Community
  • 1
  • 1
user2033427
  • 853
  • 7
  • 8
  • Thanks guys So, If the user had a folder called ''johnsmith'' in the ''Documents'' library and I wanted to delete the entire contents of that folder, what should my batch file look like? – Steve.C May 07 '13 at 09:09
  • Which method do you want to use? – user2033427 May 07 '13 at 09:13
  • i think using the %USERPROFILE% command would be easiest.. – Steve.C May 07 '13 at 09:20
  • You should only use it if all the computers have the `My Documents` directory named as the same name.. Are they? – user2033427 May 07 '13 at 09:33
  • Yes. They all have the deafult ''My Documents'' Folder – Steve.C May 07 '13 at 09:38
  • I have added the code above. Sorry for the slow response, my internet connection is intermittently cutting off. – user2033427 May 07 '13 at 10:01
  • I wrote this code : @echo off if exist "%USERPROFILE%\Documents" ( del "%USERPROFILE%\Documents\"/F /Q ) else ( del "%USERPROFILE%\My Documents\"/F /Q ) And it works to delete all files. What do I add and where to delete folders? Sorry for my ignorance... Bit of a Newbie – Steve.C May 07 '13 at 10:14