6

Hi I tried below command to delete files in UNC path

set folder="\\SERVERNAME\Publish" 
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)

But I got error saying:

UNC paths are not supported. Defaulting to Windows Directory

Somehow I need to delete files that are residing in Server's shared path using batch command. Any help appreciated.

benka
  • 4,732
  • 35
  • 47
  • 58
Gowtham
  • 252
  • 1
  • 6
  • 22

2 Answers2

9

edited 2015-09-16 - Original answer remains at the bottom

Code reformated to avoid removal of non desired folders if the mapping fails. Only if the pushd suceeds the removal is executed.

set "folder=\\SERVERNAME\Publish" 
pushd "%folder%" && (
    for /d %%i in (*) do rmdir "%%i" /s /q 
    popd
)

original answer:

set "folder=\\SERVERNAME\Publish" 
pushd "%folder%"
for /d %%i in (*) do rmdir "%%i" /s /q 
popd

pushd will create a drive mapping over the unc path and then change to it. Then, all the operations are over drive:\folders. At the end popd will remove the drive assignation.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • I had to replace single ' with double " – aggaton Sep 16 '15 at 17:13
  • @aggaton, I don't know how I didn't remove the quotes (copy/paste from original code, I think). Thank you. Anyway, the quotes are not even needed. – MC ND Sep 16 '15 at 17:22
1

This deletes all files with name like 'ms' and over a year.

@echo off
set "year=-365"
PushD "\\SERVERNAME\FolderName" && (
  "forfiles.exe" /s /m "*_ms_*" /d %year% /c "cmd /c del @file"
) & PopD
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
LIDEN
  • 156
  • 4