# MOVING files to a UNC Path using BATCH script



## sikni8 (Jan 4, 2013)

I have the following .BAT script which works fine if both source and destination folders are local but if I make the destination a networked drive, it gives me an error.

_ @echo off

set loglocation=C:\test\Copyout.txt
set olderthan=10
set source=C:\test
set destination=C:\test2
set extension=*

if exist %loglocation% del %loglocation%

echo Copying and deleting files older than %olderthan% days with the file extension *.%extension%.
echo Copying and deleting files older than %olderthan% days with the file extension *.%extension%. >> %loglocation%
echo Copying from %source% to %destination% and deleting from %source%.
echo Copying from %source% to %destination% and deleting from %source%. >> %loglocation%
echo This file may take an extremely long time to run while it looks unresponsive.
echo Check %loglocation% for copy progress.

cd %source%

echo List of files to be copied and removed: >> %loglocation%
echo List of files to be copied and removed:

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C Echo "@FILE"
FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C Echo "@FILE" >> %loglocation%

echo Starting copy...
echo Starting copy... >> %loglocation%
date /t >> %loglocation%
time /t >> %loglocation%

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C XCOPY %source%\@FILE %destination%"

echo Starting delete...
echo Starting delete... >> %loglocation%
date /t >> %loglocation%
time /t >> %loglocation%

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C if exist %destination%\@FILE del %source%\@FILE /Q & echo @FILE Deleted." >> %loglocation%

echo Log file located at %loglocation%

echo Copy and Delete finished... Sending Report.
echo Copy and Delete finished... >> %loglocation%

echo Source Directory Contents: >> %loglocation%
echo. >> %loglocation%
dir %source% >> %loglocation%

echo. >> %loglocation%
echo. >> %loglocation%
echo. >> %loglocation%

echo Destination Directory Contents: >> %loglocation%
echo. >> %loglocation%
dir %destination% >> %loglocation%

date /t >> %loglocation%
time /t >> %loglocation%

echo Move Completed!!!

_
"_set destination=C:\test2_" works fine but if I change the command to 
_"__set destination=\\sdcauto\c$\test\" _it gives me parse error while doing the XCOPY command.

Can someone let me know how to resolve it?

I know the following Script maps to network drive and moves the files, but anyone can help me incorporate into the above script?

_ echo @off
net use z:\\%%j\c$ /user:domain\myusername mypassword
mkdir \\%%j\c$\tmp\mynewdir
xcopy c:\anyfile.txt \\%%j\c$\tmp\mynewdir
net use z: /delete_

Thanks


----------



## loserOlimbs (Jun 19, 2004)

Anywhere you want to use the UNC path, replace the UNC with a mapped drive:

\\server1\C$\KIdneyBeans\ you can just replace with J:\ if you map the drive first:

net use J: \\server1\C$\KIdneyBeans\ /user:domain\user Password

Mapping the drive needs to be done at the beginning of the script too.


----------

