Solved Batch code to select profile name for backup

Hewlett-packard Hp pavilion dv9000 lapto...
May 22, 2012 at 14:23:02
Specs: XP Vista Win7, P4 3Ghz 3G ram
I have a batch file that backs up several things and was going to include user profiles. I need a script/snippet to scroll through each profile name to determine which to back up, and if possible the size of the profile folder (helps determine if it has data in it)

I'd prefer to copy only select folder as opposed to deleting unwanted folder simply for time sake.
My batch already can determine between XP Vista and 7.

(Yes I know there are backup programs, but I want this no install simple method)

I actually prefer to select/list from the folder names under User (Vista & 7) or Documents and Settings (XP)

ie
If /f tokens = ??? (%username%) etc
Do you want to backup %username%?

I really appreciate your time.

The Computer Dr
Chris Robertson


See More: Batch code to select profile name for backup

Report •


✔ Best Answer
May 25, 2012 at 23:45:58
:: =====  script starts here  ===============
:: 
:: chooser.bat  Sat 26-05-2012  9:53:55.62
@echo off & setLocal enableDELAYedeXpansioN

for /f "tokens=1* delims=[]" %%a in ('dir/b "C:\Documents and Settings" ^| find /n /v "\\\"') do (
echo %%a %%b
set %%a=%%b
)

set/p C= which ones?

for %%a in (!C!) do (
call :sub1 %%a
)
goto :eof

:sub1
echo backup !%1!
goto :eof
::======  script ends here  =================


=====================================
Life is too important to be taken seriously.

M2



#1
May 22, 2012 at 23:57:51
if %USERNAME%==Administrator echo backup?

etc


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
May 23, 2012 at 04:45:19
I'm trying to scroll through/list all the users/profiles on the computer. All users and Administrators I usually skip.

The Computer Dr
Chris Robertson


Report •

#3
May 23, 2012 at 22:59:19
:: ===== script starts here ===============
:: select user from a list
:: users.bat Thu 24-05-2012 12:39:40.69
@echo off & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in ('dir/b "C:\Documents and Settings" ^| find /n /v "\\\"') do (
echo %%a
)

set /p C= choose
echo !C!

goto :eof

::====== script ends here =================


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
May 24, 2012 at 14:09:55
That snippet works great but one at a time, but I will have to goto the beginning to copy another user and have some sort of exit selection when I am done. How do I return to the start to select another user, and have an option to exit?


This is what I worked out. It works great to copy ALL users, but I can't skip a user. The direct opposite problem. I can't to a set /p in this either since it breaks the loop ;


for /f "delims=" %%a in ('dir "%ProfileFolder%\." /b') do (

cls
echo.
echo.
echo.
echo Backing up %%a
pause

echo Desktop
xcopy "%ProfileFolder%\%%a\Desktop\*.*" "%destination%\%FOLDER%\%username%\Desktop\" /e /c /h /k /y
pause
echo favorites
xcopy "%ProfileFolder%\%%a\Favorites\*.*" "%destination%\%FOLDER%\%pause

)

The Computer Dr
Chris Robertson


Report •

#5
May 24, 2012 at 14:24:57
I did manage to edit your snippet a bit and found a way to exit out of the loop by just pressing enter.

@echo off & setLocal enableDELAYedeXpansioN
:start
cls
for /f "tokens=* delims= " %%a in ('dir/b "C:\Documents and Settings" ^| find /n /v "\\\"') do (
echo %%a
)
set /p C= Choose a profile to backup. (Press enter to end) -
echo !C!
set exit=%errorlevel%
if %exit% EQU 1 goto exitusers
::copy process
xcopy %sourcepath%%\%a\*.* %destination%%\%a
goto start
:exitusers
echo exiting user selection
pause

The Computer Dr
Chris Robertson


Report •

#6
May 25, 2012 at 09:15:30
Any ideas on the best method for asking/polling each username and skipping some predefined names? (Administrator, All users,Default user, net user, local service, network service, etc)

