Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: edit a text file in numerous folders  (Read 7193 times)

0 Members and 1 Guest are viewing this topic.

mrwonker

    Topic Starter


    Rookie

    edit a text file in numerous folders
    « on: April 03, 2019, 09:46:28 PM »
    Hello,
    I have a folder, in that folder there are numerous other folders with random names, inside each of these folders there is a file called movie.nfo in that text file there is a line that says <title>random text</title> I was wondering if it would be possible to create a batch script that could be run from the parent directory containing the random folders for it to edit these text files and replace random text with the name of the folder they are contained within, would save me some serious time if this could be done so would be grateful for any help given.
    Thanks.

    Blisk



      Intermediate

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      Re: edit a text file in numerous folders
      « Reply #1 on: April 04, 2019, 02:55:24 AM »
      if you need to do that only once you can use notepad++
      with that you can change the same string in multiple files.
      If not you can use a little program called fart.
      And maybe use it in bach

      mrwonker

        Topic Starter


        Rookie

        Re: edit a text file in numerous folders
        « Reply #2 on: April 04, 2019, 05:55:18 AM »
        Thanks for your reply Blisk, I have downloaded notepad++ and looks like this could do what I'm wanting if I just knew what phrase to use to call the parent folder name in the renaming process, any ideas?

        mrwonker

          Topic Starter


          Rookie

          Re: edit a text file in numerous folders
          « Reply #3 on: April 05, 2019, 06:03:57 PM »
          Hi, I'm still stuck on this problem I've spent I don't know how may hours trying to figure a solution to no avail, I've created scripts in the past but always with help as I don't pretend to know much of the language needed, I try and learn but the fequency that I actually need to make a script means I've forgoten half of what I've learned by the time it comes to need to write another, I've tried using notepad++ as suggested but don't think there's anyway to call the parent folder for the rename process, there's basically 3 things I need to achieve

          1. method to open movie.nfo recursively through numerous subfolders
          2. use a find and replace string for the text inside the movie.nfo files and
          3. be able to call the parent folder of the movie.nfo file to be used in the replace string

          surely that these 3 things must be achievable using a batch or vbs script?

          I've read that there could be a problem using a batch file in that the text I'm trying to find includes < and > which I understand these are special characters that will cause the script to do something other than intended but also read that you can use ^ before special characters to stop this? I know how to use find and replace using a vbs script and that's fine in replacing <title>random text</title> but don't know how to do 1. and 3.

          please someone help I'm pulling my hair out on this one!

          Salmon Trout

          • Guest
          Re: edit a text file in numerous folders
          « Reply #4 on: April 06, 2019, 01:57:13 AM »
          Can you clarify one thing? In each subfolder you have exactly one movie.nfo file, and at the end you still want exactly one movie.nfo file, and everything in that file is the same as it was before, except that any line like this <title>blablabla</title> you want to change to <title>sub folder name</title> . Is that right. OK two things. Is that just the folder name or the whole path?

          Example

          In a folder called sub101 you have movie.nfo, do you want to see this? <title>sub101</title>   ? Is that correct?

          OK three things. Do you want the old movie.nfo to just vanish, or be renamed, e.g. movie.nfo.old ?

          « Last Edit: April 06, 2019, 02:22:26 AM by Salmon Trout »

          Salmon Trout

          • Guest
          Re: edit a text file in numerous folders
          « Reply #5 on: April 06, 2019, 02:40:51 AM »
          Fourth question: You have a main folder, and under this are a bunch of (let's face it!) folders with movies in them. Is each movie.nfo file in a folder one level below the main folder?

          mrwonker

            Topic Starter


            Rookie

            Re: edit a text file in numerous folders
            « Reply #6 on: April 06, 2019, 04:48:35 AM »
            Thanks for your Reply Salmon Trout in response to your first reply you've interpreted it exactly correct in what I'm trying to do and regards to the old movie.nfo file yes I'd be happy for that to dissappear, overwritten etc and regarding 'Fourth question: You have a main folder, and under this are a bunch of (let's face it!) folders with movies in them. Is each movie.nfo file in a folder one level below the main folder?' yes that is also absolutely correct, thank you for your time, I do hope you can help (fingers crossed)

            Salmon Trout

            • Guest
            Re: edit a text file in numerous folders
            « Reply #7 on: April 06, 2019, 06:04:21 AM »
            I am going to use an example movie.nfo I found on the Kodi wiki on this page

            https://kodi.wiki/view/NFO_files/Movies

            I hope this is the right format?





            Salmon Trout

            • Guest
            Re: edit a text file in numerous folders
            « Reply #8 on: April 06, 2019, 09:04:58 AM »
            File movie.nfo in subfolder sub101

            1. Before


            2. After

            mrwonker

              Topic Starter


              Rookie

              Re: edit a text file in numerous folders
              « Reply #9 on: April 06, 2019, 09:50:28 AM »
              Yes Salmon Trout that’s precisely what I want to achieve.

              Salmon Trout

              • Guest
              Re: edit a text file in numerous folders
              « Reply #10 on: April 06, 2019, 12:43:57 PM »
              I made 20 subfolders each with the same movie.nfo, and ran the batch below. It is a bit slow, even on an SSD. It has to process every line of each movie.nfo, and takes about 20 seconds each time on my system. 20 files took 8 minutes. Put the batch script in the folder above the subfolders.

              I suggest you try it with a test folder first in case it trashes all your nfo files!!!! Also test the movie.nfo with whatever app you are using them for.

              @echo off
              setlocal enabledelayedexpansion
              set thisfolder="%~dp0"
              echo.
              cd /d "%thisfolder%"
              set nfos=0
              echo Folder: %thisfolder%
              for /f "delims=" %%A in ('dir /b /ad') do if exist "%%A\movie.nfo" set /a nfos+=1
              echo Found %nfos% movie.nfo file(s) under this folder   
              echo Press a key to process files
              echo or CTRL + C to abort
              pause
              echo %date% %time% Started
              for /f "delims=" %%A in ('dir /b /ad') do (
                 echo !date! !time! Checking folder %%A
                 if exist "%%A\movie.nfo" (
                    echo !date! !time! Found movie.nfo: YES
                    cd "%%A"
                    if exist movie.nfo.new del movie.nfo.new
                    for /f "delims=" %%B in (movie.nfo) do (
                       echo "%%B" | find "<title>" | find "</title>" >nul && (
                          echo !date! !time! Old: %%B
                          for /f "tokens=1-4 delims=^<^>" %%C in ("%%B") do (
                             echo %%C^<title^>%%A^</title^> >> movie.nfo.new
                             echo !date! !time! New: %%C^<title^>%%A^</title^>
                             )
                          ) || (
                          echo %%B >> movie.nfo.new
                          )
                       )
                    del movie.nfo
                    ren movie.nfo.new movie.nfo
                    cd "%thisfolder%"
                    ) else (
                    echo !date! !time! Found movie.nfo: NO
                    )
                 )
              echo %date% %time% Finished - press a key to close
              pause


               


              mrwonker

                Topic Starter


                Rookie

                Re: edit a text file in numerous folders
                « Reply #11 on: April 06, 2019, 01:17:48 PM »
                Thanks so much Salmon, I’m out at the minute so will be a few hours before can try this out, but can’t wait to give it a go, the time it takes to process isn’t a big issue so that should be fine, I’ll report back when I’ve tried it.

                mrwonker

                  Topic Starter


                  Rookie

                  Re: edit a text file in numerous folders
                  « Reply #12 on: April 07, 2019, 08:37:23 AM »
                  Thanks Salmon script works perfectly really appreciate your time in helping me with this, I did have an issue when first tried the script the script was escaping at the point where it says press key to continue or ctrl c to abort, I moved test folder to C: drive and it worked, thought it might have been due to security setting which did differ on the other drive so altered them and still no good on original test folder, tried it on another drive that worked, anyway to cut a long story short after a bit of head scratching worked out the reason was due to the folder was running it from had () in the title 'Movies (1)' on renaming the folder to 'Movies 1' the script worked, you'll probably understand the reson why the brackets in the folder name stopped it running but it really doesn't matter to me, I don't need the parent folder to contain brackets so all is good.
                   :) :) :) 

                  Salmon Trout

                  • Guest
                  Re: edit a text file in numerous folders
                  « Reply #13 on: April 07, 2019, 09:19:15 AM »
                  Glad it worked! Is this just a one-time thing, or are you going to be doing this a lot? Because I have been working on a much faster version using VBScript.

                  Salmon Trout

                  • Guest
                  Re: edit a text file in numerous folders
                  « Reply #14 on: April 07, 2019, 02:12:44 PM »
                  My VBscript processed the same 20 files in 0.6 seconds. As before, try it out on test files/folders first!!! Place as a .vbs file in the top folder (like the .bat) and you can double click it in Windows Explorer. You should not see a console window, just 2 message boxes.

                  Const ForWriting = 2
                  Set objFSO = CreateObject("Scripting.FileSystemObject")
                  set objShell = createobject("wscript.shell")
                  strScriptDir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)

                  iInfoCount = 0
                  For Each objFolder In objFSO.GetFolder(strScriptDir).SubFolders
                     aFilePath        = split(objFolder.Path, "\")
                     strSubFolderName = aFilePath(Ubound(aFilePath))
                     strNFOpath       = objFolder.Path & "\" & "movie.nfo"
                     If objFSO.FileExists(strNFOpath) Then
                        iInfoCount = iInfoCount + 1
                     End If
                  Next
                  v = msgbox("Found " & iInfoCount & " movie.nfo file(s)" & vbcrlf & vbcrlf & "Click OK to start or Cancel to quit", 1, "Movie.nfo processor")

                  if v = 2 Then
                     wscript.quit
                  End If

                  st = timer
                  iFiles = 0
                  For Each objFolder In objFSO.GetFolder(strScriptDir).SubFolders
                     aFilePath        = split(objFolder.Path, "\")
                     strSubFolderName = aFilePath(Ubound(aFilePath))
                     strNFOpath       = objFolder.Path & "\" & "movie.nfo"
                     If objFSO.FileExists(strNFOpath) Then
                        set nfoFile = objFSO.OpenTextFile(strNFOpath, 1)
                        content = nfoFile.ReadAll
                        aNFOfileLines = split(content, vbcrlf)
                        For iLine = 0 To Ubound(aNFOfileLines)
                              strThisLine = aNFOfileLines(iLine)
                              If instr(strThisLine, "<title>") > 0 Then
                                 If instr(strThisLine, "</title>") > 0 Then
                                    strLineIndent = Space(instr(strThisLine, "<title>")-1)
                                    strOldLine = strThisLine
                                    strNewLine = "<title>" & strSubFolderName & "</title>"
                                    aNFOfileLines(iLine) = strLineIndent & strNewLine
                                    Exit For
                                 End If
                              End If
                        Next
                        nfoFile.close
                        objShell.currentdirectory = objFolder
                        Set objTextFile = objFSO.OpenTextFile ("movie.nfo.new", ForWriting, True)
                        For iLine = 0 To Ubound(aNFOfileLines)
                           objTextFile.WriteLine(aNFOfileLines(iLine))
                        Next
                        objTextFile.Close
                        objFSO.DeleteFile "movie.nfo"
                        objFso.MoveFile "movie.nfo.new", "movie.nfo"
                        objShell.currentdirectory = strScriptDir
                        iFiles = iFiles + 1
                     End If
                  Next
                  et = timer
                  strMsg = "Processed " & iFiles & " file(s)" & vbcrlf & vbcrlf & "in " & et-st & " seconds"
                  v = msgbox(strMsg, 0, "Movie.nfo processor")