Watercooler

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

+
0 Votes
Locked

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

nigel.baldry
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.
+
0 Votes
john.a.wills

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

+
0 Votes
Slayer_

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.

+
0 Votes
nigel.baldry

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.

+
0 Votes
Slayer_

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?

+
0 Votes
nigel.baldry

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.

+
0 Votes
Slayer_

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

+
0 Votes
neilb@uk

@ 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...

+
0 Votes
Slayer_

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

+
0 Votes
neilb@uk

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.

+
0 Votes
Slayer_

But if it is the extension, then your right, it would have been better :)

But anyways, I was initially trying to give him a solution that he could use and modify himself.

+
0 Votes
nigel.baldry

Hi

Thanks for your help. The VB script works a treat. I'll have a play with the .bat script when I have more time. I do use .bat files for some other tasks, and I've started to use VBS in Access, I do find VBS easier to follow, although it does tend to be a bit more long winded.

+
0 Votes
Slayer_

people just mostly don't because we try and follow programing and readability standards.

This code should do the exact same thing, but it is impossibly unreadable. But it's only 2 lines of code.

Set FSO = CreateObject("Scripting.FileSystemObject")
For Each file In FSO.GetFolder("C:\Users\trevord\Desktop\Temp Export\").Files:file.move(file.ParentFolder & "\" & Left(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(file.OpenAsTextStream.ReadLine, "\", ""), "/", ""), ".", ""), ">", ""), "<", ""), "?", ""), ":", ""), "*", ""), "|", ""), Chr(34), ""), instr(1,Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(file.OpenAsTextStream.ReadLine, "\", ""), "/", ""), ".", ""), ">", ""), "<", ""), "?", ""), ":", ""), "*", ""), "|", ""), Chr(34), ""),"-") -1) & right(file.Name, len(file.Name) - InStrRev(file.Name, ".")+1)):Next