Link to home
Start Free TrialLog in
Avatar of Lafflin
Lafflin

asked on

Batch scripting how to use variable for user account directory.

I'm creating a script which will go onto a USB disk that will be used to recover files from an unbootable machine.
I would like to be able to copy all the files in the C:\documents and settings\%USERPROFILE% using xcopy.
I have a user account variable which is populated by what the user has typed. We'll call it %NAME%
Example: the user account is charlie.brown. I want the user to be able to type "charlie" and have the xcopy command copy anything in the "C:\documents and settings\chalie.brown\...." or  "C:\documents and settings\chalie.smith\...." etc
Right now I am using:
xcopy "C:\documents and settings\*%NAME%*\*.*"  (finds nothing)
xcopy "C:\documents and settings\*%NAME%*          (finds files containing the word charlie)

To be clear I want the command to find directories containing "chalie" and copy everythign in those directories. I can't seem to fogure out how to do this.
If you're wondering why I would need this, it's to allow end users who don't know their account names the ability to backup just their own files before reimaging (which I have already automated on a seperate USB drive)

Thanks in advance.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Have a look at this.  It will ask for a user name, and default to the logged on user.  If a name is entered it will look for that under the profile directory, as it stands it will look for *name* but you can change that... or let the user type in the *s if you wish.
Have added example xcopy line to create a dir and copy anything that has changed to the d:\name dir.

Steve

@echo off
set profilesroot=%userprofile%\..
set name=%username%
set /p name=Enter name [default %name%]:
for /f "delims=" %%D in ('dir /b /ad %profilesroot%\*%name%*') do (
  echo You can now work on subdir %%D which is full path "%%~fD"
  "md d:\%%D" 2>NUL
  xcopy /s /d "%%~fD" "D:\%%D"
)

To explain.... for /? gives info but for /f takes the output of a command, in this case a bare directory listing of the names under the %profilesroot% folder that match *name* -- do

dir /b /ad %profilesroot%\*charlie*

from cmd.exe prompt to check it gives the right results

Each line is passed to the DO loop as the variable "%%D".  The bit %%~fD gets the full path of the %%D bit which in this case is just "charlie.whatever" directory name.

You can use %%D and %%~fD etc. in your xcopy command as needed.

set /? also explains more.

Steve
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lafflin
Lafflin

ASKER

Hmm...
That's great but I think you've overshot the target. My script isn't run from within windows, but from WinPE.
My question really is as simple as how do I copy any folder containing the variable %name% within it's name as opposed to only being able to copy the named %name%.

I'm trying to see what your script is doing, but it's not really jumping out at me.
Avatar of Lafflin

ASKER

Hmm.....
Well to answer your question oBdA....cause I'm simply not that advanced. That's quite fancy, I appreciate it.

Steve, thanks as well. I begun to see where you were going with your post as well, but I think I'm going to go with oBdA'a menu.

Above and beyond the call sir...much thanks.
That should run on PE as well; it's plain batch. You can test it without problems, there's nothing really happening because the xcopy line is ECHOoed out.
The script first reads the all the profiles under the specified root, then creates a simple menu.
Here's a new version, correcting a slight error, and indicating (for the users with short term memory loss) which profiles have already been backed up successfully.
There should be a single TAB character in line 5 after "set TAB="; I'm not sure whether that will be preserved when posting and copying and pasting.
@echo off
setlocal enabledelayedexpansion
set ProfileRoot=C:\Documents and settings
set TargetRoot=C:\Temp
REM *** There should be one TAB character after "set TAB=" in the following line:
set TAB=	
set /a ProfileCount = 0
for /d %%a in ("%ProfileRoot%\*.*") do (
  set /a ProfileCount += 1
  set Profile[!ProfileCount!]=%%~nxa
  set Done[!ProfileCount!]=-
)
:Loop
cls
echo Please pick the profile to backup.
echo This menu will be repeated until you decide to leave.
echo ID%TAB%Copied%TAB%Name
for /l %%i in (1, 1, %ProfileCount%) do (
  echo [%%i]%TAB%!Done[%%i]!%TAB%!Profile[%%i]!
)
set Choice=
set /p Choice=Enter 1 .. %ProfileCount%, or X to exit, followed by ^<Return^>: 
if "!Choice!"=="" goto Loop
if /i "!Choice!"=="X" (
  echo Bye.
  goto :eof
)
if "!Choice!" LSS "1" goto Loop
if "!Choice!" GTR "%ProfileCount%" goto Loop
ECHO xcopy "%ProfileRoot%\!Profile[%Choice%]!" "%TargetRoot%\!Profile[%Choice%]!"
if errorlevel 1 (
  echo There were errors during the copy.
  goto Continue
)
echo Successfully backed up profile: !Profile[%Choice%]!
set Done[%Choice%]=X
:Continue
echo=Hit any key to continue . . .
pause >NUL
goto :Loop

Open in new window

Avatar of Lafflin

ASKER

I was able to see what your was doing just fine, and it is perfect. I was replying to the first response.
Silly me for giving you what you asked for!!
Avatar of Lafflin

ASKER

Forgive me dragon, I'm a novice scripter at best, I simply wasn't able to read what you had done.
I am just begining to see it as I am not familiar with the batch syntax.
you just had to run it as is.... And i explained each command!

Anyway never mind.