Batch File Help

cybermaniac

New member
Hey guys,

I have built the following batch file:

Code:
@echo off

REM Snell Backup Batch File

REM Created and Modified by Alex Lane

COLOR 0e

:: variables

set dateNtime="%date:~0,2%-%date:~3,2%-%date:~6,6%"

set drive=H:\Private\ITBackup\%dateNtime%

set backupcmd=xcopy /s /c /d /e /h /i /r /y /k /v

:Start

cls

echo Press 1 to Backup Files. 

echo Press 2 to Restore Files. 

echo Press 3 to Restore Firefox Profile.

echo Press 4 to Quit.

set /p userinp=Choice #: 

set userinp=%userinp:~0,1%

if "%userinp%"=="1" goto 1

if "%userinp%"=="2" goto 2

if "%userinp%"=="3" goto 3

if "%userinp%"=="4" goto 4

echo '%userinp%' is not a valid choice, press any key to try again.

pause>nul

goto start

:1

echo ### Backing up files...

echo ### Setting backup drive as: %drive%

echo ### Backing up Custom Dictionary

%backupcmd% "%USERPROFILE%\Application Data\microsoft\proof" "%drive%\Outlookbackup\Proof"

echo ### Backing up Outlook Autocomplete

%backupcmd% "%USERPROFILE%\Application Data\microsoft\outlook" "%drive%\Outlookbackup\Outlook"

echo ### Backing up Outlook Signatures

%backupcmd% "%USERPROFILE%\Application Data\microsoft\signatures" "%drive%\Outlookbackup\Signatures"

echo ### Backing up Internet Exlorer Favourites

%backupcmd% "%USERPROFILE%\Favorites" "%drive%\Favorites"

REM ### You now have the final countdown stuck in your head

echo ### Backing up Firefox Profile

%backupcmd% "%USERPROFILE%\Application Data\Mozilla\Firefox\Profiles" "%drive%\Firefox\Profiles"

echo ### Backing up My Documents

%backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Documents"

echo ### Backing up Desktop

%backupcmd% "%USERPROFILE%\Desktop" "%drive%\Desktop"

echo ### Backing up Desktop Settings (Wallpaper, screensaver etc)

reg export "HKCU\Control Panel\Desktop" "%drive%\Desktop Settings Backup\backup.REG"

echo ### Listing all programs installed on the computer

regedit /e %temp%\regexport.txt "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall"

find "DisplayName" %temp%\regexport.txt >%temp%\regprogs.txt

for /f "tokens=2 delims==" %%a in (%temp%\regprogs.txt) do echo %%~a >>"%drive%\installedprogs.txt"

del %temp%\regexport.txt

del %temp%\regprogs.txt

goto endbackup

:2

echo ### Restoring files...

echo ### Restoring files from %drive%

echo ### Restoring Custom Dictionary

%backupcmd% "%drive%\Outlookbackup\Proof" "%USERPROFILE%\Application Data\microsoft\proof"

echo ### Restoring Outlook Autocomplete

%backupcmd% "%drive%\Outlookbackup\Outlook" "%USERPROFILE%\Application Data\microsoft\outlook"

REM ### You have lost the game

echo ### Restoring Outlook Signatures

%backupcmd% "%drive%\Outlookbackup\Signatures" "%USERPROFILE%\Application Data\microsoft\signatures"

echo ### Restoring Internet Exlorer Favourites

%backupcmd% "%drive%\Favorites" "%USERPROFILE%\Favorites"

echo ### Restoring My Documents

%backupcmd% "%drive%\My Documents" "%USERPROFILE%\My Documents"

echo ### Restoring Desktop

%backupcmd% "%drive%\Desktop" "%USERPROFILE%\Desktop"

goto endrestore

:3

echo ### Restoring Firefox Profile

%backupcmd% "%drive%\Firefox\Profiles" "%USERPROFILE%\Application Data\Mozilla\Firefox\Profiles"

goto endrestore

:endbackup

cls

echo Backup Complete!

echo.

echo.

echo.

echo Thank you for using the IT Department's Profile autobackup facility

echo.

echo Your Backup is located at %drive%

echo Please press any key to exit

pause>nul

exit

:endrestore

cls

echo Restore Complete!

echo.

echo.

echo.

echo Thank you for using the IT Department's Profile autobackup facility

echo.

echo Your Backup has been restored from %drive%

echo Please press any key to exit

pause>nul

exit

:4

reg query "HKCU\Control Panel\Desktop" /v ConvertedWallpaper > "H:\Private\backup.txt"

find ConvertedWallpaper "H:\Private\wpexp.txt" "H:\Private\Wallpaper.txt"

set wallpaper < "H:\Private\Wallpaper.txt"

pause

cls

echo Thank you for using the IT Department's Profile autobackup facility

echo.

echo Please press any key to exit

pause>nul

exit

Most of this works, except for one bit that I am trying to implement:

this is what I have so far:

