Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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:


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:


If you're using VBScript, the syntax would be:

WScript.Echo Left(CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERNAME%"), 7)
share|improve this answer
I assume that the script language is batch as well as SaintWacko did. Just as an addition though, in Powershell you could do $env:UserName.Substring(0,7) – EBGreen Sep 5 '12 at 14:29
Or just in case, in VBScript: WScript.Echo Left(CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERNAME%"), 7) – EBGreen Sep 5 '12 at 14:41
Thanks! We were able to get it going using the vbscript option. – willrun4fun Sep 5 '12 at 15:48

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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .