General discussion

  • Creator
    Topic
  • #2171009

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

    Locked

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

All Comments

  • Author
    Replies
    • #2887521

      wrong forum

      by john.a.wills ·

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

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

    • #2887517

      I dont think this is a simple task in DOS

      by slayer_ ·

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

      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.

      • #2888481

        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 simple task in DOS

        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.

        • #2888480

          Sure, Like this I think

          by slayer_ ·

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

          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?

        • #2888475

          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.

        • #2888473

          Deleted by author.

          by charliespencer ·

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

          .

        • #2888471

          SO the inside line looks like

          by slayer_ ·

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

          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

    • #2888401

      Do it in a batch file

      by neilb@uk ·

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

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

      • #2888370

        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.

        • #2888355

          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.

        • #2888350

          I thought type returned the description

          by slayer_ ·

          In reply to The PowerShell is one line

          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.

        • #2431447

          Renaming file

          by nigel.baldry ·

          In reply to I thought type returned the description

          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.

        • #2431971

          Well you can condense VBS almost as bad as you can in DOS scripts

          by slayer_ ·

          In reply to Renaming file

          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

Viewing 2 reply threads