Using a string variable in the title of a .txt

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
strunker
Posts: 1
Last visit: Thu Mar 29, 2012 4:11 am

Using a string variable in the title of a .txt

Post by strunker »

Set wshShell = WScript.CreateObject( "WScript.Shell" )Set objFSO = CreateObject("Scripting.FileSystemObject")strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )Set objFile = objFSO.OpenTextFile("c:info.txt",2,true,2)objFile.WriteLine "Computer Name=" & strComputerNameobjFile.Close That above script is very simple. It generates a text file and pouts the computer name in the body. This is just a small snippet of a much larger script. The proper variable, in this case strComputerName has already been declared. What I am trying to accomplish is this. I would like the computer name to be the name of the file so for example : Randompc.txt I have tried Set objFile = objFSO.OpenTextFile("c:%computername".txt",2,true,2)Which didnt work: ' Define the path for our text file strFilePath = "C:WinInfo.txt" strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" ) Set objFSO = CreateObject("Scripting.FileSystemObject") ' Open the file for write access. Set objFile = objFSO.OpenTextFile(strFilePath, 2, True, 0)This is a peace from the larger script. here I define the file path, and then call the text file and add write privileges to it. My main question is. Whether you use the first code snip, or the second one (which is preferable) how can I add the actual computer name into the title?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Using a string variable in the title of a .txt

Post by jvierra »

'spelt' is spelled. 'Post OPtions' has the edit item on its menu.

You need to learn basic scripting. You clearly do not have any understanding about strings in VBScript.

Start here: http://www.w3schools.com/vbscript/default.asp

sString are concatenated using the & operator. You cannot just type them all in a row.
"mypsname1" & ".txt" will create a string based on the two parts. With that little gem try your script again.

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Using a string variable in the title of a .txt

Post by jvierra »

Start here: http://www.w3schools.com/vbscript/default.asp

sString are concatenated using the & operator. You cannot just type them all in a row.
"mypsname1" & ".txt" will create a string based on the two parts. With that little gem try your script again.

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Using a string variable in the title of a .txt

Post by jvierra »

Yu need to concatenate a varaible to a string.
Please do this: http://www.w3schools.com/vbscript/default.asp

Study the parts about variables and about strings until it is clear in your mind how this works.

a & b
A & "b"
"A" & "b"
"a & b"

Think about what is happening. You will eventually see it.
This topic is 11 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked