Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command to delete files in UNC path

Tags:

batch-file

unc

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.

like image 805
Gowtham Avatar asked Feb 05 '14 11:02

Gowtham


People also ask

How do I delete files from command line?

To delete a file, use the following command: del "<filename>" . For example, to delete Test file. txt , just run del "Test File. txt" .

How do I delete a file path?

Locate the item you want to delete, highlight it by left-clicking the file or folder with your mouse once, and press the Delete key. You can browse the location of the file or folder using My Computer or Windows Explorer.

Does CMD support UNC paths?

CMD does not support UNC paths as current directories. The Pushd command automatically maps a drive and navigates to it. If you run the "net use" command after you run Pushd, you'll see a new drive mapping.


1 Answers

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.

like image 142
MC ND Avatar answered Sep 22 '22 12:09

MC ND