%username% returns b_smith rather than b.smith (the actual username)

I have inherited an interesting setup utilizing windows address book to share contact fax numbers and email addresses. MS Server 2003 running Terminal Services for 25 users, and all need access to a common address book using Outlook Express.  The way that the previous IT personnel set it up is the following: One user has the "master" address book, and all other users get a copy of that address book when they log in via a batch file run in the startup folder.  The batch file simply copies the master, then replaces the default address book on the users account with the master wab.  The problem is that the .bat works great with account names that do not have a "." in them, but not with those that do.  For example, the default .wab for account b.smith is actually called b_smith.wab.  Therefore, the .bat file simply adds a new .wab to the folder called b.smith, but the system is still pointed at b_smith by default, so this user does not see the correct .wab.

I have found where to edit the registry to change the name of the default address book, but why is the "." being replaced with a "_" to begin with?  Any ideas on how to fix this for now, and a better way to share contacts in the future?

I have attached the .bat file that runs at startup.

Thanks!

cls

D:
cd wab
copy amy2-27.wab "C:\Documents and Settings\%username%\Application Data\Microsoft\Address Book\"
C:
cd "C:\Documents and Settings\%username%\Application Data\Microsoft\Address Book\"
del %username%.wab
rename amy2-27.wab

Open in new window

CreeksidetechAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
sirbountyConnect With a Mentor Commented:
I'd say its a difference between samaccountname property and the name property?

You could possibly alter your batch to account for that...
cls

D:
cd wab
set newusername=%username:_=.%
copy amy2-27.wab "C:\Documents and Settings\%newusername%\Application Data\Microsoft\Address Book\"
C:
cd "C:\Documents and Settings\%newusername%\Application Data\Microsoft\Address Book\"
del %newusername%.wab
rename amy2-27.wab

Open in new window

0
 
Mark DamenERP System ManagerCommented:
I'd say that the script is missing code on line 9.  It calls to rename a file, but doesnt tell the system what to rename it to.  therefore, file never appears under the correct username.
0
 
CreeksidetechAuthor Commented:
Sorry - I got too quick with the copy/paste.

Line 9 should be the following:
rename amy2-27.wab %username%.wab

Open in new window

0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Steve KnightIT ConsultancyCommented:
If there is only one WAB in that dir it is easy enough to read the name of it and use that for the rename, or can rea from the registry if needed?
0
 
eric27Commented:
Maybe this will help:



@echo off
rem set default new filename for wab-file
set WABFilename=%username%.wab

rem find DOT in username. if found then replace with minus or leave new variabel WABUSERNAME empty
for /F "delims=. tokens=1,2*" %%I in ("%username%") do set WABUserName=%%I-%%J

rem if WABUSERNAME was set, there was a DOT and so replace the new filename's name.
if /i not %WABUserName%=="" set WABFileName=%WABUserName%.wab

rem copy and overwrite without question an existing addressbook
copy /y d:\wab\amy2-27.wab "%UserProfile%\Application Data\Microsoft\Address Book\%WABFileName%"

Open in new window

0
 
Rant32Commented:
If you can replace the logon script with a VB script, you can use this.

It reads the actual WAB path from the HKCU registry and tries to replace the file. It logs events in the event viewer, just delete those lines if you don't want that.

Regards,
Rant32

Set oShell=CreateObject("WScript.Shell")
Set oFSO=CreateObject("Scripting.FileSystemObject")

SourceWab = "D:\wab\amy2-27.wab"

On Error Resume Next
TargetWab = oShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Wab\WAB4\Wab File Name\")
If Err.Number = 0 And TargetWab <> "" Then
    oFSO.CopyFile SourceWab, TargetWab, True
    If Err.Number = 0 Then
        oShell.LogEvent 4, "Replaced Windows Address Book:" & vbNewline & TargetWab
    Else
        oShell.LogEvent 2, "Could not replace Windows Address Book:" & vbNewline & TargetWab
    End If
End If

Open in new window

0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Your original script will function just fine no need to change to VB.

Simply put the UserName in quotes as such "%UserName%.wab"

The problem isn't the variable it holds all the data, the problem is you are passing the variable to a command (del in this case) which expects to see spaces separating terms.  Most windows commands have this expectation.

  So whenever that is the case you need to specify your string with quotes.  To prove my point try running the command manually with the username with and without quotes you will find that only the quotes string will work.

 Del "%UserName%.wab"

there is no need to worry if there are no spaces the quotes will not screw anything up.

