Last active
November 9, 2023 01:30
-
-
Save end2endzone/6032f3b77c1aaf2b4d7f7783cbbd8a8c to your computer and use it in GitHub Desktop.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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. | |
:: | |
:: 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 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... | |
) | |
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 Disabled the restore script, even if a backup is 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% | |
if "%ENABLE_RESTORE%"=="0" ( | |
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?" | |
:: 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. | |
:loop | |
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%" ( | |
:: 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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment