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 7175 times)

0 Members and 1 Guest are viewing this topic.

Salmon Trout

  • Guest
Re: edit a text file in numerous folders
« Reply #15 on: April 07, 2019, 03:53:15 PM »
My VBscript processed the same 20 files in 0.6 seconds.

Tried it again and got 0.34 seconds.

mrwonker

    Topic Starter


    Rookie

    Re: edit a text file in numerous folders
    « Reply #16 on: April 12, 2019, 03:58:57 AM »
    Hi Salmon,
    Sorry only just seen your further input to this post, you really are a star, I've tried the VBScript and yea it's super quick, whilst I said it didn't matter about the speed, It actually is going to be much better for me using the VBScript given the processing speed, I really can't thank you enough.  :) :) :)

    Salmon Trout

    • Guest
    Re: edit a text file in numerous folders
    « Reply #17 on: April 12, 2019, 12:47:44 PM »
    Here is a new version. You can store it anywhere, it does not have to be in the parent folder of the movie folders. When you run the script it shows the Windows 'browse for folder' dialog and you can use that to find the folder you want to scan. Then it works the same as before.



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

    'strScanDir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)

    strPromptString = "Choose folder to scan (click Cancel to quit)"
    strScanDir = BrowseFolder( "My Computer", False)
    if lcase(strScanDir) = "quit" Then
        wscript.quit
    End If

    iInfoCount = 0
    For Each objFolder In objFSO.GetFolder(strScanDir).SubFolders
       aFilePath        = split(objFolder.Path, "\")
       strSubFolderName = aFilePath(Ubound(aFilePath))
       strNFOpath       = objFolder.Path & "\" & "movie.nfo"
       If objFSO.FileExists(strNFOpath) Then
          strMessage = "yes"
          iInfoCount = iInfoCount + 1
       Else
          strMessage = "no"
       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(strScanDir).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 = strScanDir
          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")

    Function BrowseFolder( myStartLocation, blnSimpleDialog )
    ' This function generates a Browse Folder dialog
    ' and returns the selected folder as a string.
    '
    ' Arguments:
    ' myStartLocation   [string]  start folder for dialog, or "My Computer", or
    '                             empty string to open in "Desktop\My Documents"
    ' blnSimpleDialog   [boolean] if False, an additional text field will be
    '                             displayed where the folder can be selected
    '                             by typing the fully qualified path
    '
    ' Returns:          [string]  the fully qualified path to the selected folder
    '
    ' Based on the Hey Scripting Guys article
    ' "How Can I Show Users a Dialog Box That Only Lets Them Select Folders?"
    ' http://www.microsoft.com/technet/scriptcenter/resources/qanda/jun05/hey0617.mspx
    '
    ' Function written by Rob van der Woude
    ' http://www.robvanderwoude.com
        Const MY_COMPUTER   = &H11&
        Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0

        Dim numOptions, objFolder, objFolderItem
        Dim objPath, objShell, strPath, strPrompt

        ' Set the options for the dialog window

        strPrompt = strPromptString
        If blnSimpleDialog = True Then
            numOptions = 0      ' Simple dialog
        Else
            numOptions = &H10&  ' Additional text field to type folder path
        End If

        ' Create a Windows Shell object
        Set objShell = CreateObject( "Shell.Application" )

        ' If specified, convert "My Computer" to a valid
        ' path for the Windows Shell's BrowseFolder method
        If UCase( myStartLocation ) = "MY COMPUTER" Then
            Set objFolder = objShell.Namespace( MY_COMPUTER )
            Set objFolderItem = objFolder.Self
            strPath = objFolderItem.Path
        Else
            strPath = myStartLocation
        End If

        Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, _
                                                  numOptions, strPath )

        ' Quit if no folder was selected
        If objFolder Is Nothing Then
            BrowseFolder = "quit"
            Exit Function
        End If

        ' Retrieve the path of the selected folder
        Set objFolderItem = objFolder.Self
        objPath = objFolderItem.Path

        ' Return the path of the selected folder
        BrowseFolder = objPath
    End Function
    « Last Edit: April 12, 2019, 01:41:08 PM by Salmon Trout »

    Salmon Trout

    • Guest
    Re: edit a text file in numerous folders
    « Reply #18 on: April 12, 2019, 01:42:33 PM »
    I have changed the script slightly since I posted it about 1 hour ago.

    mrwonker

      Topic Starter


      Rookie

      Re: edit a text file in numerous folders
      « Reply #19 on: April 15, 2019, 05:13:40 PM »
      Hi, Salmon, sorry, again only just seen you've added further, don't know why I'm not getting notifications of post updates! I have selected to do so, anyway tried out your latest script and it's fantastic, will certainly be useful to have as a main script to be run from anywhere, great stuff, awesome help, awesome site, I can't thank you enough.

      Salmon Trout

      • Guest
      Re: edit a text file in numerous folders
      « Reply #20 on: April 16, 2019, 03:45:17 PM »
      Thank you for taking the time to come back and report. This is what I like about Computer Hope - you sometimes get an interesting script idea, have fun trying to make it work, and it works and is useful to someone, who is kind enough to come back and say so.