Solved Rename Text Files Based On Content First Line

October 30, 2017 at 06:33:06
Specs: Windows 10
Hi,

I desperately need to rename several hundred text files (mostly unicode) based on the first line of its content.

Can someone urgently help me with it please....

thanks


See More: Rename Text Files Based On Content First Line

Reply ↓  Report •

✔ Best Answer
November 1, 2017 at 01:23:43
Hi Sofia,

That is brilliant I am glad it worked for you, please can you mark this thread and solved by selecting a best answer.

Many thanks,



#1
October 30, 2017 at 06:49:39
Here is a quick VBA solution.

You will need to paste this into Excel VBE and run the code, ensure you change the path within the code to the folder where the files are kept. This code DOES NOT work with sub folders currently but can be modified to work with them..

What this will do is

1) Open each text file
2) Read the first line
3) close the text file
4) Rename the text file with the line it read from it

Sub RenameTextFile()

    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fso, MyFile, FileName, TextLine

    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set fol = fso.getfolder("C:\FolderPath\")
            
    For Each fil In fol.Files
          
        FileName = fil

        Set MyFile = fso.OpenTextFile(FileName, ForReading)
    
        Do While MyFile.AtEndOfStream <> True
        
            TextLine = MyFile.ReadLine
            MyFile.Close
            
            fil.Name = TextLine & ".txt"
            Exit Do
            
        Loop

        MyFile.Close
    Next fil
End Sub

message edited by AlwaysWillingToLearn


Reply ↓  Report •

#2
October 30, 2017 at 06:58:46
If TextLine Contains \ or / or : or ? or < or > or |" then
  Uh oh!
End If

;-)

How To Post Data or Code ---> Click Here Before Posting Data or VBA Code


Reply ↓  Report •

#3
October 30, 2017 at 06:59:49
Just waiting for the OP to come back screaming so we can code those errors out :)

Reply ↓  Report •

Related Solutions

#4
October 30, 2017 at 07:11:05
An update to remove special characters

Sub RenameTextFile()
    Const SpecialCharacters As String = "!,@,#,$,%,^,&,*,(,),{,[,],},?" ' Modify this as neccesary
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fso, MyFile, FileName, TextLine
    Dim char As Variant
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set fol = fso.getfolder("\C:\FolderPath\")
            
    For Each fil In fol.Files
          
        FileName = fil

        Set MyFile = fso.OpenTextFile(FileName, ForReading)
    
        Do While MyFile.AtEndOfStream <> True
        
            TextLine = MyFile.ReadLine
            MyFile.Close
            
            For Each char In Split(SpecialCharacters, ",")
                TextLine = Replace(TextLine, char, "")
            Next
            
            fil.Name = TextLine & ".txt"
            Exit Do
            
        Loop

        MyFile.Close
    Next fil
End Sub

message edited by AlwaysWillingToLearn


Reply ↓  Report •

#5
October 30, 2017 at 07:22:47
Erm, the actual list of invalid characters are '\ / : * ? " < > |', you've only got 2 of them.

How To Ask Questions The Smart Way


Reply ↓  Report •

#6
October 30, 2017 at 07:29:49
Too much haste and not enough patience that's my problem.....

The only issue is I cannot get the " sign in the invalid characters list, any ideas?

Sub RenameTextFile()
    Const SpecialCharacters As String = "\,/,:,*,?,<,>,|," ' Modify this as necessary
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fso, MyFile, FileName, TextLine
    Dim char As Variant
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set fol = fso.getfolder("C:\FolderPath\")
            
    For Each fil In fol.Files
          
        FileName = fil

        Set MyFile = fso.OpenTextFile(FileName, ForReading)
    
        Do While MyFile.AtEndOfStream <> True
        
            TextLine = MyFile.ReadLine
            MyFile.Close
            
            For Each char In Split(SpecialCharacters, ",")
                TextLine = Replace(TextLine, char, "")
            Next
            
            fil.Name = TextLine & ".txt"
            Exit Do
            
        Loop

        MyFile.Close
    Next fil
End Sub


Reply ↓  Report •

#7
October 30, 2017 at 07:34:15
Still missing double quote?

How To Ask Questions The Smart Way


Reply ↓  Report •

#8
October 30, 2017 at 07:35:00
@AlwaysWillingToLearn thanks for your help

i am not able to use VB is there any script like drag and drop

plz


Reply ↓  Report •

#9
October 30, 2017 at 07:39:51
Hi razor,

Yup cant get that in the list, just doesn't like it, any ideas?

EDIT: have added it in.

Sub RenameTextFile4e()
    Const SpecialCharacters As String = "\,/,:,*,?,<,>,|,""," ' Modify this as neccesary
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fso, MyFile, FileName, TextLine
    Dim char As Variant
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set fol = fso.getfolder("C:\FolderPath\")
            
    For Each fil In fol.Files
          
        FileName = fil

        Set MyFile = fso.OpenTextFile(FileName, ForReading)
    
        Do While MyFile.AtEndOfStream <> True
        
            TextLine = MyFile.ReadLine
            MyFile.Close
            
            For Each char In Split(SpecialCharacters, ",")
                TextLine = Replace(TextLine, char, "")
            Next
            
            fil.Name = TextLine & ".txt"
            Exit Do
            
        Loop

        MyFile.Close
    Next fil
End Sub

Unfortunately VBS or Bath are not within my gift so hopefully someone else will be able to assist you.

message edited by AlwaysWillingToLearn


Reply ↓  Report •

#10
October 30, 2017 at 07:57:20
can i send you the text files and you do the process with screen shots and i do the same..... :(
plz

Reply ↓  Report •

#11
October 30, 2017 at 08:02:34
Quotation marks in VBA can be a pain:

Sub QuotesSuck()
'To show "
 MsgBox "I hate using "" in VBA"
'To show ""
 MsgBox "I hate using """" in VBA"
'To show """ (A quotation mark inside quotation marks)
 MsgBox "I hate using """""" in VBA"
End Sub

How To Post Data or Code ---> Click Here Before Posting Data or VBA Code


Reply ↓  Report •

#12
October 30, 2017 at 08:08:21
Sofia,

I can try it for you, if you PM me by clicking on my name in blue, then when the new page open click on my name in white (top left) you can send me a Private Message and I will send you the email address to where you can send the files. I will also create an Excel workbook for you..


Reply ↓  Report •

#13
October 30, 2017 at 09:41:32
Sofia,

You have sent me upwards of 10 messages via private message with links to your files. I have told you three times now DO NOT send me PM’s with your files, i have provided an email address please use that. I have also responded to one of your PM’s with the results from a test run of my code against your file. Please check. Any further PM’s will be deleted and ignored.


Reply ↓  Report •

#14
October 30, 2017 at 09:46:06
What do the first lines look like, out of curiosity?

How To Ask Questions The Smart Way


Reply ↓  Report •

#15
October 30, 2017 at 09:57:48
Looks like arabic to me, but when i save the text as the filename, it changes from arabic to something i dont quite understand.

The site sees the content of the file as spam, can i PM you it if thats ok as i cannot paste it here

Unicode?

message edited by AlwaysWillingToLearn


Reply ↓  Report •

#16
October 30, 2017 at 10:01:08
If the site's mangling it, I doubt I'm going to get much out of it. Are we still assuming the first line is just the expected file name?

How To Ask Questions The Smart Way


Reply ↓  Report •

#17
October 30, 2017 at 10:06:14
Yesthe first line is expected to be the filename, iv got it working with the code above, although i think (and i am not sure) that once you save the code into the textfile it changes it to arabic, as in unicode conversion (not sure if this is acurrate) but the copied text then changes when you copy paste it as the filename, iv sent it to you as isfrom Sofia, be warned she DOES not read her PMs and is still sending me files so iv just deleted them.

Reply ↓  Report •

#18
October 30, 2017 at 10:25:37
Well, non-English sucks, 'cause things get messy with [VB | VBA | VBS] and localization, so there's no guarantee anything we provide will work for a non-English install of Windows.

Anyways, here's the cut down VBScript version of your code:

Const dirPath = "C:\FolderPath"
For Each fil In CreateObject("Scripting.FileSystemObject").GetFolder(dirPath).Files
  If fil <> Wscript.ScriptFullName Then 
    With fil.OpenAsTextStream(1)
      TextLine = Trim(.ReadLine) & ".txt"
    End With
    'Not even going to bother with invalid characters; they change between languages.
    fil.Name = TextLine
  End If
Next 'fil

How To Ask Questions The Smart Way


Reply ↓  Report •

#19
October 30, 2017 at 11:00:27
sorry all for mess i have created.

the text is in Persian.

the renamed file should have the first line of the content as its name


Reply ↓  Report •

#20
October 30, 2017 at 11:12:12
Here in this link you can find the persian text

http://rumibalkhi.com/allama-mohamm...

you can take the text and name it anything you want eg (abc.txt) with unicode encoding

after the process it should rename the text based on its first line.


Reply ↓  Report •

#21
October 31, 2017 at 09:28:13
@AlwaysWillingToLearn

as per your suggestion i did everything but its not working

:(


Reply ↓  Report •

#22
October 31, 2017 at 09:38:14
What isnt working? Iv sent you a workbook on which iv placed a command button.

When you click on that command button it will ask you for the root folder of where all your files are stored, you seletcthat folder and the code should rename all the files according to the first line of the file.

What happens when you the code?

Explain in detail please


Reply ↓  Report •

#23
October 31, 2017 at 10:56:29
I first of all open the workbook, then from view i run macros but when click brows button it is not going anywhere. i use office 2013

Reply ↓  Report •

#24
October 31, 2017 at 11:08:43
Great It worked for English Version But It mangles the Persian Version. Any suggestion with Persian As Well

Reply ↓  Report •

#25
October 31, 2017 at 11:44:07
Dear @AlwaysWillingToLearn

I thank you very much for your very kind help, Your workbook did what i needed. once again I thank you. You are very helpful and kind. Thank you

Best Regards


Reply ↓  Report •

#26
November 1, 2017 at 01:23:43
✔ Best Answer
Hi Sofia,

That is brilliant I am glad it worked for you, please can you mark this thread and solved by selecting a best answer.

Many thanks,


Reply ↓  Report •

Ask Question