The Computer Dr
Chris Robertson


Report •

#7
May 25, 2012 at 23:45:58
✔ Best Answer
:: =====  script starts here  ===============
:: 
:: chooser.bat  Sat 26-05-2012  9:53:55.62
@echo off & setLocal enableDELAYedeXpansioN

for /f "tokens=1* delims=[]" %%a in ('dir/b "C:\Documents and Settings" ^| find /n /v "\\\"') do (
echo %%a %%b
set %%a=%%b
)

set/p C= which ones?

for %%a in (!C!) do (
call :sub1 %%a
)
goto :eof

:sub1
echo backup !%1!
goto :eof
::======  script ends here  =================


=====================================
Life is too important to be taken seriously.

M2


Report •

#8
May 27, 2012 at 07:25:23
ok, I've encountered a small wrinkle. This code works fine until I put it in my script then the !%1! goes stupid.
Do I need to declare a variable or something. I don't understand how the name got into 1 anyway.
I can post my script if necessary.

The Computer Dr
Chris Robertson


Report •

#9
May 27, 2012 at 09:25:43
"I can post my script if necessary."

yep


=====================================
Life is too important to be taken seriously.

M2


Report •

#10
May 27, 2012 at 09:54:54
You asked for it. The key program is an external program. I know it is a poor, but best I can do for now.

@echo off
color 0A

:::===============================
SET "Title=Backup Utility"
TITLE %title%

:::=============================
:Date
::get current date

FOR /F "tokens=1-6 delims=.-/: " %%A IN ("%DATE% %TIME%") DO (
FOR /F "tokens=2-4 skip=1 delims=(.-/)" %%G IN ('VER^|DATE') DO (
SET YY=%%D
SET MM=%%B
SET DD=%%C
SET HH=%%E
SET SS=%%F
SET II=%%A
)
)
set today=%YY%-%MM%-%DD%_%SS%
::show date
::ECHO %YY%-%MM%-%DD%_%SS%
::echo %today%
::PAUSE

::=======================
:source
::define the name of the destination folder
echo.
echo.
set /p usersname=Enter the users name :
cls
echo This will copy drivers from backup locations
echo.
echo Select C: if your backing up locally
echo If the drive is a slave it will be E: or F: or similar

setlocal
set ProfileFolder=%ALLUSERSPROFILE:\All Users=%
set mypath=X:
set drive=C:
::This will select the source drive
SET /P drive=Select letter of drive to backup (Blank is C:) :

::If "%drive%"=="C:" echo %drive%
::pause
::If "%drive%"=="C:" set mypath=%ProfileFolder%

::pause


:bits
::===============================
::determine if it is 32 or 64 bit
::IF DEFINED ProgramFiles(x86) GOTO IS_x64
::set bits=32
::IF DEFINED ProgramFiles(x86) SET bits=64

set bits=64
IF %PROCESSOR_ARCHITECTURE% == x86 (
IF NOT DEFINED PROCESSOR_ARCHITEW6432 Set bits=32
)
Echo Operating System is %bits% bit
pause


:: Find windows Version
::===========================
:Version
SET Version=Unknown
VER | FINDSTR /IL "5.0" > NUL
IF %ERRORLEVEL% EQU 0 SET Version=2000
IF %ERRORLEVEL% EQU 0 SET type=1
:IF %ERRORLEVEL% EQU 0 goto 2000ver

VER | FINDSTR /IL "5.1." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=XP
IF %ERRORLEVEL% EQU 0 SET type=2
:IF %ERRORLEVEL% EQU 0 goto xpver

VER | FINDSTR /IL "5.2." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=2003
IF %ERRORLEVEL% EQU 0 SET type=3
:IF %ERRORLEVEL% EQU 0 goto 2003ver

VER | FINDSTR /IL "6.0." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=Vista
IF %ERRORLEVEL% EQU 0 SET type=4
:IF %ERRORLEVEL% EQU 0 goto vistaver

