3

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.

2 Answers 2

5

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

2
  • It's working fine now after adding PUSHD to map drive. But how to paste files by source folder structure. If files copying from \ActiveMQ\logfile then how to get it like c:\temp\new\ActiveMQ\logfile, with folder structure. Please help Commented Feb 14, 2019 at 8:18
  • please help me to copy files with source folder structure.. I'm getting no clue Commented Feb 15, 2019 at 9:36
-1

In batch files you need to use % twice when referencing variables, ie %%DATE_TO%%, whereas you only need to do it once on the command line. Try fixing that, and see if the above works when you paste it into cmd.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .