1.

Solve : Assistance creating backup script?

Answer»

I'm trying to create a batch script to enumerate user profiles on both Windows XP and Windows 7 workstations. The purpose of the script is to:
1.) First the script will determine the operating system and run the appropriate commands for the different user profile file structures.
2.) Then the script will delete any user folders older than 45 days
2.) Copy some folders and files from each remaining user profile to their respective network shares (their H: drives - in this case \\cfs002\%Username%). The files/folders would be dumped into a folder named "backup" on their H: drives. The script thus far is enumerating and reading the contents of the user profiles just fine, but I'm having difficulty with the code to move the folders to each user's respective network drive.
During Enumeration the variable %%K takes on the C:\Users\Username value. I want to take just the username portion of that path and have that value PUT into destination directory (incorrectly coded %%K). Currently it is putting 'C:\Users\Username' in that variable, but I just want Username to be passed to it.
ie. (Windows 7 SECTION) C:\Users\Username\Favorites folder needs to get COPIED to \\cfs002\Username\Backup\
The script is currently trying to copy it to \\cfs002\C:\Users\Username\Backup (which obviously won't work). I need to strip out the "C:\Users\" part of it.

Anyone up for the challenge?

Code: [Select]:: Delete user profiles older than 30 days
:: Enumerate Each User Profile on the PC used within the last 30 days
:: Copy Desktop files to the user's H: drive
:: Copy IE Favorites to the user's H: drive
:: Copy anything locally in the My Documents folder to the user's H: drive
:: Copy any locally stored .pab and .pst files to the user's H: drive
:: Copy Outlook SIGNATURE file and settings to the user's H: drive
:: Copy Outlook settings file to the user's H: drive

REM Check Windows Version
ver | findstr /i "5\.0\." > nul
IF %ERRORLEVEL% EQU 0 GOTO ver_2kxp
ver | findstr /i "5\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_2kxp
ver | findstr /i "6\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_Vista7
ver | findstr /i "6\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_Vista7
goto warn_and_exit

:: This Section Is For Windows 2000 and XP Cleanup
:: Delete user accounts older than 45 days
:: delprof /q /I /d:45
cls

:ver_2kxp

:: variables

:: The following 7 commands enumerate the existing Windows 2000/XP user profiles and copies the data to the user's H: drive (in this case \\CFS002\%USERNAME%)

for /d %%K in ("C:\Documents and Settings\*") do (
Xcopy /e /i /y "%%~fK\Favorites\*" "\\cfs002\%%~fK\Backup"
Xcopy /e /i /y "%%~fK\My Documents\*" "\\cfs002\%%~fK\Backup"
Xcopy /e /i /y "%%~fK\Desktop\*" "\\cfs002\users\%%~fK\Backup"
Xcopy /y "%%~fK\Local Settings\Application Data\Microsoft\Outlook\*.pst" "\\cfs002\%%~fK\Backup\Outlook Files"
Xcopy /y "%%~fK\Local Settings\Application Data\Microsoft\Outlook\*.pab" "\\cfs002\%%~fK\Backup\Outlook Files"
Xcopy /y "%%~fK\Local Settings\Application Data\Microsoft\Outlook\*.nk2" "\\cfs002\%%~fK\Backup\Outlook Files"
Xcopy /y "%%~fK\Local Settings\Application Data\Microsoft\Outlook\*.ost" "\\cfs002\%%~fK\Backup\Outlook Files"
)
pause

:ver_Vista7

:: variables

:: The following 7 commands enumerate the existing Windows Vista/7 user profiles and copy the data to the user's H: drive (in this case \\CFS002\%USERNAME%)

for /d %%K in ("C:\Users\*") do (
Xcopy /e /i /k /y "%%~fK\Favorites\*" "\\cfs002\%%~fK\Favorites\"
Xcopy /e /i /y "%%~fK\My Documents\*" "\\cfs002\%%~fK\Backup\"
Xcopy /e /i /y "%%~fK\Desktop\*" "\\cfs002\%%~fK\Backup\"
Xcopy /y "%%~fK\AppData\Local\Microsoft\Outlook\*.pst" "\\cfs002\%%~fK\Backup\Outlook Files"
Xcopy /y "%%~fK\AppData\Local\Microsoft\Outlook\*.pab" "\\cfs002\%%~fK\Backup\Outlook Files"
Xcopy /y "%%~fK\AppData\Local\Microsoft\Outlook\*.nk2" "\\cfs002\%%~fK\Backup\Outlook Files"
Xcopy /y "%%~fK\AppData\Local\Microsoft\Outlook\*.ost" "\\cfs002\%%~fK\Backup\Outlook Files"
)

pause
Try using the file name modifier with the %%K variable. (ie: %%~nK)

The logic flow seems illogical. When :ver_2kxp completes and pauses, it will fall into :ver_Vista7. Either use a goto to skip over the the block of code or use a goto :eof to terminate the script.

[string](0..9|%{[char][int](32+("051073068069087073078068069082").substring(($_*3),3))})-replace "\s{1}\b"



Discussion

No Comment Found