Solved

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

Posted on 2010-08-30
15
731 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:Creeksidetech
  • 4
  • 3
  • 3
  • +4
15 Comments
 
LVL 13

Expert Comment

by:markusdamenous
Comment Utility
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
 

Author Comment

by:Creeksidetech
Comment Utility
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
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
 
LVL 67

Accepted Solution

by:
sirbounty earned 125 total points
Comment Utility
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
 
LVL 1

Expert Comment

by:eric27
Comment Utility
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
 
LVL 12

Expert Comment

by:Rant32
Comment Utility
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
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Assisted Solution

by:Ben Personick
Ben Personick earned 125 total points
Comment Utility
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
 
LVL 12

Assisted Solution

by:Rant32
Rant32 earned 125 total points
Comment Utility
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
 
LVL 12

Expert Comment

by:Rant32
Comment Utility
I just got that you read a space. Nevermind that.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
 

Author Comment

by:Creeksidetech
Comment Utility
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
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 125 total points
Comment Utility
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
 

Author Comment

by:Creeksidetech
Comment Utility
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
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
No problem, glad if it helped.

Steve
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us need to configure DHCP server(s) in their environment. We can do that simply via DHCP console on server or using MMC snap-in on each computer with Administrative Tools installed in a network. But what if we have to configure many DHCP ser…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now