Posts Tagged ‘Robocopy’

Batch-scripting

Friday, March 21st, 2008

Batch-scripting is not as powerful as WSH-scripting or PowerShell.
But with some tools you can perform some simple but useful actions.
One tool I often use is robocopy.

  1. Asking for values in a batch-script
    @echo off
    @set /P pctocheck=Enter name of pc:
    @cscript.exe "script.vbs" %pctocheck%
    pause
  2. Copy non-existing folders back to C-drive
    @echo off
    rem Check if the script was called with parameter RUN
    IF "%1"=="RUN" GOTO run
    rem Since we are not running the script with parameter RUN, start it using cmd whilst enabling delayed environment variable expansion (/V:ON)
    cmd /V:ON /C H:\copy_c-drive.cmd RUN
    GOTO :EOF
    :run
    echo ...Copying extra folders on C-drive
    rem List all existing files and directories on the C-drive, including hidden ones.
    dir /a:d /b C:\ >C:\WINDOWS\TEMP\rc_existsystemdirs.txt
    dir /a:hd /b C:\ >>C:\WINDOWS\TEMP\rc_existsystemdirs.txt
    dir /a:-d /b C:\ >C:\WINDOWS\TEMP\rc_existsystemfiles.txt
    dir /a:h-d /b C:\ >>C:\WINDOWS\TEMP\rc_existsystemfiles.txt
    rem Build up variables
    set existingdirs=
    FOR /F "delims=," %%i IN (C:\WINDOWS\TEMP\rc_existsystemdirs.txt) do set existingdirs=!existingdirs! /XD "%%i"
    set existingfiles=
    FOR /F "delims=," %%i IN (C:\WINDOWS\TEMP\rc_existsystemfiles.txt) do set existingfiles=!existingfiles! /XF "%%i"
    rem Do the copy thing
    robocopy /COPYALL /E X:\ C:\ /TEE /LOG+:C:\WINDOWS\TEMP\rc_system.log /XO /R:1 /W:3 %existingdirs% %existingfiles%