[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1970
  • Last Modified:

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.


1 Solution
Chad FranksCommented:
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

SQLSearcherAuthor Commented:
Hello Chad
I am getting the following error.

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


Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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.

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now