General discussion

Locked

batch script to rename .txt file using the first line of the file

By nigel.baldry ·
Tags: Off Topic
Hi

I have a number of text files which are named nnn.txt, I want to rename them using the first line of the text file. ie 123.txt becomes my new name.txt. Can anyone help.

This conversation is currently closed to new comments.

13 total posts (Page 1 of 2)   01 | 02   Next
| Thread display: Collapse - | Expand +

All Comments

Collapse -

wrong forum

by john.a.wills In reply to batch script to rename .t ...

This belongs in Q & A, not the water cooler. Also, I think you have written "ie" where you meant "e.g.".

Collapse -

I dont think this is a simple task in DOS

by Slayer_ In reply to batch script to rename .t ...

You might be better off using VBScript and loop through the files using the FileSystemObject which has the ability to read a line of test from the file as well as rename the file.

Collapse -

batch script to rename .txt file using the first line of the file

by nigel.baldry In reply to I dont think this is a si ...

John
Sorry for posting in wrong forum, it's my first posting, I realised afterwrds that it was in the wrong place but could not find how to move it.

Slayer
Thanks for your replies. I was looking at a batch file as i know you can rename using these and having googled my enquiry it looked like you can get the first line of a file using a batch script. Being a novice I did not know how to put the two togther. I wouldn't even know where to start with a FileSystemObject but would be prepared to give it a go with some guidance.

Collapse -

Sure, Like this I think

by Slayer_ In reply to batch script to rename .t ...

Make a file and give it a .vbs extension.
And put this in there. It should get you started.

Dim FSO
Dim file
Dim firstline
Dim extension
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each file In FSO.GetFolder("FolderPath").Files
extension = right(file.Name, len(file.Name) - InStrRev(file.Name, ".")+1) 'store the file extension
firstline = file.OpenAsTextStream.ReadLine
'remove bad characters that can ruin a file name
firstline = Replace(firstline, "\", "")
firstline = Replace(firstline, "/", "")
firstline = Replace(firstline, ".", "")
firstline = Replace(firstline, ">", "")
firstline = Replace(firstline, "<", "")
firstline = Replace(firstline, "?", "")
firstline = Replace(firstline, ":", "")
firstline = Replace(firstline, "*", "")
firstline = Replace(firstline, "|", "")
firstline = Replace(firstline, Chr(34), "") 'Search for quotes
firstline = Left(firstline, 60) 'max file name length, can be adjusted
file.move(file.ParentFolder & "\" & firstline & extension)
Next


This code seems to work, however, if the first line of the file has some non character data in it, the file extension seems to get omitted. I'm not sure why, maybe someone else here can assist?

Collapse -

batch script to rename .txt file using the first line of the file

by nigel.baldry In reply to Sure, Like this I think

Hi Slayer

Thanks very much. That works a treat, however, my vbs got renamed as well as I had placed in in the folder with the txt files.

If it is possible to do a tweak then it would be perfect. the first line of text as an "-" in it then a bit more text. I only need the text before the "-". is this possible, if not, I can live with what you have given me.

Collapse -

Deleted by author.

by CharlieSpencer In reply to batch script to rename .t ...
Collapse -

SO the inside line looks like

by Slayer_ In reply to batch script to rename .t ...

This is some text - This is some more text

Like that?

TO do that, change the left(text,60) to be an instr instead (instr stands for "in string")


Dim FSO
Dim file
Dim firstline
Dim extension
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each file In FSO.GetFolder("FolderPath").Files
extension = right(file.Name, len(file.Name) - InStrRev(file.Name, ".")+1) 'store the file extension
firstline = file.OpenAsTextStream.ReadLine
'remove bad characters that can ruin a file name
firstline = Replace(firstline, "\", "")
firstline = Replace(firstline, "/", "")
firstline = Replace(firstline, ".", "")
firstline = Replace(firstline, ">", "")
firstline = Replace(firstline, "<", "")
firstline = Replace(firstline, "?", "")
firstline = Replace(firstline, ":", "")
firstline = Replace(firstline, "*", "")
firstline = Replace(firstline, "|", "")
firstline = Replace(firstline, Chr(34), "") 'Search for quotes
if instr(1,firstline,"-") > 0 then 'if there are no dashes, the next line will error
firstline = Left(firstline, instr(1,firstline,"-") -1) 'max file name length, can be adjusted
end if
file.move(file.ParentFolder & "\" & firstline & extension)
Next

Collapse -

Do it in a batch file

by neilb@uk In reply to batch script to rename .t ...

@ echo off
SetLocal ENABLEDELAYEDEXPANSION
for %%i in (.\*.txt) do (
set /p var= <<"%%i"
ren %%i "!var!.txt"
)
Athough you can't see it, there is a less-than chevron before "%%i" where we set the variable value

Or use PowerShell
Get-childitem path_to_files\*.txt | %{rename-item $_.fullname $_.name.replace($_.basename,(get-content $_.fullname)[0])}

Powershell up to the "-"
Get-childitem path_to_files\*.txt | %{rename-item $_.fullname $_.name.replace($_.basename,(get-content $_.fullname)[0].split("-")[0])}

vbscript is dead...

Collapse -

Too bad cause

by Slayer_ In reply to Do it in a batch file

The vbScript I wrote is a 1000 times easier to read and modify.

Collapse -

The PowerShell is one line

by neilb@uk In reply to Too bad cause

Yes, I could have written an easy-to-read and easy-to-modify version of a half dozen lines - but I didn't have to.

Oh, and "right(file.Name, len(file.Name) - InStrRev(file.Name, ".")+1) " is a thousand times easier to read and modify than the PoSh one-liner?

Of course it is...

Though you could have used the file object's 'type' property to get the extension in your vbscript and you wouldn't need that line, anyway.

Back to After Hours Forum
13 total posts (Page 1 of 2)   01 | 02   Next

Off-Topic Forums