Also it's good practice NEVER to CD or change drive letters, this better ensures you don;t accidentally run your commands in the wrong location, see ever so slightly changed script attached which will meet all your needs.

ECHO OFF
CLS

ECHO Copying..
COPY "D:\wab\amy2-27.wab" "C:\Documents and Settings\%UserName%\Application Data\Microsoft\Address Book\amy2-27.wab"

ECHO Deleting..
DEL "C:\Documents and Settings\%UserName%\Application Data\Microsoft\Address Book\%UserName%.wab"

ECHO Renaming..
REN "C:\Documents and Settings\%UserName%\Application Data\Microsoft\Address Book\amy2-27.wab" "%UserName%.wab"

ECHO Script Completed!

Open in new window

0
 
Ben Personick (Previously QCubed)Connect With a Mentor Lead Network EngineerCommented:
strange I miss-read this issue as being a space, not sure how I did that.  I have altered the batch file below to handle your issue, runt he one above first and if it does not work and you still have the same issue then please run this one instead.

ECHO OFF
CLS

ECHO Copying..
COPY "D:\wab\amy2-27.wab" "C:\Documents and Settings\%UserName:_=.%\Application Data\Microsoft\Address Book\amy2-27.wab"

ECHO Deleting..
DEL "C:\Documents and Settings\%UserName:_=.%\Application Data\Microsoft\Address Book\%UserName:_=.%.wab"

ECHO Renaming..
REN "C:\Documents and Settings\%UserName:_=.%\Application Data\Microsoft\Address Book\amy2-27.wab" "%UserName:_=.%.wab"

ECHO Script Completed!

Open in new window

0
 
Rant32Connect With a Mentor Commented:
I was actually first looking how to do this in batch, and I think sirbounty has it heads-on with: %username:_=.% (on second thought, is that the other way around?)

QCubed: the issue is not that the wab file isn't copied correctly. It does. The issue is that Outlook Express creates the default address book to contain underscores instead of periods, so the file name is not identical to the %username%.

I agree on not CD-ing around in scripts. This whole command is just one line then:

COPY /Y "D:\wab\amy2-27.wab" "C:\Documents and Settings\%UserName%\Application Data\Microsoft\Address Book\%username:.=_%.wab"

Although I still think it's technically more correct to retrieve the registry location directly. Especially because the OP has mentioned that he may have changed locations already in the registry, and the above wouldn't work anymore.
0
 
Rant32Commented:
I just got that you read a space. Nevermind that.
0
 
Steve KnightIT ConsultancyCommented:
You could also do it the lazy way and check using if exist to check if %username%.wab exists and if not check if %username:.=_%.wab exists, i.e.

@echo off
REM Set default format for someone with none there yet
set WAB=%username:.=_%.wab"

REM Check if dotted version exists
if exist "%appdata%\Microsoft\Address Book\%username%.wab" (set WAB=%username%.wab)

REM Check if underscore version exists which overrides a dotted version
if exist "%appdata%\Microsoft\Address Book\%username:.=_%.wab" (set WAB=%username:.=_%

REM Copy the file
copy /Y d:\wab\amy2-27.wab "%appdata%\Microsoft\Address Book\%WAB%"

or for the few Kb involved... just copy it to both the _ and . versions on each PC ..

though personally I'd probably read the registry in case it points somewhere else completely.

Steve
0
 
CreeksidetechAuthor Commented:
Great ideas everyone - I am a newbie with batch files, so all of this is a great help to me!  I agree that it probably is a good idea to get the location from the registry, although I haven't moved anything, only changed the default "_" entry on several accounts to a "." entry.  I will definitely get rid of the directory change as well.  I will try/combine some of the suggestions and assign points as required - thanks!
0
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
If you want to read it from the registry in batch you can use something like this, don't have it on this machine to check but the REG QUERY should grab it like that.

Steve

@echo off
set WAB="%appdata%\Microsoft\Address Book\%username:.=_%.wab"
for /f "tokens=3* " %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Wab\WAB4\Wab File Name" ^| find "REG_SZ"') do (set WAB="%%b")
echo The WAB is in %WAB%
copy /y d:\wab\amy2-27.wab %WAB%
0
 
CreeksidetechAuthor Commented:
Thanks for the help everyone - I split the points since the final solution was obtained using feedback from several posts.  I will post back with the script I ended using.
0
 
Steve KnightIT ConsultancyCommented:
No problem, glad if it helped.

Steve
0
All Courses

From novice to tech pro — start learning today.