VER | FINDSTR /IL "6.1." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=Windows7
IF %ERRORLEVEL% EQU 0 SET type=5
:IF %ERRORLEVEL% EQU 0 goto win7ver

:echo %Version%
:echo %type%
:pause

:winver
IF %type% EQU 1 GOTO 2000VER
IF %type% EQU 2 GOTO XPVER
IF %type% EQU 3 GOTO 2003VER

:runasadmin
::==========================

color 0C
echo.
echo.
echo.
set /P c=Did you run as administrator? Y/N?....
echo Remember to disable or exit your antivirus!
if /I "%c%" EQU "N" goto tryagain

IF %type% EQU 4 goto vistaver
IF %type% EQU 5 goto win7ver


:echo %Version%
:pause

:xpver
::============================
::XP XP XP XP XP XP XP XP XP
cls
:moved this section
::set drive=C:
::SET /P drive=Select letter of drive to backup (Blank is C:) :

::Determine profilefolder
if "%drive%" neq "C:" set ProfileFolder="%drive%\Document and Settings\"
echo Profiles are stored at %ProfileFolder%
::pause

echo.
echo.
echo This computer is running Windows XP
echo.
pause

::echo This will copy drivers from backup locations
::echo.
::echo Select C: if your backing up locally
::echo If the drive is a slave it will be E: or F: or similar

::set drive=C:
::REM -get input
::SET /P drive=Select letter of drive to backup (Blank is C:) :

cls
echo.
echo.
echo.
::echo You selected %drive%
::now combine with text


::echo Set destination path
set drivers=%drive%\windows\system32\drivers
set inf=%drive%\windows\inf
set destination=%~dp0%today%-%usersname%\
md "%destination%"

:drivers
cls
echo.
echo.
echo.
set /P c=Would you like to backup Drivers? Y/N?....
if /I "%c%" EQU "N" goto documents

::echo Drive %drive% will be tested
xcopy "%drivers%" "%destination%drivers\" /s /y
xcopy "%inf%" "%destination%inf\" /s /y

:::--------Backup docs
setlocal EnableDelayedExpansion

:documents
cls
pause
echo.
echo.
echo.
set /P w=Would you like to backup Documents? Y/N?....
if /I "%w%" EQU "N" goto keys
::---------------------------

for /f "tokens=1* delims=[]" %%a in ('dir/b "C:\Documents and Settings" ^| find /n /v "\\\"') do (
echo %%a %%b
set %%a=%%b
)

set/p C= which ones?

for %%a in (!C!) do (
call :sub1 %%a
)
goto :eof

:sub1
cls
echo backup !%1!
pause


::-----------------------------
::removed this section
::for /f "delims=" %%a in ('dir "%ProfileFolder%\." /b') do (
::cls
::echo Backing up %%a
::pause

echo Desktop
xcopy "%ProfileFolder%\%%a\Desktop\*.*" "%destination%\%FOLDER%\%username%\Desktop\" /e /c /h /k /y
pause
echo favorites
xcopy "%ProfileFolder%\%%a\Favorites\*.*" "%destination%\%FOLDER%\%username%\Favorites\" /e /c /h /k /y
rem xcopy "%ProfileFolder%\%%a\My Documents\*.*" "%destination%\%FOLDER%\%username%\My Documents\" /e /c /h /k /y
xcopy "%ProfileFolder%\%%a\Application Data\Microsoft\Outlook\*.*" "%destination%\%FOLDER%\%username%\Outlook2\" /e /c /h /k /y
rem xcopy "%ProfileFolder%\%%a\Local Settings\Application Data\Microsoft\Outlook\*.*" "%destination%\%FOLDER%\%username%\Outlook\" /e /c /h /k /y
xcopy "%ProfileFolder%\%%a\Application Data\Identities\*.*" "D:\Backup\001\%FOLDER%\%username%\oe\" /e /c /h /k /y
::)

:
pause
goto keys

