Solved

Make a text file from file names

Posted on 2004-09-28
9
170 Views
Last Modified: 2010-05-02
vb6
win 2000

I need to take files name and make a text file  .txt file with just the name and  MINUS the extensions...

Example:
In folder  
c:\PICS\1A002.JPG
c:\PICS\1P321.JPG
c:\PICS\1SQE0.JPG
c:\PICS\1SQDF.JPG
c:\PICS\1A003.JPG
c:\PICS\1A004.JPG
c:\PICS\1VRDR.JPG
ETC........ any file in this folder


The text file would be called:
Names.txt
and would look like
1A002
1P321
1SQE0
1SQDF
1A003
1A004
1VRDR



Thanks
fordraiders





0
Comment
Question by:fordraiders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 

Expert Comment

by:amethyst3739
ID: 12174172
Hi Fordraiders,

You might want to try this:

Private Function RetrieveFiles(s_directory As String, ByRef coll_return_list As Collection)
  Dim s_file_list_item As String
 
  Set coll_return_list = New Collection
 
  s_file_list_item = Dir$(s_directory, vbDirectory)
 
  While (Len(s_file_list_item))
    If GetAttr(s_directory & s_file_list_item) <> vbDirectory Then
      coll_return_list.Add StripFileName(s_file_list_item)
    End If
    s_file_list_item = Dir$
  Wend
End Function

Private Function StripFileName(s_file As String) As String
  Dim n_lastperiod As Integer
 
  n_last_period = InStrRev(s_file, ".")
  If n_last_period <> 0 Then
    StripFileName = Mid(s_file, 1, n_last_period - 1)
  Else
    StripFileName = s_file
  End If
End Function

'------------------------------------------------------------------------------
'And then the calling routine could be something like this:

Private Sub Command1_Click()
 Dim file_list As Collection
 
 RetrieveFiles "C:\", file_list
End Sub
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12175173
Public Sub RenameFiles(ByVal fnFolder As String, fnSrcExtension As String, fnTrgExtension As String)
Dim srcFile As String, lenTrg As String
If Right(fnFolder, 1) <> "\" Then fnFolder = fnFolder & "\"
srcFile = Dir(fnFolder & "*." & fnSrcExtension, vbDirectory)
    Do While Not srcFile = vbNullString
        Select Case srcFile
            Case ".", ".."
            Case Else
                lenTrg = Right(srcFile, Len(srcFile) - InStrRev(srcFile, ".", -1))
                Name fnFolder & srcFile As fnFolder & Left(srcFile, Len(srcFile) - Len(lenTrg)) & fnTrgExtension
        End Select
        srcFile = Dir
    Loop
MsgBox "Mission accomplished"
End Sub

Private Sub Command1_Click()
Call RenameFiles("c:\PICS", "jpg", "txt")
End Sub

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12175192
Didn't read the question properly. Please ignore :)

S
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Shauli
ID: 12175296
Set reference to "Microsoft Scripting runtime" (From the menu bar select Project > Reference > and scroll down to "Microsoft Scripting runtime" and check the box).


Public Sub RenameFiles(ByVal fnSrcFolder As String, fnTrgFolder As String, fnSrcExtension As String, fnTrgExtension As String)
Dim srcFile As String, lenTrg As String, FSO As FileSystemObject
Set FSO = New FileSystemObject
If Right(fnSrcFolder, 1) <> "\" Then fnSrcFolder = fnSrcFolder & "\"
If Right(fnTrgFolder, 1) <> "\" Then fnTrgFolder = fnTrgFolder & "\"
srcFile = Dir(fnSrcFolder & "*." & fnSrcExtension, vbDirectory)
    Do While Not srcFile = vbNullString
        Select Case srcFile
            Case ".", ".."
            Case Else
                FSO.CreateTextFile fnTrgFolder & Left(srcFile, InStrRev(srcFile, ".", -1)) & fnTrgExtension
        End Select
        srcFile = Dir
    Loop
MsgBox "Mission accomplished"
End Sub

Private Sub Command1_Click()
Call RenameFiles("c:\PICS", "c:\PICS", "jpg", "txt")
End Sub

'when you call the sub, the first parameter is the source folder, the second is the target folder, the third is the source extenstion, and the lat is the target extension

S

0
 
LVL 3

Expert Comment

by:sgayatri
ID: 12177387
In the following code
1. Assign your complete path to drv
2. give your file for mainfile

Dim fso
Dim f, sf, f1

        Set fso = CreateObject("scripting.filesystemobject")

        Set mainfile = fso.CreateTextFile("c:\tempfile.txt")
        drv = "c:\"           ''specify your folder here
       
        Set f = fso.GetFolder(drv)
        Set sf = f.SubFolders
       
        For Each f1 In sf
        mainfile.WriteLine (f1.Name)
         mainfile.WriteBlankLines (1)
         
        Next
     
        mainfile.Close
    Set fso = Nothing
           

0
 
LVL 3

Accepted Solution

by:
sgayatri earned 500 total points
ID: 12178295
Kindly take the following as the proper code,
as I forgot to include getting file name without extension
in my previous post


Set fso = CreateObject("scripting.filesystemobject")

        Set mainfile = fso.CreateTextFile("c:\tempfile.txt")
        drv = "c:\myfolder"  ''specify your folder here
       
        Set f = fso.GetFolder(drv)
        Set sf = f.Files
       
        For Each f1 In sf
        mainfile.writeline (fso.getbasename(f1.Name))
       ' mainfile.writeline (f1.Name)
            mainfile.WriteBlankLines (1)
         
        Next
     
        mainfile.Close
    Set fso = Nothing
0
 
LVL 3

Author Comment

by:fordraiders
ID: 12186961
amethyst,
Where does it create and put the new file ?

Thanks
0
 
LVL 3

Author Comment

by:fordraiders
ID: 12187004
Shauli,
I'm not wanting to rename the files themselves.
Take the file names and creating ONE text file. Which will have all the filename but NO extensions.

Just like sgayatri did.
sgayatri ,
code worked great !

0
 
LVL 3

Expert Comment

by:sgayatri
ID: 12187094
Thanks. That's for sure, because I tested it before posting.
What's stopping you to close the question then?!?!?!?!?!
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month4 days, 18 hours left to enroll

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question