Solved

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

Posted on 2010-08-30
15
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +4
15 Comments
 
LVL 13

Expert Comment

by:markusdamenous
ID: 33560038
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
ID: 33560066
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
ID: 33560307
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 67

Accepted Solution

by:
sirbounty earned 125 total points
ID: 33561751
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
ID: 33561869
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
ID: 33561897
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 12
ID: 33562918
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
 
LVL 12

Assisted Solution

by:Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed) earned 125 total points
ID: 33562996
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
ID: 33563008
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
ID: 33563033
I just got that you read a space. Nevermind that.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33565477
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
ID: 33565852
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
ID: 33565989
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
ID: 33613823
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
ID: 33613874
No problem, glad if it helped.

Steve
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Scenerio: You have a server running Server 2003 and have applied a retail pack of Terminal Server Licenses.  You want to change servers or your server has crashed and you need to reapply the Terminal Server Licenses. When you enter the 16-digit lic…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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