• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

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.
0
Lafflin
Asked:
Lafflin
  • 4
  • 4
  • 2
1 Solution
 
Steve KnightIT ConsultancyCommented:
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"
)
0
 
Steve KnightIT ConsultancyCommented:

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
0
 
oBdACommented:
Why not offer the user a menu in which he only has to pick an existing profile to backup?
@echo off
setlocal enabledelayedexpansion
set ProfileRoot=C:\Documents and settings
set TargetRoot=C:\Temp
set /a ProfileCount = 0
for /d %%a in ("%ProfileRoot%\*.*") do (
  set /a ProfileCount += 1
  set Profile[!ProfileCount!]=%%~nxa
)
:Loop
cls
echo Please pick the profile to backup.
echo This menu will be repeated until you decide to leave.
for /l %%i in (1, 1, %ProfileCount%) do (
  echo [%%i]	!Profile[%%i]!
)
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.
) else (
  echo Successfully backed up profile: !Profile[%Choice%]!
)
echo=Hit any key to continue . . .
pause >NUL
goto :Loop

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LafflinAuthor Commented:
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.
0
 
LafflinAuthor Commented:
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.
0
 
oBdACommented:
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

0
 
LafflinAuthor Commented:
I was able to see what your was doing just fine, and it is perfect. I was replying to the first response.
0
 
Steve KnightIT ConsultancyCommented:
Silly me for giving you what you asked for!!
0
 
LafflinAuthor Commented:
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.
0
 
Steve KnightIT ConsultancyCommented:
you just had to run it as is.... And i explained each command!

Anyway never mind.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now