Code:
reg query "HKCU\Control Panel\Desktop" /v ConvertedWallpaper > "H:\Private\backup.txt"

find ConvertedWallpaper "H:\Private\wpexp.txt" "H:\Private\Wallpaper.txt"

set wallpaper < "H:\Private\Wallpaper.txt"

What I am trying to do is this:

Back up the user's current desktop background, chosen screen saver, and misc values that go with it.

now, exporting the registry file is easy as seen here:

Code:
echo ### Backing up Desktop Settings (Wallpaper, screensaver etc)

reg export "HKCU\Control Panel\Desktop" "%drive%\Desktop Settings Backup\backup.REG"

But to be able to actually restore the background + screensaver, one would ideally need to be able to get the dynamic value of the wallpaper and screensaver path + file name.

Does anyone know how I would go about this, WITHOUT using an external exe?

Thanks guys!
 
Can't you call the MS Files and Settings transfer wizard to pull that information and then /q restore that?
 
probably, but it would most likely back up more than I wish, I'd rather have specific batch file to do the job.

Might need to package up setx :/
 
I think that windows creates a copy of the background to a specific file called background or summat like that.
 
So far I've managed to do the following:

Code:
@echo off

reg query "HKCU\Control Panel\Desktop" /v Wallpaper > "h:\backup.txt"

@ping 127.0.0.1 -n 2 -w 1000 > nul

setx bob -f h:\backup.txt -r 0,1 REG_SZ

@pause > nul

It allows me to see the current value of the reg key called "wallpaper" (current wallpaper), and export it to a text file.

It then waits 2 seconds until it calls upon setx, and reads the line.

question is....how do I now do the following?:

A) call upon variable "bob" to back up that particular file,

B) once I restore the registry key, how do I then restore that dynamic file (as the wallpaper might always have a different name depending on what the user has used as a background)?
 
awesome, managed to sort it out. No need for setx or any other external file/program. Here is the final script:

Code:
@echo off

REM Snell Backup Batch File

REM Created and Modified by Alex Lane

REM This script is designed to back up user files.

REM It current has the following features:

REM Number Choice error correction.

