Skip to content

Instantly share code, notes, and snippets.

@end2endzone
Last active November 9, 2023 01:30
Show Gist options
  • Save end2endzone/6032f3b77c1aaf2b4d7f7783cbbd8a8c to your computer and use it in GitHub Desktop.
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.
@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
: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%
:: 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment