Windows Vista Forums

Using Variable In Filename
  1. #1


    Nick Guest

    Using Variable In Filename

    Hello all..

    I have the following two lines in my VBS file



    Set fs = CreateObject("Scripting.FileSystemObject")
    Set TestFile = fs.CreateTextFile("c:\temp\file.txt", True)

    I want to be able to do something like this:
    set testfile = fs.createtextfile("c:\temp\%computername%.txt", true)
    However when I tried that it named the file %computername%.txt ..not exactly
    what I wanted!

    Any suggestions on how I can achieve this?

    Learning as I go...thanks!

      My System SpecsSystem Spec

  2. #2


    Jon Wallace Guest

    Re: Using Variable In Filename

    Hey Nick,

    So you're going to want to obtain the environment variable first and use
    that...

    Set oWshShell = CreateObject("WScript.Shell")
    Set oWshEnv = oWshShell.Environment("PROCESS")

    sComputerName = oWshEnv("ComputerName")

    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oFile = oFS.CreateTextfile("C:\temp\" & sComputerName & ".txt", true)

    My syntax may be off here as I'm doing this from memory and haven't tested
    but that's the basics of it. The CreateTextFile call will take the path
    literally so you have to create the path using VB...

    Hope this helps,
    Jon

    www.insidetheregistry.com

    --


    "Nick" <Nick@xxxxxx> wrote in message
    news:81632E09-71C3-425D-86F3-052505BC8B31@xxxxxx

    > Hello all..
    >
    > I have the following two lines in my VBS file
    >
    > Set fs = CreateObject("Scripting.FileSystemObject")
    > Set TestFile = fs.CreateTextFile("c:\temp\file.txt", True)
    >
    > I want to be able to do something like this:
    > set testfile = fs.createtextfile("c:\temp\%computername%.txt", true)
    > However when I tried that it named the file %computername%.txt ..not
    > exactly
    > what I wanted!
    >
    > Any suggestions on how I can achieve this?
    >
    > Learning as I go...thanks!

      My System SpecsSystem Spec

  3. #3


    James Whitlow Guest

    Re: Using Variable In Filename

    "urkec" <urkec@xxxxxx> wrote in message
    news:E876744A-B663-42C7-84D1-FA2C73DB9432@xxxxxx

    > "Nick" wrote:
    >

    >> Hello all..
    >>
    >> I have the following two lines in my VBS file
    >>
    >> Set fs = CreateObject("Scripting.FileSystemObject")
    >> Set TestFile = fs.CreateTextFile("c:\temp\file.txt", True)
    >>
    >> I want to be able to do something like this:
    >> set testfile = fs.createtextfile("c:\temp\%computername%.txt", true)
    >> However when I tried that it named the file %computername%.txt ..not
    >> exactly
    >> what I wanted!
    >>
    >> Any suggestions on how I can achieve this?
    >>
    >> Learning as I go...thanks!
    >
    >
    > Take a look at WshShell.ExpandEnvironmentStrings method:
    >
    > http://msdn.microsoft.com/en-us/libr...cf(VS.85).aspx
    >
    >
    > Set WshShell = CreateObject ("Wscript.Shell")
    >
    > Set fs = CreateObject("Scripting.FileSystemObject")
    >
    > Set TestFile = fs.CreateTextFile("c:\temp\" _
    > & WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%") _
    > & ".txt", True)
    You can also include the entire path inside the argument passed to the
    'ExpandEnvironmentStrings' method:

    Set TestFile = fs.CreateTextFile(WshShell.ExpandEnvironmentStrings _
    ("c:\temp\%computername%.txt"), true)



      My System SpecsSystem Spec

  4. #4


    Nick Guest

    Re: Using Variable In Filename

    Thanks a lot! All of your answers are a big help!



    "James Whitlow" wrote:

    > "urkec" <urkec@xxxxxx> wrote in message
    > news:E876744A-B663-42C7-84D1-FA2C73DB9432@xxxxxx

    > > "Nick" wrote:
    > >

    > >> Hello all..
    > >>
    > >> I have the following two lines in my VBS file
    > >>
    > >> Set fs = CreateObject("Scripting.FileSystemObject")
    > >> Set TestFile = fs.CreateTextFile("c:\temp\file.txt", True)
    > >>
    > >> I want to be able to do something like this:
    > >> set testfile = fs.createtextfile("c:\temp\%computername%.txt", true)
    > >> However when I tried that it named the file %computername%.txt ..not
    > >> exactly
    > >> what I wanted!
    > >>
    > >> Any suggestions on how I can achieve this?
    > >>
    > >> Learning as I go...thanks!
    > >
    > >
    > > Take a look at WshShell.ExpandEnvironmentStrings method:
    > >
    > > http://msdn.microsoft.com/en-us/libr...cf(VS.85).aspx
    > >
    > >
    > > Set WshShell = CreateObject ("Wscript.Shell")
    > >
    > > Set fs = CreateObject("Scripting.FileSystemObject")
    > >
    > > Set TestFile = fs.CreateTextFile("c:\temp\" _
    > > & WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%") _
    > > & ".txt", True)
    >
    > You can also include the entire path inside the argument passed to the
    > 'ExpandEnvironmentStrings' method:
    >
    > Set TestFile = fs.CreateTextFile(WshShell.ExpandEnvironmentStrings _
    > ("c:\temp\%computername%.txt"), true)
    >
    >
    >

      My System SpecsSystem Spec

Using Variable In Filename problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How can I search for a ( in the filename only? MBUnit Vista General 2 29 Mar 2009
about 8.3 filename bamsan Vista General 3 15 Oct 2007
Get-Acl for filename containing [ and ] Angelina PowerShell 0 01 Mar 2007
What is the RC2 filename please? Beck Vista General 12 30 Oct 2006
How can I ensure that a variable is a built-in powershell variable? Sung M Kim PowerShell 7 22 Sep 2006