Announcement

Collapse
No announcement yet.

Deleting remote files

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Deleting remote files

    Hi guys, i'm trying to run a script on the server to check a network storage device for files older than 14 days. i can get this running by using the robocopy command and moving the files into a specific directory and then deleting them, but due to their size this is too combersome. i have a forfile command that works perfectly on local, but is not suitable as it does not work over the network. the script needs to be able to be ran as a system task.


    forfiles.exe /p //DEVICE /s /d -14 /c "cmd /c del @file"

  • #2
    Re: Deleting remote files

    You cannot use the /P switch for unc paths, because forfiles requires to beable to open the path as the working directory. A script, any script, can only read, write or execute from UNC paths, but an UNC-path can never be used as default active drive/folder (working directory).
    What you can do is to create a temporary drive mapping to the UNC-path.

    If you're not using the /P switch then ForFiles use the present working directory (".") to start from.
    Use PushD and PopD in a batch to switch working directory to go to and return from a defined path. If the defined path happen to be an UNC path then PushD create a temp drive mapping and PopD deletes it again.

    note:
    btw The path that you defined in your batch (//DEVICE) is NOT a directory!


    sample
    Code:
    @echo off
    
    :: define the "\name" of a shared folder on the remote "\\device"
    PushD "\\DEVICE\name" &&(
      "forfiles.exe" /s /m "*.*" /d -14 /c "cmd /c del @file"
     ) & PopD
    The &&(combined commands) is used here for safety!!!
    explained: http://forums.petri.com/showpost.php...15&postcount=4

    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Deleting remote files

      FYI

      If you want to capture the full path of the new working directoy made by PushD, you can use the %CD% pseudo-variable after PushD.


      Here is an other example of how you can use %CD%
      Code:
      :: note:
      :: This sample is *just* for the purpose
      :: of introducing the %CD% variable.
      :: What this complete batch actualy is
      :: trying to accomplish is rather useless
      :: because PushD and PopD do this automatically and better.
      
      
      @echo off
      
      set "workingdirectory=path\folder"  you can enter a local- or unc-path 
      
      :: Alternative for PushD
      Set "WrkDir1=%CD%"
      Set "wrkDir2=%workingdirectory%"
      (CD/D "%wrkDir2%" ||(
        For /F "tokens=2 Delims= " %%* in (
         'net use * "%wrkDir2%" ^|Find ":"') Do (
      Set "_MapDrv=%%*" & CD/D %%*)) )>Nul,2>&1
      
      Set "WrkDir2=%CD%"
      
      :: Start my script from here
          echo.
          echo.checking the new working directory:
           echo.%CD%
          echo.
      :: End of my script
      
      :: Alternative for PoPD
      CD/D "%WrkDir1%"
      If defined _MapDrv (Net use %_MapDrv% /D /yes)>Nul,2>&1
      Set "WrkDir2="
      
      echo.
      echo.checking the default working directory:
       echo.%CD%
      echo.
      
      pause >Nul
      \Rems
      Last edited by Rems; 25th May 2008, 18:10. Reason: little typo

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Deleting remote files

        Thanks for your help, slight typo with the device name, I was typing it correctly in my batch, your file works correctly for me, so thanks very much.

        i'm going to try to make my own in powershell now

        Comment

        Working...
        X