Hello,
I am making a *.vbs file and I need help putting two codes in.
The first code is to make a .txt file named "Hello World", with the text in the .txt file "Time ... (your time)-Test123"

Code:

myfilename = "\Hello World.txt"
MakeHelloWorldFile myfilename
 
Sub MakeHelloWorldFile (FileName)
'Create a new file in C: drive or overwrite existing file
   Set FSO = CreateObject("Scripting.FileSystemObject")
   If FSO.FileExists(FileName) Then 
      Answer = MsgBox ("File " & FileName & " exists ... OK to overwrite?", vbOKCancel)
      'If button selected is not OK, then quit now
      'vbOK is a language constant
      If Answer <> vbOK Then Exit Sub
   Else
      'Confirm OK to create
      Answer = MsgBox ("File " & FileName & " ... OK to create?", vbOKCancel)
      If Answer <> vbOK Then Exit Sub
   End If
   'Create new file (or replace an existing file)
   Set FileObject = FSO.CreateTextFile (FileName)
   FileObject.WriteLine "Time ... " & Now()
   FileObject.WriteLine "Test123"
   FileObject.Close()
   MsgBox "File " & FileName & " ... updated."
   End Sub

And the 2nd file I want to combine with this is to desplay the IP address of the machine. I would like to make it desplay the IP address in the .txt file, not as a msg box.
Code:

strcomputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

for each objitem in colitems

strIPAddress = Join(objitem.IPAddress, ",")
         IP = stripaddress
wscript.echo ip

next

Hi

If I understand you right you just want to merge the two bits of code in your post
and put the output (Ip addreess) out to the 'Hello World.txt' file.

if so the following should do it or if I've got it wrong then sorry!

myfilename = "\Hello World.txt"
MakeHelloWorldFile myfilename
 
Sub MakeHelloWorldFile (FileName)
'Create a new file in C: drive or overwrite existing file
   Set FSO = CreateObject("Scripting.FileSystemObject")
   If FSO.FileExists(FileName) Then 
      Answer = MsgBox ("File " & FileName & " exists ... OK to overwrite?", vbOKCancel)
      'If button selected is not OK, then quit now
      'vbOK is a language constant
      If Answer <> vbOK Then Exit Sub
   Else
      'Confirm OK to create
      Answer = MsgBox ("File " & FileName & " ... OK to create?", vbOKCancel)
      If Answer <> vbOK Then Exit Sub
   End If


   'Create new file (or replace an existing file)
   Set FileObject = FSO.CreateTextFile (FileName)

  ' ***  New line to O/P Ip address
   FileObject.WriteLine "Ip   ... " & GetIP
   FileObject.WriteLine "Time ... " & Now()
   FileObject.WriteLine "Test123"
   FileObject.Close()
   MsgBox "File " & FileName & " ... updated."
 End Sub


  ' ***  New Function (Code 2) to return Ip address

Function GetIp()

  strcomputer = "."
  Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

  Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

  for each objitem in colitems

    strIPAddress = Join(objitem.IPAddress, ",")
    ip = stripaddress
' wscript.echo ip
  next
  GetIP = ip

End Function

You can always PM me if I got what you want wrong and I'll look at it again for you.


Hello,
I am making a *.vbs file and I need help putting two codes in.
The first code is to make a .txt file named "Hello World", with the text in the .txt file "Time ... (your time)-Test123"

Code:

myfilename = "\Hello World.txt"
MakeHelloWorldFile myfilename
 
Sub MakeHelloWorldFile (FileName)
'Create a new file in C: drive or overwrite existing file
   Set FSO = CreateObject("Scripting.FileSystemObject")
   If FSO.FileExists(FileName) Then 
      Answer = MsgBox ("File " & FileName & " exists ... OK to overwrite?", vbOKCancel)
      'If button selected is not OK, then quit now
      'vbOK is a language constant
      If Answer <> vbOK Then Exit Sub
   Else
      'Confirm OK to create
      Answer = MsgBox ("File " & FileName & " ... OK to create?", vbOKCancel)
      If Answer <> vbOK Then Exit Sub
   End If
   'Create new file (or replace an existing file)
   Set FileObject = FSO.CreateTextFile (FileName)
   FileObject.WriteLine "Time ... " & Now()
   FileObject.WriteLine "Test123"
   FileObject.Close()
   MsgBox "File " & FileName & " ... updated."
   End Sub

And the 2nd file I want to combine with this is to desplay the IP address of the machine. I would like to make it desplay the IP address in the .txt file, not as a msg box.
Code:

strcomputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

for each objitem in colitems

strIPAddress = Join(objitem.IPAddress, ",")
         IP = stripaddress
wscript.echo ip

next

This worked exactly how I would like it, thanks!

Hi

If I understand you right you just want to merge the two bits of code in your post
and put the output (Ip addreess) out to the 'Hello World.txt' file.

if so the following should do it or if I've got it wrong then sorry!

myfilename = "\Hello World.txt"
MakeHelloWorldFile myfilename
 
Sub MakeHelloWorldFile (FileName)
'Create a new file in C: drive or overwrite existing file
   Set FSO = CreateObject("Scripting.FileSystemObject")
   If FSO.FileExists(FileName) Then 
      Answer = MsgBox ("File " & FileName & " exists ... OK to overwrite?", vbOKCancel)
      'If button selected is not OK, then quit now
      'vbOK is a language constant
      If Answer <> vbOK Then Exit Sub
   Else
      'Confirm OK to create
      Answer = MsgBox ("File " & FileName & " ... OK to create?", vbOKCancel)
      If Answer <> vbOK Then Exit Sub
   End If


   'Create new file (or replace an existing file)
   Set FileObject = FSO.CreateTextFile (FileName)

  ' ***  New line to O/P Ip address
   FileObject.WriteLine "Ip   ... " & GetIP
   FileObject.WriteLine "Time ... " & Now()
   FileObject.WriteLine "Test123"
   FileObject.Close()
   MsgBox "File " & FileName & " ... updated."
 End Sub


  ' ***  New Function (Code 2) to return Ip address

Function GetIp()

  strcomputer = "."
  Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

  Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

  for each objitem in colitems

    strIPAddress = Join(objitem.IPAddress, ",")
    ip = stripaddress
' wscript.echo ip
  next
  GetIP = ip

End Function

You can always PM me if I got what you want wrong and I'll look at it again for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.