Bat file to copy from a network unc path

Hello Experts Exchange
I am trying to write a bat file that will copy files from a folder that is a UNC path.

I have a bit of the script ready that give me the month. Which I put into a variable called mydate.

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=\%%a\)

Xcopy "\\ntserver2\data"%mydate%"AC4\Moulding\*.*" "Y:\GGR\moulding"

Open in new window

Does anyone know how I can get this to work as the XCopy line does not work.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chad FranksSenior System EngineerCommented:
I would use ROBOCOPY.  Its part of windows 7 and its much more efficient than Xcopy;

robocopy "\\ntserver2\data"%mydate%"AC4\Moulding\*.*"  "Y:\GGR\moulding" /E /IS
"Does not work" is not a very suitable error description. What exactly is the error you're getting from xcopy?
If you don't have time to read the error message because the window closes too fast, open a command prompt, and start the script from the prompt instead using a double-click in Explorer.
So your source folder has the current month in it?
Use this to get the date, it doesn't rely on the (user and OS dependent) date and time format. In addition, Microsoft recommends using robocopy.exe (part of the OS since Vista) instead of xcopy.
@echo off
setlocal enabledelayedexpansion
echo Getting time ...
for /f "tokens=1-9" %%a in ('wmic Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
	set /a Line += 1
	if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
	if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set SourceFolder=\\ntserver2\data\%Month%\AC4\Moulding
set TargetFolder=Y:\GGR\moulding
echo Source folder: %SourceFolder%
echo Target folder: %TargetFolder%
robocopy.exe "%SourceFolder%" "%TargetFolder%" *.* /r:0 

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SQLSearcherAuthor Commented:
Hello Chad
I am getting the following error.

ERROR : Invalid Parameter #1 : "\\ntserver2\data\08\AC4\Moulding\*.*"


Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

On that computer, using Explorer, does "\\ntserver2\data\08\AC4\Moulding\" exist?
You can't just replace xcopy syntax with robocopy syntax, see my script.
robocopy expects the source folder as first argument, the target folder as second argument, then the file mask(s), then further options. Chad's example has the file mask as part of the source folder.
In addition, note that /E that Chad posted will copy all subdirectories, including empty ones; this was not a part of your original xcopy syntax, so I left it out.
Note, too, that robocopy will by default skip files that already exist in the same version (size and time stamp) in the target folder; /IS is "Include Same" and will (usually unnecessarily) copy files that are already there.
SQLSearcherAuthor Commented:
Thank you, oBdA.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.