Need to truncate the %username% variable in windows 7 to seven characters
I use a script that generates a session name for out AS400 sessions based on the system %username%, the problem is that if the username is over 7 characters long the connection will fail. The session names get one character appended and the connection supports a total lengh of 8 characters.
Any ideas?
Solution 1:
If I understand correctly, the best way to do this will be to select a substring of the username. This can be done like so:
%username:~0,7%
This will select the first seven characters of the username. If you ever need to truncate in a different way, the first number (0) is the start position of the substring, and the second number (7) is the number of characters to select.
As EBGreen pointed out, there are other scripting languages besides batch. If you're using Powershell, the syntax would be:
$env:UserName.Substring(0,7)
If you're using VBScript, the syntax would be:
WScript.Echo Left(CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERNAME%"), 7)
Solution 2:
The clean way would be to create a new environment variable, which is read from the directory server during logon and stored in the user's environment.
This will allow you to change the function used to generate the 7-character names in a central place rather than in all of the scripts, and also allows you to verify uniqueness during account creation.