Solved

Make a text file from file names

Posted on 2004-09-28
9
164 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

828 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