Solved

Make a text file from file names

Posted on 2004-09-28
9
161 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now