Page 1 of 2

Key Bindings Backup Script for Windows

Posted: Sat Feb 24, 2018 2:02 pm
by b00st3d
Hello Cmdrs! I am by trade a system administrator, so any chance I get, I'll write a script or program to do something on a computer. With that said, version 3.0 coming soon so I wrote this to help you with backing up your keybindings. What it does is copies the content of the keybindings folder to a folder on your desktop, allows you to restore your keybindings folder by doing the same in reverse, and also has quick links to both locations.

TO USE THIS SCRIPT YOU MUST BE ON WINDOWS.
1.) Copy everything in the code tag below into a notepad.
2.) Save the notepad as keybindingbackup.bat (the name isn't nearly as important as the .bat)
3.) Launch the script and choose what you want to do. It gives you a menu.
4.) Profit?


Updated: Found an error where it was having trouble determining errorlevel when inside an if statement was allowing for a command to be canceled. Added setlocal enabledelayedexpansion in order to get around this issue.

keybindingbackup.bat

Code: Select all

@goto script
This script was written for the elite dangerous.  What it does is backup your current bindings to your desktop.  You will then be able to restore the backups to the game folder.  When backups are restored, it will backup the original folder then copy the backed up files into the game folder.
Author b00st3d@nrevolt.net
:script
@echo off&setlocal enabledelayedexpansion
cd %desktop%
set savepath="%userprofile%\desktop\BindingsBackup"
set bindings="%localappdata%\Frontier Developments\Elite Dangerous\Options\Bindings"
title Backup and Restore Elite Dangerous Bindings

:top
cls
echo Bindings location: 
echo %bindings%
echo.
echo Backup location:
echo %savepath%
echo.
echo.
echo ++++++++++++++++++++++++++++++++++++++++++++++++++
echo + This script will backup your bindings folder   +
echo + to your desktop.                               +
echo +                                                +
echo + 1. Backup bindings                             +
echo + 2. Restore bindings                            +
echo + 3. Open bindings folder                        +
echo + 4. Open backup folder                          +
echo +                                                +
echo + 5. Exit                                        +
echo +                                                +
echo ++++++++++++++++++++++++++++++++++++++++++++++++++
echo.
choice /C 12345 /N /M "Please enter a selection: "
if %errorlevel% == 1 goto backup
if %errorlevel% == 2 goto restore
if %errorlevel% == 3 start "" %bindings%&goto top
if %errorlevel% == 4 start "" %savepath%&goto top
REM if the user doesn't select 1-4 automatically exit.
exit
:backup
cls
REM check if the savepath exists
call :foldercheck %savepath%

REM if the folder exists then warn the user they are about to overwrite
if %errorlevel% == 1 (
   choice /C yn /N /M "Bindings have already been backed up.  Would you like to overwrite? [y,n]: "
   if !errorlevel! == 1 rd /s/q %savepath%\&md %savepath%
   if !errorlevel! == 2 goto top
) else (
REM if the folder doesn't exist then make it
   md %savepath% >nul
)     
REM copy the bindings to the new backup folder
xcopy /I /Q /S %bindings% %savepath%
echo.
echo Backup complete.
echo Backup is located on your desktop in the BindingsBackup folder
timeout 5 >nul
goto top

:restore
cls
REM check if the backup exists
call :foldercheck %savepath%
if %errorlevel% == 1 (
REM warn the user they are about to overwrite their current bindings with a backup

   echo If you continue your current bindings will be replaced by the bindings located in
   echo %userprofile%\desktop\BindingBackup. 
   echo.
   choice /c yn /n /m "Continue? [y,n]: "
   if !errorlevel! == 2 goto top
   if !errorlevel! == 1 (
      cd %bindings%
      cd ../
REM backup the current folder to Bindings.bak just in case
      xcopy /I /S Bindings Bindings.bak
REM delete the Bindings folder and everything in it
      rd /s/q Bindings
REM Create a new bindings folder and move the backup into it
      md Bindings
      xcopy /I /S %savepath% %bindings%
   )
) else (
      echo Error!  No backup located!
      timeout 3 >nul
      goto top
   )
goto top


REM This function comes from M. Jamal on https://stackoverflow.com/questions/138981/how-to-test-if-a-file-is-a-directory-in-a-batch-script
REM This function will return an errorlevel depending on whether the supplied folderpath is valid.
REM Errorlevel 0 means file/folder doesn't exist
REM Errorlevel 1 means exists, and it is a folder
REM Errorlevel 3 means exists, and it is a file
:foldercheck <path>
@pushd %~dp1
@if not exist "%~nx1" (
        popd
        exit /b 0
) else (
        if exist "%~nx1\*" (
                popd
                exit /b 1
        ) else (
                popd
                exit /b 3
        )
)

Re: Key Bindings Backup Script for Windows

Posted: Sat Feb 24, 2018 2:14 pm
by Skonkerz
This is very useful and confirmed as working perfectly! :D

Thank you CMDR

Re: Key Bindings Backup Script for Windows

Posted: Sat Feb 24, 2018 3:05 pm
by b00st3d
Glad you like it Skonkerz. I'm also looking for critiques. If you see something wrong with the script or something that should be done differently, please feel free to bring it up.

Re: Key Bindings Backup Script for Windows

Posted: Sat Feb 24, 2018 3:36 pm
by Hengest
Excellent, worked perfectly.

Thank you

Re: Key Bindings Backup Script for Windows

Posted: Sat Feb 24, 2018 6:35 pm
by LeDoyen
pretty handy with 3.0 being only days away

Re: Key Bindings Backup Script for Windows

Posted: Sat Feb 24, 2018 8:36 pm
by GreenViper
Handy indeed, thanks CMDR !

Re: Key Bindings Backup Script for Windows

Posted: Tue Feb 27, 2018 10:10 am
by cussine
Thank You, should be made a sticky :)

Re: Key Bindings Backup Script for Windows

Posted: Tue Feb 27, 2018 11:15 am
by Judson
Thank you very neat and simple. A bit like me! :p

Re: Key Bindings Backup Script for Windows

Posted: Tue Feb 27, 2018 2:17 pm
by b00st3d
Found an error in the script and updated. The error I found was when you try to cancel an action, it would ask for permission then go ahead with the action regardless of the user's choice. This was caused by not having delayed expansion enabled. Without delayed expansion, when a statement is evaluated, it doesn't update until after the statement is complete. With delayed expansion, I can enter a statement, change a value and have the change reflect immediately.

here is an example of how that works for those that care about batch scripting

Code: Select all

@echo off&setlocal enabledelayedexpansion

set test=1

if %test% == 1 (
   echo %test%
   set /p test="Please enter a new value: "
   echo.
   echo.
   echo with delayedexpansion disabled
   echo %test%
   pause
   echo.
   echo.
   echo with delayedexpansion enabled
   echo !test!
   pause
)

Re: Key Bindings Backup Script for Windows

Posted: Tue Feb 27, 2018 2:44 pm
by Judson
Added with permission to the OoM Inara wing documents. Thanks again Cmdr b00st3d