none
Copying Multiple files and directory creation using a batch file RRS feed

  • Question

  • I need to create a new folder and copy multiple files into multiple destination directories with a batch file. The destination folder will have a standard location, "\\server\metfabdir" with multiple student subfolders.

    \\server\metfabdir\student1, \\server\metfabdir\student2, etc.

     I already use this script to copy files which works well (for the exception that it copies the file to the root dir too which I don't want but can live with)

    set Source="C:\mysourcefolder\file.doc"
    set Destinfol="\\Destinationfolder\share"
    for /r %Destinfol% %%d in (.) do (
        (
            xcopy %Source% "%%~d"
        )
    )

    I want to add a new folder to the students folder called "test" and then add the files to that folder only.

    I have tried a few different ideas but nothing works. Any ideas?


    • Edited by trimark1 Saturday, July 22, 2017 1:24 PM
    Saturday, July 22, 2017 12:31 PM

Answers

  • Should be something like this

    set Source="C:\mysourcefolder\file.doc"
    set Destinfol="\\Destinationfolder\share"
    for /r %Destinfol% %%d in (.) do (
        (
           
           mkdir "%%~d\TEST" 
           xcopy %Source% "%%~d\TEST"
        )
    )

    or this version with some control an log
    @echo off
    set SourcePath=C:\mysourcefolder
    Set singlefile=file.doc
    :: set rootShare=\\server\metfabdir
    set rootShare=c:\temp
    Set newFolder=TESTexam
    pushD "%rootShare%"
    if %errorlevel% EQU 0 (echo Root ok %rootShare%) else (echo %rootShare% & goto :ERR)
    for /F %%d in ('dir .  /b /ad') do (
    		pushD  "%%~d"
    		if %errorlevel% EQU 0 (echo Do work on folder "%%~d") else (echo  "%%~d" & goto :ERR)
    		if not exist ".\%newFolder%" mkdir ".\%newFolder%"
                    robocopy "%SourcePath%" ".\%newFolder%" "%singlefile%" /log+:"%temp%\_rcopy.log">nul
    		popD
    )
    
    goto :eof
    :err
    echo path not found 
    


    Gastone Canali >http://www.armadillo.it


    Se alcuni post rispondono al tuo quesito(non necessariamente i miei), ricorda di contrassegnarli come risposta e non dimenticare di contrassegnare anche i post utili. GRAZIE! Ricorda di dare un occhio ai link Click Here andHere


    • Edited by GastoneCanali Sunday, July 23, 2017 9:59 PM
    • Marked as answer by trimark1 Sunday, July 23, 2017 11:36 PM
    Sunday, July 23, 2017 9:59 PM

All replies

  • This should be done by RoboCopy.

    RoboCopy /?

    Batch file are pretty much obsolete for doing anything serious.


    \_(ツ)_/

    • Marked as answer by trimark1 Sunday, July 23, 2017 2:00 PM
    • Unmarked as answer by trimark1 Sunday, July 23, 2017 2:05 PM
    • Proposed as answer by GastoneCanali Sunday, July 23, 2017 8:55 PM
    • Unproposed as answer by GastoneCanali Sunday, July 23, 2017 8:57 PM
    Saturday, July 22, 2017 3:53 PM
  • Guess I am too old school...didn't even know this existed. Been doing batch files for 35 years. Looks like I gotta keep up with the times,

    Thanks

    In looking at Robocopy, how do I recurse destination subdirectories?

    • Edited by trimark1 Sunday, July 23, 2017 2:20 PM
    Sunday, July 23, 2017 2:01 PM
  • Should be something like this

    set Source="C:\mysourcefolder\file.doc"
    set Destinfol="\\Destinationfolder\share"
    for /r %Destinfol% %%d in (.) do (
        (
           
           mkdir "%%~d\TEST" 
           xcopy %Source% "%%~d\TEST"
        )
    )

    or this version with some control an log
    @echo off
    set SourcePath=C:\mysourcefolder
    Set singlefile=file.doc
    :: set rootShare=\\server\metfabdir
    set rootShare=c:\temp
    Set newFolder=TESTexam
    pushD "%rootShare%"
    if %errorlevel% EQU 0 (echo Root ok %rootShare%) else (echo %rootShare% & goto :ERR)
    for /F %%d in ('dir .  /b /ad') do (
    		pushD  "%%~d"
    		if %errorlevel% EQU 0 (echo Do work on folder "%%~d") else (echo  "%%~d" & goto :ERR)
    		if not exist ".\%newFolder%" mkdir ".\%newFolder%"
                    robocopy "%SourcePath%" ".\%newFolder%" "%singlefile%" /log+:"%temp%\_rcopy.log">nul
    		popD
    )
    
    goto :eof
    :err
    echo path not found 
    


    Gastone Canali >http://www.armadillo.it


    Se alcuni post rispondono al tuo quesito(non necessariamente i miei), ricorda di contrassegnarli come risposta e non dimenticare di contrassegnare anche i post utili. GRAZIE! Ricorda di dare un occhio ai link Click Here andHere


    • Edited by GastoneCanali Sunday, July 23, 2017 9:59 PM
    • Marked as answer by trimark1 Sunday, July 23, 2017 11:36 PM
    Sunday, July 23, 2017 9:59 PM
  • Gastone!

    Exactly what I am looking for! Code worked awesome without any modifications. Especially since it's coded to accept wildcards and a log.

    You are the man!

    Monday, July 24, 2017 12:05 AM