Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers

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)
share|improve this answer
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 '12 at 14:29
1  
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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.