Recently, I wanted to make a terminal application automatically use the current signed-in Windows user name as the terminal session user name. When I first started on the task, I thought this will be easy; what acclaimed terminal program with heavily definable session settings is not capable of such? Well shoot – I was mistaken. However, I have never given up on the impossible in the past, and quite frankly this task was definitely not impossible in comparison to other tasks or projects…
As a quick overview to help you understand the problem, each session configuration is kept in individual files located in the \scripts\ folder within the application folder. The session files were not a standard format, such as an INI or XML, and appeared as though the application developer(s) used some proprietary data component for saving session configuration to the hard drive. While reviewing the session files with UltraEdit, I noticed that the data values were in standard text format amongst a fog of extended ASCII characters.
Ah ha! Concatenation is the solution…will it be batch file or .NET application?
Gluing the Pieces Together
Runner up, the batch file. I took the session configuration file and cut it right down the middle (where the user name value was), saving one half as session.p1 and the other half as session.p2. The next trick was to glue these files back together with the current Windows user name in between. I grew up using DOS so I thought that this would be easy; I could just echo %USERNAME% to a file, such as:
echo %USERNAME% > session.nam
Unfortunately, the echo command puts a CR/LF at the end of the file, even though %USERNAME% contains no such characters. Logic would state that since echo is typically used for screen output, it assumes that you want a line feed after your statement. Well, a line feed is going to break my batch file concatenation solution; I can’t have line feeds in a proprietary configuration file!
After much research, I pieced together a batch file line that echo’s a statement without line feeds! This is a golden nugget for anyone that is trying to solve this problem and it worked quite well in my batch file concatenation solution!
for %%a in (%USERNAME%) do ( echo/|set /p ="%%a" ) > session.nam
Obviously, you can replace %USERNAME% with any environment variable or text that you want – no quotes required.
Remember that where there is a will to do so, there is a way to accomplish any task.