0

I would like to delete everything inside a folder on a network drive, but not the folder itself. It's school drive, therefore I can't delete the folder and replace it. If I try to delete the folder, it will look like i deleted it, until I restart my pc. I need to have access to the folder afterwards, and a restart in between is just too inconvenient.

Things I've tried:

cd \\LOCATION
rd . /q /s

However this error occurs

CMD does not support UNC paths as current directories.

I hope I explained this well enough, if there was something you didn't understand then just write, then I'll try to clarify.

gsa
  • 790
  • 1
  • 8
  • 20
error 40
  • 17
  • 8
  • You have to use pushd and popd, [see](https://superuser.com/questions/282963/browse-an-unc-path-using-windows-cmd-without-mapping-it-to-a-network-drive) – kelalaka Sep 12 '18 at 06:17
  • You have to use pushd and popd [see](https://stackoverflow.com/questions/40996943/use-of-pushd-and-popd-command-with-unc-path) – kelalaka Sep 12 '18 at 06:18
  • The process can not access the file as it is used by another process. – error 40 Sep 12 '18 at 11:38

2 Answers2

1

Create a batch file and loop through every folder inside the folder you want to keep. Consider you have \\LOCATION\Folder\folder2 then folder3 etc. then this will remove all folders from within \\LOCATION\Folder but keep Folder:

set "myunc=\\LOCATION\Folder" 
pushd "%myunc%" && (
    for /d %%i in (*) do rmdir "%%i" /q /s 
    popd
)

Set sets a variable name of myfunc with a value which in this case is your path.

For /d is a loop that goes through each directory inside your %myunc% path and then simply do a rd on each.

Pushd allows you to pretty much cd to a network UNC path. pushd will create a temp drive letter for the UNC path. So it is almost like you doing net use X: \\servername\path

popd will then just remove the temp drive letter for you.

Open cmd.exe and type pushd /? and popd /?

To learn more on batch commands, from cmd.exe do help which will list all cmd commands, for each command you can run the /? switch to learn more about it.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Perfect. This worked fantastically. Could I get you to explain what the different things do? Because I dont understand much of it.... – error 40 Sep 12 '18 at 11:43
  • Added explanation to the Answer. – Gerhard Sep 12 '18 at 11:54
  • A single `rd . /s /q 2>nul` after the `pushd` would do the job. Besides that your FOR loop will not remove files in the root directory. – sst Sep 12 '18 at 12:48
  • @sst the reason I use `rmdir "%%i"` is so the user could understands how this loop works.. I could have also elimiated the loop entirely, but I didn't. Also, OP specifies removing directory, not files, if he specified files I would have added it. – Gerhard Sep 12 '18 at 12:59
  • Fair enough. It seems that we have disagreement on interpreting things. That's what "delete everything inside..." means to me: to delete everything inside. – sst Sep 12 '18 at 13:34
  • @sst yes, I get what you are saying, at a glance it seems like everything, but then he says deleting the folder, so seems he simply has a folder in that folder that he wants to delete, especially considering the fact that he used the `rd` command in his example. By the way, not disagreement in my view, just a discussion :) – Gerhard Sep 12 '18 at 13:40
  • Oh well, it was me not expressing myself correctly. I do/did need to remove all the folders as well. Gerhard Barnard's solution works like a charm. I havn't tried and properly will not try, the option you came with, sorry sst. But you know, no need to fix something that works. – error 40 Sep 12 '18 at 13:46
  • @error40, You are free to try anything you see fit, nothing is personal here. It doesn't matter for any of us which method the reader chooses at the end, It's not a competition. Of course for your own good you should fully understand the implications of each method before using them. `rd . /s /q 2>nul` will remove everything inside the folder including all files and folders while keeping the root folder, Gerhard's method will remove all directories inside, but not the files inside the root folder. So that's up to you to decide which one fits your needs. That's all about it. – sst Sep 12 '18 at 14:39
  • @sst I dont know if im understanding you right. Gerhards script removes everything inside \\network_location\folder_I_want_to_keep\folder_and_files_I_dont_want_to_delete after the scrip i have \\network_location\folder_I_want_to_keep\ I dont know if I misunderstood you – error 40 Sep 12 '18 at 16:02
-1

Open the Start Menu and in the text box, type cmd.exe and hit Enter (or open the command prompt using your preferred method)
Switch to the network drive by typing Z: (where Z is the letter of the network drive)
Change to the parent directory of the directory you're trying to delete using cd path\to\parent\directory Delete the directory using rmdir /S giantdir
For example, if you want to delete the directory O:\MG\WTF\BBQ\SOMANYFILES:

C:\Documents And Settings\Me> O:

O:> cd MG\WTF\BBQ

O:\MG\WTF\BBQ> rmdir /S SOMANYFILES
Or now that I think about it, I think you could just do

C:\Documents And Settings\Me> O:

O:> rmdir /S MG\WTF\BBQ\SOMANYFILES
but you would miss out on the chance to see Windows spell out OMGWTFBBQ in terminal font ;-)

By the way, rmdir (or del) does not move things to the Recycle Bin, it just deletes them,
so be careful not to delete things you don't really want to.

Anuj
  • 170
  • 11