Category Archives: Windows Batch Files

Windows Robocopy command common usage issues problems & gotchas

Published by:

Enclosing the source or destination in double quotes allows the use of paths with spaces in the folder names but DO NOT end with a trailing backslash ‘\’ as this will be interpreted as an escape character.

When using the task scheduler to run a robocopy command script be aware that using the account credentials of the logged-in user may be necessary for access to network drives referred to by the command. An administrator account may not have the necessary permissions.

      ROBOCOPY Source_folder Destination_folder [files_to_copy] [options]

      ROBOCOPY "M:\" "X:Destination Folder\" DON'T USE EITHER OF THESE

      ROBOCOPY M:\ "X:Destination Folder" THIS WORKS

Windows Batch File – Copy Folder from an Existing Template with User Input

Published by:

Paste the following code into a .bat text file to create a runnable script that copies an example template folder with contents to a new folder with a name entered by the user. There are an additional couple of lines that then rename and delete files in the new folder as an example of further customisation that could be automated.

@echo off
pushd %~dp0
REM request a Directory Name
set /p UserInput= "Create Directory: "
REM create directory using a source folder template
if not exist "%UserInput%" (
mkdir "%UserInput%"
xcopy /e SourceTemplateFolder "%UserInput%"
REM customise contents
rename "%UserInput%\Content.txt" NewContent.txt
del "%UserInput%\FileToDelete.txt"

Windows Batch File – Back Up DropBox to Removable USB drive

Published by:

Paste the following code into a .bat text file to create a runnable script that copies your DropBox folder contents to a specified folder on any removable drives (D: thru H:) present. This excludes the dropbox cache and other hidden and system files. Double-click the batch file or run from the command line. You can change the destination folder name (after ‘Destination=’) or the source dropbox folder location if different (“%USERPROFILE%\Dropbox”). You could also edit the example file (‘Excludefile.123’) to exclude other files or file types (like ‘*.zip’) or directories (add to ‘.dropbox.cache’) – use spaces to separate multiple entries – see Robocopy for more information. A log file is created in the root of the destination drive recording what was done.

TITLE Backup
SET Destination=FolderOnUSB
SET Counter=0
ECHO  DropBox BackUp Job to folder '%Destination%' on removable USB drives
ECHO  *** Excluding hidden '.dropbox.cache' folder, system files ***
ECHO  *** WARNING files-in-use may be skipped ***
CALL :SleepSeconds 5
REM loop thru drive letters
FOR %%x in (D E F G H) DO (CALL :CopyToDrive %%x)
ECHO  Finished - Drives updated = %Counter%

REM  **************Functions*****************
SET DriveLetter=%~1
REM ECHO  Searching for %DriveLetter%:\%Destination%
IF EXIST "%DriveLetter%:\%Destination%\" (
ECHO  %DriveLetter%: Data Drive updating
CALL :SleepSeconds 5
ROBOCOPY "%USERPROFILE%\Dropbox" "%DriveLetter%:\%Destination%" /MIR /FFT /Z /NP /TEE /XA:H /XD .dropbox.cache /W:5 /XJ /XF ExcludeFile.123 /NFL /log:"%DriveLetter%:\%Destination%-CopyLog.txt"
set /a Counter=%Counter%+1)

REM ========================================
SET DelaySec=%~1
TIMEOUT /T  %DelaySec% /NOBREAK > nul

Windows Batch File – to create an empty folder structure in the same folder

Published by:

Paste the following code into a .bat text file to create a runnable script that makes a new empty folder structure within the same directory by double-clicking the file or running from the command line. You can change the CALL lines to reflect the folder names you require.

TITLE Make Folders
REM use path of script %~dp0 as working directory 
REM this ensures that working directory is set expicitly
REM if run from CMD prompt the script path might be otherwise ignored
pushd %~dp0
ECHO Making Empty Folder Structure 
CALL :MakeFolder My-Config
REM create a folder with a space in the name use quotes "My Config"
CALL :MakeFolder My-Desktop\Sub-folder
REM create a sub folder
CALL :MakeFolder My-Documents
CALL :MakeFolder My-Downloads
CALL :MakeFolder My-Music
CALL :MakeFolder My-Pictures
CALL :MakeFolder My-Videos
REM create folder function
SET fldr=%~1
if not exist %fldr% MD %fldr%