Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Copy using .bat

  • 16-01-2016 6:12pm
    #1
    Posts: 15,661 ✭✭✭✭


    Hi guys been years since I've used .bat or xcopy so just need to check I've got things right :o

    I basically need to copy a large amount of files to an external drive , I could do that manually but I will need to copy additional files to the external drive occasionally as they get added to the source drive, from that point I will only need to copy new files , so if I recall I can just use /A in the initial script to set an archive attribute, so that only new files get copied :confused:

    Xcopy /S /I /E /A Source_DirectoryA Destination_directoryA
    Xcopy /S /I /E /A Source_DirectoryB Destination_directoryB

    I have 2 main folders with many sub folders


Comments

  • Registered Users, Registered Users 2 Posts: 3,091 ✭✭✭Antar Bolaeisk


    Robocopy is most likely the best solution for something like this:

    https://technet.microsoft.com/en-us/library/cc733145.aspx

    Discussion on Spiceworks for doing something similar to what you're describing:
    https://community.spiceworks.com/topic/209883-robocopy-only-copy-new-changed-files (just be careful with /MIR if you don't want things deleted on the destination end)


  • Posts: 15,661 ✭✭✭✭ [Deleted User]


    cheers, I'll have a look.


  • Registered Users, Registered Users 2 Posts: 5,150 ✭✭✭homer911


    I have to regularly transfer files between two pcs using a datastick and use the following bat file to backup to datastick, with a complementary script to restore off the datastick. The script copies any new/updated files and deletes any files no longer needed on the datastick. It also detects which drive is assigned to the datastick..

    ***********************************************

    REM SCRIPT TO BACKUP MY FILES TO FLASH DRIVE, DELETE SURPLUS FILES ON BACKUP

    cls

    @ECHO ** BACKUP MY FILES TO FLASH DRIVE **
    @ECHO.
    @ECHO Path being backed up is C:\MYFiles
    @ECHO.
    @ECHO ********************************************************************
    @ECHO Please shutdown "Application" and press any key when ready to continue
    @ECHO ********************************************************************
    @ECHO.
    @ECHO off
    PAUSE
    @ECHO.
    @ECHO off

    REM Check to see which USB port is in use for the Flash Drive

    if exist F: (GOTO BACKUP_F) ELSE GOTO G_CHECK

    :BACKUP_F
    ECHO Backing up to the F: Drive...
    @ECHO.
    XCOPY C:\MyFiles\*.* /E /C /I /K /Y /D /Q F:\Myfiles\*.*
    @ECHO.
    @ECHO File Backup Completed
    @ECHO.
    @ECHO Deleting surplus files on Backup
    REM DELETE FROM BACKUP THE FILES NOT ON HARD DRIVE - RUN AS PART OF BACKUP
    FOR /R "F:\MyFiles\" %%I in (*) DO IF NOT EXIST "C:%%~pnxI" DEL "%%~I"
    @ECHO.
    @ECHO Surplus files deleted

    GOTO THE_END

    :G_CHECK
    if exist G: (GOTO BACKUP_G) ELSE GOTO NO_FLASH_DRIVE

    :BACKUP_G
    ECHO Backing up to the G: Drive...
    @ECHO.
    XCOPY C:\Myfiles\*.* /E /C /I /K /Y /D /Q G:\Myfiles\*.*
    @ECHO.
    @ECHO File Backup Completed
    @ECHO.
    @ECHO Deleting surplus files on Backup
    REM DELETE FROM BACKUP THE FILES NOT ON HARD DRIVE - RUN AS PART OF BACKUP
    FOR /R "G:\MyFiles\" %%I in (*) DO IF NOT EXIST "C:%%~pnxI" DEL "%%~I"
    @ECHO.
    @ECHO Surplus files deleted
    GOTO THE_END

    :NO_FLASH_DRIVE
    @ECHO ********************************************************************
    @ECHO No Flash Drive was detected in F: or G:
    @ECHO No Backup generated
    @ECHO Please insert a Flash Drive and rerun this script
    @ECHO ********************************************************************

    :THE_END

    @ECHO.
    PAUSE


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 93,563 Mod ✭✭✭✭Capt'n Midnight


    homer911 wrote: »
    I have to regularly transfer files between two pcs using a datastick and use the following bat file to backup to datastick, with a complementary script to restore off the datastick. The script copies any new/updated files and deletes any files no longer needed on the datastick. It also detects which drive is assigned to the datastick..
    If there are too many files then XCOPY will keel over and i've had problems with the %%i when files have special characters in their names

    ROBOCOPY replaced it a long time ago.

    /mir mirrors ( but deletes so be careful )
    /move moves files


  • Moderators, Arts Moderators, Regional Abroad Moderators Posts: 11,106 Mod ✭✭✭✭Fysh


    +1 for robocopy, it's the business!

    Copy all files from one directory to another:
    robocopy <sourcedir> <targetdir> * /e /z /r:3 /w:0
    

    Copy specific files from one directory to another:
    robocopy <sourcedir> <targetdir>  <filename1> ...<filenameX> /e /z /r:3 /w:0
    

    /e will include empty directories.
    /z uses restartable mode, meaning you can resume transfers that fail part-way through.
    /r sets the number of times to try failed transfers (the default is something ridiculous like 1 million, IIRC)
    /w sets the time to wait between attempts for failed transfers (the default is 30 seconds)

    You can also add /log:<pathtolog> for a logfile (or log+:<pathtolog> if you want to append to an existing log file).
    As Capt. Midnight says, /mir will mirror the source directory to the destination - be careful, though, this will delete folders that only exist in the destination! /mov will move files from the source to the destination.
    The usual windows CLI rules apply i.e. any paths containing spaces should be enclosed in quotes.

    One last thing: I just learned learned something new about robocopy - using the /mir option will update the security settings on the folder in the destination, whereas using /e and /purge would not.


  • Advertisement
Advertisement