0

I’m trying to make this batch script automatic. The drive letter changes and i cant find a way in window to force it to use the same one each time. im not using the %date% environment variable because i need the date format to be like this: "YYYY_MM_DD"

Is there any way I can i get this script to run without user input?

    @echo off
    set /p "Drive_Letter=Enter Drive letter.>"
    set /p "BKDate=Enter date.>"
    cd\
    %Drive_Letter%:
    cd %Drive_Letter%:\Backup\
    md BK_%BKDate%
    cd\
    Robocopy /E c:\users\%username%\Dropbox\ %Drive_Letter%:\Backup\BK_%BKDate% *
    cd %Drive_Letter%:\Backup
    dir /s %Drive_Letter%:\Backup\BK_%BKDate%% >> LOG_%BKDate%.txt
4

2 Answers 2

0

This uses:

robocopy inside a subroutine to get the current date (an error is forced to get the time stamp of the error in yyyy/mm/dd format)

mountvol to enumerate the defined drives

vol to test drive accesibility

It will search for a "flag" folder (the backup folder) in the drives to determine which to use

Once all the information is retrived, the adecuated robocopy command is used.

@echo off
    setlocal enableextensions disabledelayedexpansion

    call :getDriveLetter "\Backup\bk*" drive
    if errorlevel 1 (
        echo Drive not found
        goto :eof
    )

    call :getTodayDate   today
    if errorlevel 1 (
        echo Date retrieval error
        goto :eof
    )

    set "BKDate=%today:/=_%"
    set "source=c:\users\%username%\Dropbox"
    set "target=%drive%:\Backup\BK_%BKDate%"

    robocopy "%source%" "%target%" /e
    dir /s "%target%" > "%drive%:\Backup\LOG_%BKDate%.txt"

    endlocal
    exit /b

:getTodayDate returnVar
    set "%~1=" & for /f %%a in (
        'robocopy "|" "." "%~nx0" /njh /r:0 /nocopy /l'
    ) do set "%~1=%%a" & exit /b 0
    exit /b 1

:getDriveLetter folderToTest returnVar
    set "%~2=" & for /f "tokens=1 delims=: " %%a in (
        'mountvol ^| find ":\"' 
    ) do vol %%a: >nul 2>&1 && (if exist "%%a:%~1" set "%~2=%%a" & exit /b 0)
    exit /b 1
0
0

This should do want you want.

@echo off
Z:
cd Z:\Backup\
Rem Get Day,Mth & Year from %Date%
set Day=%Date:~0,2%
set Mth=%Date:~3,2%
set Yr=%Date:~6,4%
md BK_%Yr%_%Mth%_%Day%

Robocopy /E %userprofile%\Dropbox\ Z:\Backup\BK_%Day%_%Mth%_%Yr% *

dir /s Z:\Backup\BK_%Yr%_%Mth%_%Day%% >> LOG_%Yr%_%Mth%_%Day%.txt

This Link will show you how to force the USB to use the same drive letter each time it is connected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.