REM OS Detection. (Will only work on Windows XP, if another OS is detected, it will either run custom code or give an incompatability error.

REM Will backup (and restore): Microsoft Office custom dictionary (including outlook dictionary), Outlook Autocomplete, Outlook Signatures, Internet Explorer favourites, User "My Documents", User Desktop and will list all programs installed.

REM It will also backup and restore Firefox Profile (including all plugins).

REM This script is also designed to backup user defined Wallpaper and Screensaver. However it is not designed to restore these due to probable lack of local admin rights blocking access to write to the registry.

COLOR 0e

:: variables

set drive=H:\Private\ITBackup\

set backupcmd=xcopy /s /c /d /e /h /i /r /y /k /v /z

:FindOS

ver | find "2003" > nul

if %ERRORLEVEL% == 0 goto ver_2003

ver | find "XP" > nul

if %ERRORLEVEL% == 0 goto ver_xp

ver | find "2000" > nul

if %ERRORLEVEL% == 0 goto ver_2000

ver | find "NT" > nul

if %ERRORLEVEL% == 0 goto ver_nt

if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit

systeminfo | find "OS Name" > %TEMP%\osname.txt

FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i

echo %vers% | find "Windows 7" > nul

if %ERRORLEVEL% == 0 goto ver_7

echo %vers% | find "Windows Server 2008" > nul

if %ERRORLEVEL% == 0 goto ver_2008

echo %vers% | find "Windows Vista" > nul

if %ERRORLEVEL% == 0 goto ver_vista

goto warnthenexit

:ver_xp

cls

echo You have Windows XP

echo.

:Start

echo ### Backup drive set as: %drive%

echo.

echo Press 1 to Backup Files. 

echo.

echo Press 2 to Restore Files.

echo.

echo Press 3 to Backup Wallpaper

echo.

echo Press 4 to Backup Firefox Profile.

echo.

echo Press 5 to Restore Firefox Profile.

echo.

echo Press 6 to Quit.

echo.

set /p userinp=Choice #: 

set userinp=%userinp:~0,1%

if "%userinp%"=="1" goto 1

if "%userinp%"=="2" goto 2

if "%userinp%"=="3" goto 3

if "%userinp%"=="4" goto 4

if "%userinp%"=="5" goto 5

if "%userinp%"=="6" goto 6

echo '%userinp%' is not a valid choice, press any key to try again.

pause>nul

goto start

:1

echo ### Backing up files...

echo ### Setting backup drive as: %drive%

echo ### Backing up Custom Dictionary

%backupcmd% "%USERPROFILE%\Application Data\microsoft\proof" "%drive%\Outlookbackup\Proof"

echo ### Backing up Outlook Autocomplete

%backupcmd% "%USERPROFILE%\Application Data\microsoft\outlook" "%drive%\Outlookbackup\Outlook"

echo ### Backing up Outlook Signatures

%backupcmd% "%USERPROFILE%\Application Data\microsoft\signatures" "%drive%\Outlookbackup\Signatures"

echo ### Backing up Internet Explorer Favourites

%backupcmd% "%USERPROFILE%\Favorites" "%drive%\Favorites"

REM ### You now have the final countdown stuck in your head

echo ### Backing up My Documents

%backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Documents"

echo ### Backing up Desktop

%backupcmd% "%USERPROFILE%\Desktop" "%drive%\Desktop"

echo ### Listing all programs installed on the computer

regedit /e %temp%\regexport.txt "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall"

find "DisplayName" %temp%\regexport.txt >%temp%\regprogs.txt

for /f "tokens=2 delims==" %%a in (%temp%\regprogs.txt) do echo %%~a >>"%drive%\installedprogs.txt"

del %temp%\regexport.txt

del %temp%\regprogs.txt

goto endbackup

:2

echo ### Restoring files...

echo ### Restoring files from %drive%

echo ### Restoring Custom Dictionary

%backupcmd% "%drive%\Outlookbackup\Proof" "%USERPROFILE%\Application Data\microsoft\proof"

echo ### Restoring Outlook Autocomplete

%backupcmd% "%drive%\Outlookbackup\Outlook" "%USERPROFILE%\Application Data\microsoft\outlook"

REM ### You have lost the game

echo ### Restoring Outlook Signatures

%backupcmd% "%drive%\Outlookbackup\Signatures" "%USERPROFILE%\Application Data\microsoft\signatures"

echo ### Restoring Internet Explorer Favourites

%backupcmd% "%drive%\Favorites" "%USERPROFILE%\Favorites"

echo ### Restoring My Documents

%backupcmd% "%drive%\My Documents" "%USERPROFILE%\My Documents"

echo ### Restoring Desktop

%backupcmd% "%drive%\Desktop" "%USERPROFILE%\Desktop"

goto endrestore

:3	

call:ReadDesktopRegistry Wallpaper

call:ReadDesktopRegistry SCRNSAVE.EXE

call:ReadDesktopRegistry WallpaperStyle

call:ReadDesktopRegistry ScreenSaveActive

call:ReadDesktopRegistry ScreenSaveTimeOut

xcopy /c /h /i /r /y /k /v %Wallpaper% %drive%\Wallpaper\

xcopy /c /h /i /r /y /k /v %SCRNSAVE.EXE% %drive%\Wallpaper\Screensaver\

goto endwallpaper

:ReadDesktopRegistry

FOR /F "delims=" %%A in ('reg query "HKCU\Control Panel\Desktop" /v "%~1"') DO (SET RegRead=%%A)

FOR /F "tokens=3" %%A IN ("%RegRead%" ) DO (SET "%~1=%%A" )

SET RegRead=""

goto:eof

:4

echo ### Backing up Firefox Profile

%backupcmd% "%USERPROFILE%\Application Data\Mozilla\Firefox\Profiles" "%drive%\Firefox\Profiles"

goto endbackup

:5

echo ### Restoring Firefox Profile

%backupcmd% "%drive%\Firefox\Profiles" "%USERPROFILE%\Application Data\Mozilla\Firefox\Profiles"

goto endrestore

:ver_2000

echo You have Windows 2000

echo.

:Run Windows 2000 specific commands here.

goto warnthenexit

:ver_nt

echo You have Windows NT

:Run Windows NT specific commands here.

goto warnthenexit

:ver_7

echo You have Windows 7

:Run Windows 7 specific commands here.

goto warnthenexit

:ver_2008

echo You have Windows Server 2008

:Run Windows Server 2008 specific commands here.

goto warnthenexit

:ver_vista

echo You have Windows Vista

:Run Windows Vista specific commands here.

goto warnthenexit

:ver_2003

echo You have Windows Server 2003

:Run Windows Server 2003 specific commands here.

goto warnthenexit

:warnthenexit

echo.

echo I'm Sorry, this script is not compatible with this OS.

pause > nul

goto 6

:endbackup

cls

echo Backup Complete!

echo.

echo.

echo.

echo Thank you for using the IT Department's Profile autobackup facility

echo.

echo Your Backup is located in %drive%

echo Please press any key to go back to menu

pause>nul

goto start

:endrestore

cls

echo Restore Complete!

echo.

echo.

echo.

echo Thank you for using the IT Department's Profile autobackup facility

echo.

echo Your Backup has been restored from %drive%

echo Please press any key to go back to menu

pause>nul

goto start

:endwallpaper

cls

echo Wallpaper backed up to %drive%Wallpaper\

echo.

echo Screensaver backed up to %drive%Wallpaper\Screensaver\ 

echo.

echo Thank you for using the IT Department's Profile autobackup facility

echo.

echo Please press any key to go back to menu

pause>nul

goto start

:6

cls

echo Thank you for using the IT Department's Profile autobackup facility

echo.

echo No options have been actioned.

echo.

echo Please press any key to exit

pause>nul

exit
 
Back
Top