Skip to content

Instantly share code, notes, and snippets.

@end2endzone
Last active November 9, 2023 01:30

Revisions

  1. end2endzone revised this gist Nov 9, 2023. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions backup_explorer_directories.bat
    Original file line number Diff line number Diff line change
    @@ -26,19 +26,19 @@ if NOT "%~1"=="" (

    :: Refresh depent variables
    title Backup explorer windows every !DELAY_IN_SECONDS! seconds...
    set RESTORE_SCRIPT=restore.!DELAY_IN_SECONDS!.bat
    set TEMP_SCRIPT=!RESTORE_SCRIPT!.tmp
    )
    ENDLOCAL & set DELAY_IN_SECONDS=%DELAY_IN_SECONDS% & set RESTORE_SCRIPT=%RESTORE_SCRIPT% & set TEMP_SCRIPT=%TEMP_SCRIPT%
    REM echo RESTORE_SCRIPT=%RESTORE_SCRIPT%
    REM echo TEMP_SCRIPT=%TEMP_SCRIPT%
    ENDLOCAL & set DELAY_IN_SECONDS=%DELAY_IN_SECONDS%

    :: Refresh depent variables, if DELAY_IN_SECONDS was updated.
    set RESTORE_SCRIPT=restore.%DELAY_IN_SECONDS%.bat
    set TEMP_SCRIPT=%RESTORE_SCRIPT%.tmp


    :check_no_restore
    SETLOCAL EnableDelayedExpansion
    if "%~2"=="/NO_RESTORE" (
    set ENABLE_RESTORE=0
    echo Diabled the restore script, even of available.
    echo Disabled the restore script, even if a backup is available.
    )
    ENDLOCAL & set ENABLE_RESTORE=%ENABLE_RESTORE%
    REM echo ENABLE_RESTORE=%ENABLE_RESTORE%
    @@ -49,7 +49,7 @@ SETLOCAL EnableDelayedExpansion
    if exist "%RESTORE_SCRIPT%" (
    echo Found a restore script: %RESTORE_SCRIPT%
    if "%ENABLE_RESTORE%"=="0" (
    echo Skipping restoration of previous backup file.
    echo Skipping restore script as requested by user.
    )
    if "%ENABLE_RESTORE%"=="1" (
    choice /C YN /M "Do you want to restore window directories as previously backup?"
  2. end2endzone revised this gist Nov 9, 2023. 1 changed file with 72 additions and 19 deletions.
    91 changes: 72 additions & 19 deletions backup_explorer_directories.bat
    Original file line number Diff line number Diff line change
    @@ -1,37 +1,77 @@
    @echo off

    :: This script backup the directory of all explorer.exe windows.
    :: For installation, create a shortcut to the script and
    :: place it in `shell:startup` directory.
    :: For installation, create a shortcut to the script and place it in `shell:startup` directory.
    :: Press CTRL+C to stop the script execution.

    ::
    :: Usage:
    :: backup_explorer_directories.bat [DELAY] /NO_RESTORE
    ::
    :: DELAY: The delay between backups in seconds.
    :: /NO_RESTORE: Do not try to restore the previous backup.
    ::
    title Backup explorer windows...

    set RESTORE_SCRIPT=restore.bat
    set DELAY_IN_SECONDS=300
    set RESTORE_SCRIPT=restore.%DELAY_IN_SECONDS%.bat
    set TEMP_SCRIPT=%RESTORE_SCRIPT%.tmp
    set ROLL_COUNTER=0
    set ENABLE_RESTORE=1

    :check_custom_delay
    SETLOCAL EnableDelayedExpansion
    if NOT "%~1"=="" (
    set DELAY_IN_SECONDS=%~1
    echo Delay between backups sets to !DELAY_IN_SECONDS! seconds.

    :: Refresh depent variables
    title Backup explorer windows every !DELAY_IN_SECONDS! seconds...
    set RESTORE_SCRIPT=restore.!DELAY_IN_SECONDS!.bat
    set TEMP_SCRIPT=!RESTORE_SCRIPT!.tmp
    )
    ENDLOCAL & set DELAY_IN_SECONDS=%DELAY_IN_SECONDS% & set RESTORE_SCRIPT=%RESTORE_SCRIPT% & set TEMP_SCRIPT=%TEMP_SCRIPT%
    REM echo RESTORE_SCRIPT=%RESTORE_SCRIPT%
    REM echo TEMP_SCRIPT=%TEMP_SCRIPT%


    :check_no_restore
    SETLOCAL EnableDelayedExpansion
    if "%~2"=="/NO_RESTORE" (
    set ENABLE_RESTORE=0
    echo Diabled the restore script, even of available.
    )
    ENDLOCAL & set ENABLE_RESTORE=%ENABLE_RESTORE%
    REM echo ENABLE_RESTORE=%ENABLE_RESTORE%


    :check_restore
    SETLOCAL EnableDelayedExpansion
    if exist "%RESTORE_SCRIPT%" (
    echo Found a restore script: %RESTORE_SCRIPT%
    choice /C YN /M "Do you want to restore window directories as previously backup?"
    :: Y=1, N=2
    if !ERRORLEVEL! equ 1 (
    echo Restoring previous window backup...
    cmd /C "%RESTORE_SCRIPT%"

    :: Wait 5 seconds before proceeding to window backup.
    powershell -Command "Start-Sleep -Seconds 5"

    echo done.
    echo.
    if "%ENABLE_RESTORE%"=="0" (
    echo Skipping restoration of previous backup file.
    )
    if "%ENABLE_RESTORE%"=="1" (
    choice /C YN /M "Do you want to restore window directories as previously backup?"
    :: Y=1, N=2
    if !ERRORLEVEL! equ 1 (
    echo Restoring previous window backup...
    cmd /C "%RESTORE_SCRIPT%"

    :: Wait 5 seconds before proceeding to window backup.
    powershell -Command "Start-Sleep -Seconds 5"

    echo done.
    echo.
    )
    )
    )
    ENDLOCAL


    echo Starting window directories backup.

    :backup
    :loop
    echo %DATE% %TIME%

    :: List directories opened by explorer.exe and
    @@ -42,15 +82,28 @@ if %errorlevel% neq 0 echo Failed getting extract directories from explorer.exe

    :: Delete previous backup script, if any
    if exist "%RESTORE_SCRIPT%" (
    :: Make 1 or more backups (*.bck) of the restore script. Comment the line below if you do not need this.
    call :roll

    del "%RESTORE_SCRIPT%"
    )

    :: The new temp script becomes the new restore script.
    ren "%TEMP_SCRIPT%" "%RESTORE_SCRIPT%"

    :: Waiting 300 secondes (5 minutes) before the next backup
    powershell -Command "Start-Sleep -Seconds 300"
    goto backup
    :: Waiting before the next loop (default to 5 minutes)
    powershell -Command "Start-Sleep -Seconds %DELAY_IN_SECONDS%"
    goto loop


    :roll
    :: Create a rolling backup of the restore script.
    :: Keep the restore file from the last 5 intervals...
    copy "%RESTORE_SCRIPT%" "%RESTORE_SCRIPT%.%DELAY_IN_SECONDS%.%ROLL_COUNTER%.bck" >NUL
    set /A ROLL_COUNTER=ROLL_COUNTER+1 >NUL
    if %ROLL_COUNTER% equ 10 set ROLL_COUNTER=0
    goto :EOF



    :EOF
  3. end2endzone revised this gist Oct 29, 2022. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion backup_explorer_directories.bat
    Original file line number Diff line number Diff line change
    @@ -11,11 +11,12 @@ set RESTORE_SCRIPT=restore.bat
    set TEMP_SCRIPT=%RESTORE_SCRIPT%.tmp

    :check_restore
    SETLOCAL EnableDelayedExpansion
    if exist "%RESTORE_SCRIPT%" (
    echo Found a restore script: %RESTORE_SCRIPT%
    choice /C YN /M "Do you want to restore window directories as previously backup?"
    :: Y=1, N=2
    if %errorlevel% equ 1 (
    if !ERRORLEVEL! equ 1 (
    echo Restoring previous window backup...
    cmd /C "%RESTORE_SCRIPT%"

    @@ -26,6 +27,7 @@ if exist "%RESTORE_SCRIPT%" (
    echo.
    )
    )
    ENDLOCAL

    echo Starting window directories backup.

  4. end2endzone revised this gist Oct 28, 2022. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions backup_explorer_directories.bat
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,25 @@ title Backup explorer windows...
    set RESTORE_SCRIPT=restore.bat
    set TEMP_SCRIPT=%RESTORE_SCRIPT%.tmp

    :check_restore
    if exist "%RESTORE_SCRIPT%" (
    echo Found a restore script: %RESTORE_SCRIPT%
    choice /C YN /M "Do you want to restore window directories as previously backup?"
    :: Y=1, N=2
    if %errorlevel% equ 1 (
    echo Restoring previous window backup...
    cmd /C "%RESTORE_SCRIPT%"

    :: Wait 5 seconds before proceeding to window backup.
    powershell -Command "Start-Sleep -Seconds 5"

    echo done.
    echo.
    )
    )

    echo Starting window directories backup.

    :backup
    echo %DATE% %TIME%

  5. end2endzone created this gist Oct 28, 2022.
    35 changes: 35 additions & 0 deletions backup_explorer_directories.bat
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    @echo off

    :: This script backup the directory of all explorer.exe windows.
    :: For installation, create a shortcut to the script and
    :: place it in `shell:startup` directory.
    :: Press CTRL+C to stop the script execution.

    title Backup explorer windows...

    set RESTORE_SCRIPT=restore.bat
    set TEMP_SCRIPT=%RESTORE_SCRIPT%.tmp

    :backup
    echo %DATE% %TIME%

    :: List directories opened by explorer.exe and
    :: prefix all elements with `@start ""` to convert each element as a valid command.
    :: Dump the output to a batch file.
    powershell "$directories = @((New-Object -com shell.application).Windows()).Document.Folder.Self.Path; $directories = $directories -replace '^', '@start \""\"" \""'; $directories -replace '$', '\""';"> "%TEMP_SCRIPT%"
    if %errorlevel% neq 0 echo Failed getting extract directories from explorer.exe && exit /b %errorlevel%

    :: Delete previous backup script, if any
    if exist "%RESTORE_SCRIPT%" (
    del "%RESTORE_SCRIPT%"
    )

    :: The new temp script becomes the new restore script.
    ren "%TEMP_SCRIPT%" "%RESTORE_SCRIPT%"

    :: Waiting 300 secondes (5 minutes) before the next backup
    powershell -Command "Start-Sleep -Seconds 300"
    goto backup


    :EOF