::================
:vistaver
::Vista Vista Vista Vista Vista
cls
echo.
echo.
echo. This Vista version has not been tested yet
echo This computer is running Windows Vista
echo.
::Determine profilefolder
if "%drive%" neq "C:" set ProfileFolder="%drive%\Document and Settings\"
echo Profiles are stored at %ProfileFolder%
cls
echo.
echo.
echo.
::echo You selected %drive%
::now combine with text

::echo Set destination path
set drivers=%drive%\windows\system32\drivers
set inf=%drive%\windows\inf
set destination=%~dp0%today%-%COMPUTERNAME%\
md "%destination%"

:drivers
cls
echo.
echo.
echo.
set /P c=Would you like to backup Drivers? Y/N?....
if /I "%c%" EQU "N" goto documents

::echo Drive %drive% will be tested
xcopy "%drivers%" "%destination%drivers\" /s /y
xcopy "%inf%" "%destination%inf\" /s /y

:::--------Backup docs

:documents
cls
echo.
echo.
echo.

set /P c=Would you like to backup Documents? Y/N?....
if /I "%c%" EQU "N" goto keys


setlocal

set ProfileFolder=%ALLUSERSPROFILE:\All Users=%

for /f "delims=" %%a in ('dir "%ProfileFolder%\." /b') do (

cls
echo.
echo.
echo.
echo Backing up %username%
echo Desktop
xcopy "%ProfileFolder%\%%a\Desktop\*.*" "%destination%\%FOLDER%\%username%\Desktop\" /e /c /h /k /y
pause
echo favorites
xcopy "%ProfileFolder%\%%a\Favorites\*.*" "%destination%\%FOLDER%\%username%\Favorites\" /e /c /h /k /y
xcopy "%ProfileFolder%\%%a\My Documents\*.*" "%destination%\%FOLDER%\%username%\My Documents\" /e /c /h /k /y
xcopy "%ProfileFolder%\%%a\Application Data\Microsoft\Outlook\*.*" "%destination%\%FOLDER%\%username%\Outlook2\" /e /c /h /k /y
xcopy "%ProfileFolder%\%%a\Local Settings\Application Data\Microsoft\Outlook\*.*" "%destination%\%FOLDER%\%username%\Outlook\" /e /c /h /k /y
xcopy "%ProfileFolder%\%%a\Application Data\Identities\*.*" "D:\Backup\001\%FOLDER%\%username%\oe\" /e /c /h /k /y

pause

)

:
pause
goto keys

::===============
:win7ver
::Win7 Win7 Win7 Win7
cls
echo.
echo.
echo. This Windows 7 version has not been tested yet
echo This computer is running Windows Vista
echo.
::Determine profilefolder
echo %drive%
if "%drive%" neq "C:" set ProfileFolder="%drive%\Document and Settings\"
echo Profiles are stored at %ProfileFolder%

cls
echo.
echo.
echo.
::echo You selected %drive%
::now combine with text

::echo Set destination path
set drivers=%drive%\windows\system32\drivers
set inf=%drive%\windows\inf
set destination=%~dp0%today%-%COMPUTERNAME%\
md "%destination%"

:drivers
cls
echo.
echo.
echo.
set /P c=Would you like to backup Drivers? Y/N?....
if /I "%c%" EQU "N" goto documents

::echo Drive %drive% will be tested
xcopy "%drivers%" "%destination%drivers\" /s /y
xcopy "%inf%" "%destination%inf\" /s /y

:::--------Backup docs

:documents
cls
echo.
echo.
echo.

set /P c=Would you like to backup Documents? Y/N?....
if /I "%c%" EQU "N" goto keys


setlocal
set userpath=%drive%\Users
::echo User files are at %userpath%
::pause
::set ProfileFolder=%ALLUSERSPROFILE:\All Users=%
::set ProfileFolder=%HOMEPATH:\All Users=%

set ProfileFolder=%userpath:\All Users=%


::echo profilefolder is = %profilefolder%
::echo alluserprifle folder= %allusersprofile%
::echo homepath = %homepath%

