4

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?

2 Answers 2

13

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)
3
  • 2
    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, 2012 at 14:29
  • 1
    Or just in case, in VBScript: WScript.Echo Left(CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERNAME%"), 7)
    – EBGreen
    Sep 5, 2012 at 14:41
  • Thanks! We were able to get it going using the vbscript option. Sep 5, 2012 at 15:48
1

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.

You must log in to answer this question.

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