1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Append first line of dir files to output file list

Discussion in 'Software Development' started by JuniperGreen, Jan 3, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. JuniperGreen

    JuniperGreen Thread Starter

    Joined:
    Jan 3, 2013
    Messages:
    25
    As a newbie I am struggling to write a VBS script and would appreciate some help. I have searched through previous queries but couldn't find anything to match this:

    I have directory with a fair number of files in it and wish to write a VBS script which will go through all the files in the directory and copy the first line of each of the files and append each first line to a list in an output file.

    I have made a start but am at a loss how to proceed to write the info to the output file

    Any help or script would be greatly appreciated.

    JuniperGreen

    "Script:

    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set folder = objFSO.GetFolder("c:\library\")
    Set outfile = objFSO.CreateTextFile("c:\library2\Reference.txt")
    for each file in folder.Files
    Set testfile = objFSO.OpenTextFile(file.path, ForReading)
    Do While Not testfile.AtEndOfStream

    StrLine = testfile.ReadLine
    Loop
    Testfile.close
    Set testfile = nothing
    Next
    Outfile.close"

    Vista Home Premium 32 bit
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    I can do this in a batch file if you want.
     
  3. JuniperGreen

    JuniperGreen Thread Starter

    Joined:
    Jan 3, 2013
    Messages:
    25
    While I would really prefer to use VBS I can't be choosy as I really need to get this sorted. A batch file version would be appreciated - I haven't used a batch file since the last true DOS in Windows 98. I take it it can still run using CMD in Vista?

    Thank you
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Code:
    @echo off
    FOR %%G in (c:\library\*) do (
    	setlocal enabledelayedexpansion
    	set /p line=<"%%~G"
    	echo !line!>>"c:\library2\Reference.txt"
    	endlocal
    )
     
  5. draceplace

    draceplace

    Joined:
    Jun 8, 2001
    Messages:
    2,583
    Here is the VB, Squashmans .bat looks a lot easier!
    'Copy First Line of file
    ' ---------------------------------------------------------------
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    'set up for write file
    Const ForReading = 1
    Const ForWriting = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set folder = objFSO.GetFolder("H:\A1\")
    Set outfile = objFSO.CreateTextFile("H:\A2\Reference.txt", ForWriting)
    for each file in folder.Files
    Set testfile = objFSO.OpenTextFile(file.path, ForReading)
    StrLine = testfile.ReadLine
    outfile.WriteLine (StrLine)
    Testfile.close
    Next
    Outfile.close
    Set testfile = nothing
    Set objfso = nothing
     
  6. JuniperGreen

    JuniperGreen Thread Starter

    Joined:
    Jan 3, 2013
    Messages:
    25
    My grateful thanks to Squashman and draceplace for providing me with the batch and vbs solutions. I got both working perfectly!

    Cheers guys!
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1083591

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice