Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Make a text file from file names

Posted on 2004-09-28
9
Medium Priority
?
175 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
Industry Leaders: 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Suggested Courses

876 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