Batch file "unc path not supported" copying files

I'm trying to copy files from a network location to my local computer using below script, but it gives me an error message of

unc path not supported


The Script

SET DESTINATION=c:\temp\new
SET DATE_FROM=02/13/2019
SET DATE_TO=02/13/2019

> nul forfiles /P \\sdpw9123app\work\ActiveMQ\logfile /S /D +%DATE_FROM% /C "cmd /C if @isdir==FALSE 2> nul forfiles /M  @file /D -%DATE_TO% && > con ( echo @path && copy /V @path %DESTINATION% )"

pause

I also tried using some Robocopy commands but I couldn't get it to work either but ideally I'd like to use the forfiles command to perform the copy operation.


The issue seems to be with using the forfiles command and it not supporting UNC paths. You can use pushd to map the UNC path for you, then just use the rest of the path after the \\servername\sharename that maps which contains folders you need to run the commands against. End the script with the popd command to disconnect any temporary mapped drives created with the pushd command.

Script

SET DESTINATION=c:\temp\new
SET DATE_FROM=02/13/2019
SET DATE_TO=02/13/2019

PUSHD \\sdpw9123app\work
> nul forfiles /P \ActiveMQ\logfile /S /D +%DATE_FROM% /C "cmd /C if @isdir==FALSE 2> nul forfiles /M  @file /D -%DATE_TO% && > con ( echo @path && copy /V @path %DESTINATION% )"
POPD

pause

Clarification

  • Instead of using forfiles /P \\sdpw9123app\work\ActiveMQ\logfile

    • Use PUSHD \\sdpw9123app\work on the line before the forfiles command
    • Run the forfiles command line as forfiles /P \ActiveMQ\logfile
    • Use POPD on the line after the forfiles command

Further Resources

  • PUSHD

    UNC Network paths

    When a UNC path is specified, PUSHD will create a temporary drive map and will then use that new drive. The temporary drive letters are allocated in reverse alphabetical order, so if Z: is free it will be used first.

  • POPD

    POPD will also remove any temporary drive maps created by PUSHD