echo.
echo.
for /f "delims=" %%a in ('dir "%ProfileFolder%\." /b') do (

cls
echo.
echo.
echo.
::echo Backing up %username%
set /P d=Would you like to backup %username%? Y/N?....
if /I "%d%" EQU "N" echo skip this user


xcopy "%ProfileFolder%\%%a\Contacts\*.*" "%destination%\%FOLDER%\%username%\Desktop\" /e /c /h /k /y
xcopy "%ProfileFolder%\%%a\Desktop\*.*" "%destination%\%FOLDER%\%username%\Desktop\" /e /c /h /k /y
xcopy "%ProfileFolder%\%%a\Favorites\*.*" "%destination%\%FOLDER%\%username%\Favorites\" /e /c /h /k /y
xcopy "%ProfileFolder%\%%a\Documents\*.*" "%destination%\%FOLDER%\%username%\My Documents\" /e /c /h /k /y
::xcopy "%ProfileFolder%\%%a\Application Data\Microsoft\Outlook\*.*" "%destination%\%FOLDER%\%username%\Outlook2\" /e /c /h /k /y
::xcopy "%ProfileFolder%\%%a\Local Settings\Application Data\Microsoft\Outlook\*.*" "%destination%\%FOLDER%\%username%\Outlook\" /e /c /h /k /y
::xcopy "%ProfileFolder%\%%a\Application Data\Identities\*.*" "D:\Backup\001\%FOLDER%\%username%\oe\" /e /c /h /k /y
pause


)

pause
goto keys


::================
:2000ver
echo.
echo.
echo.
echo There is no branding for 2000
pause
goto settings
::================

::================
:2003ver
echo.
echo.
echo.
echo There is no branding for 2003
pause
goto settings


::===============
:UNKNOWN
echo.
echo.
echo.
echo This version of windows is unknown
goto exit


::===================
:keys
::gets product keys using productkeyexplorer.exe on THIS DRIVE ONLY
cls
::echo %destination%
echo.
echo.
echo.
set /P c=Would you like to backup Program keys? Y/N?....
if /I "%c%" EQU "N" goto programs


start /wait ProductKeyExplorer.exe /remote 127.0.0.1 /f "%destination%\keys.txt" /exit
cls
echo.
echo.
echo.
echo A windows should open with the program keys. If not manually run the program.
echo Please close the window and the program will continue


cd %destination%\
start /wait keys.txt
::echo Keys were saved to %destination%

::pause

rd "%~dp0%Data" /s /Q

::=========================
:Programs
::Make a text file with all the installed programs
cls

echo.
echo.
echo This utility will make a text file of all programs installed
echo This utility is NOT complete.
set /P c=Would you like to backup a list of the installed programs? Y/N?....
if /I "%c%" EQU "N" goto Exit
pause

type nul>.\apps.txt
type nul>.\sorted.txt

for /f "tokens=2,*" %%a in ('Reg Query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /S^|find " DisplayName"') do (
echo "%%b"|findstr /B /V /C:"Hotfix"|findstr /B /V /C:"Security Update"|findstr /B /V /C:"Update">>.\apps.txt
)
sort .\apps.txt /o .\sorted.txt
::moves apps.txt and sorted.txt to the destination directory

:move
move sorted.txt "%destination%"
move apps.txt "%destination%"

echo. Program is Complete!!
pause
goto exit


:tryagain
0A
cls
echo.
echo.
echo.
echo Please rightclick and select Run as Administrator
pause
goto exit

:exit

The Computer Dr
Chris Robertson


Report •

#11
May 27, 2012 at 16:29:27
It may be a bit much to ask a volunteer to plow through that 500 line mess.


=====================================
Life is too important to be taken seriously.

M2


Report •

#12
May 27, 2012 at 20:46:24
ya, I didn't expect that. I'm just wondering why the original code works, and when I paste it in my full code it doesn't. Do I need to declare the variables at the begining of this section. I don't know enough to understand why it wouldn't work.

The Computer Dr
Chris Robertson


Report •


Ask Question