Bat file to copy from a network unc path

Posted on 2014-08-18
Last Modified: 2014-08-18
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.


Question by:SQLSearcher
    LVL 6

    Expert Comment

    by:Chad Franks
    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
    LVL 82

    Accepted Solution

    "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


    Author Comment

    Hello Chad
    I am getting the following error.

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


    LVL 35

    Expert Comment

    On that computer, using Explorer, does "\\ntserver2\data\08\AC4\Moulding\" exist?
    LVL 82

    Expert Comment

    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.

    Author Closing Comment

    Thank you, oBdA.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now