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% )"
pauseI 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
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
pauseClarification
Instead of using
forfiles /P \\sdpw9123app\work\ActiveMQ\logfile
- Use
PUSHD \\sdpw9123app\workon the line before theforfilescommand- Run the
forfilescommand line asforfiles /P \ActiveMQ\logfile- Use
POPDon the line after theforfilescommand
Further Resources
UNC Network paths
When a UNC path is specified,
PUSHDwill create a temporary drive map and will then use that new drive. The temporary drive letters are allocated in reverse alphabetical order, so ifZ:is free it will be used first.POPDwill also remove any temporary drive maps created byPUSHD
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.