Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forfiles with UNC path

I am trying to use forfiles to delete files that are older than 7 days. The files are in a UNC path. Below is the script that I am using.

Forfiles -p \\devexpress\C$\FULL\ -s -m *.* -d -7 -c "cmd /c del /q @path" 

But I get an error mentioning that UNC paths (\\machine\share) are not supported.

There appears to be workarounds available but cannot get a clear answer googling.

like image 224
kla Avatar asked Sep 21 '11 16:09

kla


People also ask

How do I fix UNC path is not supported?

If you open a file with such a path, the program will crash when you try to import a glazing system. You can solve this problem by mapping a normal drive letter to the path that has the UNC path.

Can you use UNC path in CMD?

CMD does not support UNC paths as current directories. The Pushd command automatically maps a drive and navigates to it.

How do I delete files from UNC path?

Only if the pushd suceeds the removal is executed. 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.

What is a UNC path?

Answer: A UNC path, or Universal Naming Convention, is simply a shared folder on a computer. The purpose for this folder is so when you upgrade, the registers and back office computers know where the upgrade file is and can connect to it. An example of an UNC path is \\ComputerName\SharedFolder.


2 Answers

Enhanced solution to the PA's first answer is:

PushD "\\devexpress\C$\FULL\" &&(
    forfiles -s -m *.* -d -7 -c "cmd /c del /q @path" 
     ) & PopD

The PushD command maps the UNC path to free drive letter automatically, so this is portable approach. Found in http://www.petri.co.il/forums/showthread.php?t=24241.

like image 199
jirkamat Avatar answered Sep 19 '22 15:09

jirkamat


The error I get when trying to reproduce the problem says that the problem is not with FORFILES not suporting UNC Path, but with CMD not being able to start with an UNC path as default directory. In case that this is also your problem, there are three approaches to solve it.

  1. you might assign the UNC path to a disk letter, via NET USE

    NET USE V: \\devexpress\C$
    Forfiles -p V:\FULL\ -s -m *.* -d -7 -c "cmd /c del /q @path" 
    
  2. You may bypass CMD and directly use some ERASEFILE executable utility directly in the -C option of the FORFILES

  3. You may bypass FORFILES and use FOR command with some date checking logic instead. See my answer to this Stack overflow question How can I check the time stamp creation of a file in a Windows batch script?

like image 34
PA. Avatar answered Sep 21 '22 15:09

PA.