2

I've been search for a shell (cmd) solution for this for a while and haven't found any. So I wanted to create a Junction but first I need remove the folder if it exists (Junction from sysinterals won't let me ovewrite this).

My first solution was to to run rmdir /S/Q folder\to\be\overwritten. But if I re-run the script and folder\to\be\overwritten is already a link pointing to e.g. common\folder than contents of common\folder will be whipped out. Not good ;-).

So how to safely remove folder but only that folder in Windows command line? Also note that I need this to work in Windows XP.

Nux
  • 9,276
  • 5
  • 59
  • 72

1 Answers1

0

The answer I found well... works.

rem ad-l means directories but not links (symlinks or hardlinks)
for /f %%d in ('dir /ad-l /b .\folder\to\be') do @if [%%d]==[overwritten] rmdir /S /Q .\folder\to\be\overwritten
rem adl means directories which are links (symlinks or hardlinks)
for /f %%d in ('dir /adl /b .\folder\to\be') do @if [%%d]==[overwritten] rmdir .\folder\to\be\overwritten

Note that in this example folder\to\be contains folder (or link) named overwritten which is to be removed.

Nux
  • 9,276
  • 5
  • 59
  • 72