How to pass one or more file names in a directory to a variable name in Windows NT shell script


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: How to pass one or more file names in a directory to a variable name in Windows NT shell script

Hybrid View

  1. #1
    Anh Ly-Hang Guest

    How to pass one or more file names in a directory to a variable name in Windows NT shell script


    Hi,

    I need your help.

    1) Could someone with expertise on Windows NT 4.0 shell scripting show me
    how to pass one or more file names in a (local or remote) directory to a
    variable name in the Windows NT shell script?

    2) Using Windows NT 4.0 shell scripting, can we open, read, and close a
    file in a designate directory?

    I'd really appreciate if someone could help me with these two questions above.

    Sincerely,

    Anh Ly-Hang
    anh_ly-hang@vanguard.com

    Share on Google+

  2. #2
    L.J. Johnson Guest

    Re: How to pass one or more file names in a directory to a variable name in Windows NT shell script

    Anh,

    > 1) Could someone with expertise on Windows NT 4.0 shell scripting show me
    > how to pass one or more file names in a (local or remote) directory to a
    > variable name in the Windows NT shell script?
    >
    > 2) Using Windows NT 4.0 shell scripting, can we open, read, and close a
    > file in a designate directory?


    You mean like this? You have full access to the FileSystemObject within the
    script, so you can do pretty much what you want with the file. You can also
    pass multiple file names (up to the max length of the command line) to the
    command line, and they will be additional arguments in the command line
    array.

    C:\>test.vbs "This is a test file.txt"

    C:\>type userlog.txt
    This is a test
    This is a test
    This is a test
    Hello
    Goodbye
    This is a test file.txt

    ' text for test.vbs
    Option Explicit

    Dim ArgObj ' Object which contains the command line argument
    Dim Result ' Result of the command function call

    Set ArgObj = WScript.Arguments

    ' Test to make sure there is at least one command line arg - the command
    If ArgObj.Count < 1 Then
    DisplayHelpMessage
    WScript.Quit (GENERAL_FAILURE)
    End If

    ' Send first arg to text file
    AppendText ArgObj.Item(0)

    Sub AppendText (txt)
    Const file1 = "C:\UserLog.txt" ' Log file name
    Const ForAppending = 8 ' Append mode
    Dim fso, fi ' object variable

    ' Creates a FileSystemObject object
    Set fso = CreateObject("Scripting.FileSystemObject")

    ' open file, force create, if not exists
    Set fi = fso.OpenTextFile(file1, ForAppending, true)

    fi.WriteLine (txt) ' append log
    Set fi = Nothing
    End Sub

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    <http://www.SlightlyTiltedSoftware.com>
    Ask The NT Pro at <http://www.devx.com/gethelp>

    "Anh Ly-Hang" <anh_ly-hang@vanguard.com> wrote in message
    news:3b70202b$1@news.devx.com...
    >
    > Hi,
    >
    > I need your help.
    >
    >
    > I'd really appreciate if someone could help me with these two questions

    above.
    >
    > Sincerely,
    >
    > Anh Ly-Hang
    > anh_ly-hang@vanguard.com
    >



    Share on Google+

  3. #3
    Anh Ly-Hang Guest

    Re: How to pass one or more file names in a directory to a variable name in Windows NT shell script


    Hi L.J.,

    Thank you very much for your information.

    I am sorry for not very clear on Windows NT 4.0 shell scripting. The script
    language here is NT scripts, not VB scripts or Javascripts.

    For example:

    set var="Hello L.J."
    echo %var%

    or if %ERRORLEVEL% EQU 3 goto :EOF


    Since NT scripting is not very powerful as Unix scripting, I have some obstacles
    when programming with NT scripting.

    For question 1, I have found and tested a way to pass a file name in the
    directory to a variable in the NT script.

    However, for question 2, I still try to figure out how to develop NT script
    to open, read, and close a file in the directory.

    Again, I really appreciated your help on this, L.J.

    -- Anh

    ------------------------------------------

    "L.J. Johnson" <LJJohnson@SlightlyTiltedSoftware.com> wrote:
    >Anh,
    >
    >> 1) Could someone with expertise on Windows NT 4.0 shell scripting show

    me
    >> how to pass one or more file names in a (local or remote) directory to

    a
    >> variable name in the Windows NT shell script?
    >>
    >> 2) Using Windows NT 4.0 shell scripting, can we open, read, and close

    a
    >> file in a designate directory?

    >
    >You mean like this? You have full access to the FileSystemObject within

    the
    >script, so you can do pretty much what you want with the file. You can also
    >pass multiple file names (up to the max length of the command line) to the
    >command line, and they will be additional arguments in the command line
    >array.
    >
    >C:\>test.vbs "This is a test file.txt"
    >
    >C:\>type userlog.txt
    >This is a test
    >This is a test
    >This is a test
    >Hello
    >Goodbye
    >This is a test file.txt
    >
    >' text for test.vbs
    >Option Explicit
    >
    >Dim ArgObj ' Object which contains the command line argument
    >Dim Result ' Result of the command function call
    >
    >Set ArgObj = WScript.Arguments
    >
    >' Test to make sure there is at least one command line arg - the command
    >If ArgObj.Count < 1 Then
    > DisplayHelpMessage
    > WScript.Quit (GENERAL_FAILURE)
    >End If
    >
    >' Send first arg to text file
    >AppendText ArgObj.Item(0)
    >
    >Sub AppendText (txt)
    >Const file1 = "C:\UserLog.txt" ' Log file name
    >Const ForAppending = 8 ' Append mode
    >Dim fso, fi ' object variable
    >
    >' Creates a FileSystemObject object
    > Set fso = CreateObject("Scripting.FileSystemObject")
    >
    >' open file, force create, if not exists
    > Set fi = fso.OpenTextFile(file1, ForAppending, true)
    >
    > fi.WriteLine (txt) ' append log
    > Set fi = Nothing
    >End Sub
    >
    >--
    >L.J. Johnson, Slightly Tilted Software
    >Microsoft MVP (Visual Basic)
    >LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    ><http://www.SlightlyTiltedSoftware.com>
    >Ask The NT Pro at <http://www.devx.com/gethelp>
    >
    >"Anh Ly-Hang" <anh_ly-hang@vanguard.com> wrote in message
    >news:3b70202b$1@news.devx.com...
    >>
    >> Hi,
    >>
    >> I need your help.
    >>
    >>
    >> I'd really appreciate if someone could help me with these two questions

    >above.
    >>
    >> Sincerely,
    >>
    >> Anh Ly-Hang
    >> anh_ly-hang@vanguard.com
    >>

    >
    >


    Share on Google+

  4. #4
    L.J. Johnson Guest

    Re: How to pass one or more file names in a directory to a variable name in Windows NT shell script

    Anh,

    Ah, batch files. Been a while...

    Create a batch file Test.Bat (see text below). Call it with "Test aa bb cc
    dd"

    As far as dealing with the lines returned from the file you created on a
    more detailed basis than just displaying every line, see
    http://www.winscriptingsolutions.com...rticleID=21984

    ===================================================================
    @echo off

    If "%1" == "" Goto TheError

    Rem Append to file
    :Loop
    Echo %1 >> C:\Test1.txt
    Shift
    If Not '%1' == '' Goto Loop

    Rem Create new file
    Echo %1 > C:\Test2.txt

    Echo.
    Echo ----------------------------------
    Echo This is test1.txt
    Echo ----------------------------------
    For /f %%i in (C:\Test1.txt) Do @Echo %%i
    Echo ----------------------------------
    Echo.

    Goto TheEnd

    :TheError
    Echo.
    Echo ----------------------------------
    Echo Error
    Echo You must enter at least 1 filename
    Echo ----------------------------------
    Echo.

    :TheEnd

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    <http://www.SlightlyTiltedSoftware.com>
    Ask The NT Pro at <http://www.devx.com/gethelp>

    "Anh Ly-Hang" <anh-ly_hang@vanguard.com> wrote in message
    news:3b7151f9$1@news.devx.com...
    >
    > Hi L.J.,
    >
    > Thank you very much for your information.
    >
    > I am sorry for not very clear on Windows NT 4.0 shell scripting. The

    script
    > language here is NT scripts, not VB scripts or Javascripts.
    >
    > For example:
    >
    > set var="Hello L.J."
    > echo %var%
    >
    > or if %ERRORLEVEL% EQU 3 goto :EOF
    >
    >
    > Since NT scripting is not very powerful as Unix scripting, I have some

    obstacles
    > when programming with NT scripting.
    >
    > For question 1, I have found and tested a way to pass a file name in the
    > directory to a variable in the NT script.
    >
    > However, for question 2, I still try to figure out how to develop NT

    script
    > to open, read, and close a file in the directory.
    >
    > Again, I really appreciated your help on this, L.J.
    >
    > -- Anh
    >
    > ------------------------------------------
    >
    > "L.J. Johnson" <LJJohnson@SlightlyTiltedSoftware.com> wrote:
    > >Anh,
    > >
    > >> 1) Could someone with expertise on Windows NT 4.0 shell scripting show

    > me
    > >> how to pass one or more file names in a (local or remote) directory to

    > a
    > >> variable name in the Windows NT shell script?
    > >>
    > >> 2) Using Windows NT 4.0 shell scripting, can we open, read, and close

    > a
    > >> file in a designate directory?

    > >
    > >You mean like this? You have full access to the FileSystemObject within

    > the
    > >script, so you can do pretty much what you want with the file. You can

    also
    > >pass multiple file names (up to the max length of the command line) to

    the
    > >command line, and they will be additional arguments in the command line
    > >array.
    > >
    > >C:\>test.vbs "This is a test file.txt"
    > >
    > >C:\>type userlog.txt
    > >This is a test
    > >This is a test
    > >This is a test
    > >Hello
    > >Goodbye
    > >This is a test file.txt
    > >
    > >' text for test.vbs
    > >Option Explicit
    > >
    > >Dim ArgObj ' Object which contains the command line argument
    > >Dim Result ' Result of the command function call
    > >
    > >Set ArgObj = WScript.Arguments
    > >
    > >' Test to make sure there is at least one command line arg - the command
    > >If ArgObj.Count < 1 Then
    > > DisplayHelpMessage
    > > WScript.Quit (GENERAL_FAILURE)
    > >End If
    > >
    > >' Send first arg to text file
    > >AppendText ArgObj.Item(0)
    > >
    > >Sub AppendText (txt)
    > >Const file1 = "C:\UserLog.txt" ' Log file name
    > >Const ForAppending = 8 ' Append mode
    > >Dim fso, fi ' object variable
    > >
    > >' Creates a FileSystemObject object
    > > Set fso = CreateObject("Scripting.FileSystemObject")
    > >
    > >' open file, force create, if not exists
    > > Set fi = fso.OpenTextFile(file1, ForAppending, true)
    > >
    > > fi.WriteLine (txt) ' append log
    > > Set fi = Nothing
    > >End Sub
    > >
    > >--
    > >L.J. Johnson, Slightly Tilted Software
    > >Microsoft MVP (Visual Basic)
    > >LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    > ><http://www.SlightlyTiltedSoftware.com>
    > >Ask The NT Pro at <http://www.devx.com/gethelp>
    > >
    > >"Anh Ly-Hang" <anh_ly-hang@vanguard.com> wrote in message
    > >news:3b70202b$1@news.devx.com...
    > >>
    > >> Hi,
    > >>
    > >> I need your help.
    > >>
    > >>
    > >> I'd really appreciate if someone could help me with these two questions

    > >above.
    > >>
    > >> Sincerely,
    > >>
    > >> Anh Ly-Hang
    > >> anh_ly-hang@vanguard.com
    > >>

    > >
    > >

    >



    Share on Google+

  5. #5
    Anh Ly-Hang Guest

    Re: How to pass one or more file names in a directory to a variable name in Windows NT shell script


    Hi L.J.,

    Thank you so much for your help and samples and pointing me to the helpful
    information for my solution :-))

    Have a great week!

    Best regards,

    -- Anh
    ------------------------------------------

    "L.J. Johnson" <LJJohnson@SlightlyTiltedSoftware.com> wrote:
    >Anh,
    >
    >Ah, batch files. Been a while...
    >
    >Create a batch file Test.Bat (see text below). Call it with "Test aa bb

    cc
    >dd"
    >
    >As far as dealing with the lines returned from the file you created on a
    >more detailed basis than just displaying every line, see
    >http://www.winscriptingsolutions.com...rticleID=21984
    >
    >===================================================================
    >@echo off
    >
    >If "%1" == "" Goto TheError
    >
    >Rem Append to file
    >:Loop
    > Echo %1 >> C:\Test1.txt
    > Shift
    > If Not '%1' == '' Goto Loop
    >
    >Rem Create new file
    >Echo %1 > C:\Test2.txt
    >
    >Echo.
    >Echo ----------------------------------
    >Echo This is test1.txt
    >Echo ----------------------------------
    >For /f %%i in (C:\Test1.txt) Do @Echo %%i
    >Echo ----------------------------------
    >Echo.
    >
    >Goto TheEnd
    >
    >:TheError
    >Echo.
    >Echo ----------------------------------
    >Echo Error
    >Echo You must enter at least 1 filename
    >Echo ----------------------------------
    >Echo.
    >
    >:TheEnd
    >
    >--
    >L.J. Johnson, Slightly Tilted Software
    >Microsoft MVP (Visual Basic)
    >LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    ><http://www.SlightlyTiltedSoftware.com>
    >Ask The NT Pro at <http://www.devx.com/gethelp>
    >
    >"Anh Ly-Hang" <anh-ly_hang@vanguard.com> wrote in message
    >news:3b7151f9$1@news.devx.com...
    >>
    >> Hi L.J.,
    >>
    >> Thank you very much for your information.
    >>
    >> I am sorry for not very clear on Windows NT 4.0 shell scripting. The

    >script
    >> language here is NT scripts, not VB scripts or Javascripts.
    >>
    >> For example:
    >>
    >> set var="Hello L.J."
    >> echo %var%
    >>
    >> or if %ERRORLEVEL% EQU 3 goto :EOF
    >>
    >>
    >> Since NT scripting is not very powerful as Unix scripting, I have some

    >obstacles
    >> when programming with NT scripting.
    >>
    >> For question 1, I have found and tested a way to pass a file name in the
    >> directory to a variable in the NT script.
    >>
    >> However, for question 2, I still try to figure out how to develop NT

    >script
    >> to open, read, and close a file in the directory.
    >>
    >> Again, I really appreciated your help on this, L.J.
    >>
    >> -- Anh
    >>
    >> ------------------------------------------
    >>
    >> "L.J. Johnson" <LJJohnson@SlightlyTiltedSoftware.com> wrote:
    >> >Anh,
    >> >
    >> >> 1) Could someone with expertise on Windows NT 4.0 shell scripting

    show
    >> me
    >> >> how to pass one or more file names in a (local or remote) directory

    to
    >> a
    >> >> variable name in the Windows NT shell script?
    >> >>
    >> >> 2) Using Windows NT 4.0 shell scripting, can we open, read, and close

    >> a
    >> >> file in a designate directory?
    >> >
    >> >You mean like this? You have full access to the FileSystemObject within

    >> the
    >> >script, so you can do pretty much what you want with the file. You can

    >also
    >> >pass multiple file names (up to the max length of the command line) to

    >the
    >> >command line, and they will be additional arguments in the command line
    >> >array.
    >> >
    >> >C:\>test.vbs "This is a test file.txt"
    >> >
    >> >C:\>type userlog.txt
    >> >This is a test
    >> >This is a test
    >> >This is a test
    >> >Hello
    >> >Goodbye
    >> >This is a test file.txt
    >> >
    >> >' text for test.vbs
    >> >Option Explicit
    >> >
    >> >Dim ArgObj ' Object which contains the command line argument
    >> >Dim Result ' Result of the command function call
    >> >
    >> >Set ArgObj = WScript.Arguments
    >> >
    >> >' Test to make sure there is at least one command line arg - the command
    >> >If ArgObj.Count < 1 Then
    >> > DisplayHelpMessage
    >> > WScript.Quit (GENERAL_FAILURE)
    >> >End If
    >> >
    >> >' Send first arg to text file
    >> >AppendText ArgObj.Item(0)
    >> >
    >> >Sub AppendText (txt)
    >> >Const file1 = "C:\UserLog.txt" ' Log file name
    >> >Const ForAppending = 8 ' Append mode
    >> >Dim fso, fi ' object variable
    >> >
    >> >' Creates a FileSystemObject object
    >> > Set fso = CreateObject("Scripting.FileSystemObject")
    >> >
    >> >' open file, force create, if not exists
    >> > Set fi = fso.OpenTextFile(file1, ForAppending, true)
    >> >
    >> > fi.WriteLine (txt) ' append log
    >> > Set fi = Nothing
    >> >End Sub
    >> >
    >> >--
    >> >L.J. Johnson, Slightly Tilted Software
    >> >Microsoft MVP (Visual Basic)
    >> >LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    >> ><http://www.SlightlyTiltedSoftware.com>
    >> >Ask The NT Pro at <http://www.devx.com/gethelp>
    >> >
    >> >"Anh Ly-Hang" <anh_ly-hang@vanguard.com> wrote in message
    >> >news:3b70202b$1@news.devx.com...
    >> >>
    >> >> Hi,
    >> >>
    >> >> I need your help.
    >> >>
    >> >>
    >> >> I'd really appreciate if someone could help me with these two questions
    >> >above.
    >> >>
    >> >> Sincerely,
    >> >>
    >> >> Anh Ly-Hang
    >> >> anh_ly-hang@vanguard.com
    >> >>
    >> >
    >> >

    >>

    >
    >


    Share on Google+

  6. #6
    Hansjoerg Henker Guest

    Re: How to pass one or more file names in a directory to a variable name in Windows NT shell script


    "Anh Ly-Hang" <anh_ly-hang@vanguard.com> wrote:
    >
    >Hi,
    >
    >I need your help.

    Hi again,

    >1) Could someone with expertise on Windows NT 4.0 shell scripting show

    me
    >how to pass one or more file names in a (local or remote) directory to a
    >variable name in the Windows NT shell script?

    Return the required string as default-output and interprete this with "for"
    - like this:

    for /F %A IN ('CScript test.vbs') do set Test_Result=%A

    This should be enough...

    >2) Using Windows NT 4.0 shell scripting, can we open, read, and close a
    >file in a designate directory?

    I couldnt understand the vocabular: designate - so I cant help you.

    >I'd really appreciate if someone could help me with these two questions

    above.
    >
    >Sincerely,
    >
    >Anh Ly-Hang
    >anh_ly-hang@vanguard.com
    >

    Bye, Hansjoerg....
    Share on Google+

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center