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

Author Topic: .bat or .vbs Find within text and Rename File  (Read 10905 times)

0 Members and 1 Guest are viewing this topic.

ghostdog74



    Specialist

    Thanked: 27
    Re: .bat or .vbs Find within text and Rename File
    « Reply #15 on: September 01, 2010, 08:00:45 AM »
    You are not using the amended code! You are using my old code with YOUR OWN amendments. Gosh..

    jmituzas

      Topic Starter


      Greenhorn

      Re: .bat or .vbs Find within text and Rename File
      « Reply #16 on: September 01, 2010, 08:06:32 AM »
      I liked ghostdog74 response because after discussing with our CIO I have found that he wanted this in .vbs and after searching some he informed me that there was another way of doing this since we have our .txt file as a delimited text, we can count tabs to find the RXC number.

      Here is an example of what I have found from http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24081716.html

      I have already edited this script to point to the group number second line 4th tab:

         Call TwoDimensionArrayTest
       
      Sub TwoDimensionArrayTest
      ' Version 1.0
      ' Writtem by Krystian Kara
      ' Dated 25-Jan-2009
       
       
         Dim fso
         Dim oFile
         Dim arrline
         Dim arrItem
         Dim i
         Dim arrMain()
         Dim sFileLocation, strResults
         
         Const forReading = 1
         
      ' The file contains on each line:
         ' Text1 (tab) Text2 (tab) Text3 (tab) Text4
         ' Text5 (tab) Text6 (tab) Text7 (tab) Text8
      'etc etc
       
       
         Set fso = CreateObject("Scripting.FileSystemObject")
            sFileLocation = "C:\ArrayTest.txt"
            
            Set oFile = fso.OpenTextFile(sFileLocation, forReading, False)
            
         Do While oFile.AtEndOfStream <> True
            strResults = oFile.ReadAll
         Loop
         
      ' Close the file
         oFile.Close
         
      ' Release the object from memory
         Set oFile = Nothing
         
      ' Return the contents of the file if not Empty
         If Trim(strResults) <> "" Then
            
            ' Create an Array of the Text File
            arrline = Split(strResults, vbNewLine)
         End If
       
         For i = 0 To UBound(arrline)
            If arrline(i) = "" Then
               ' checks for a blank line at the end of stream
               Exit For
            End If
            
            ReDim Preserve arrMain(i)
            
               arrMain(i) = Split(arrline(i), vbTab)
            
               WScript.Echo i & ",0 contains: " & arrMain(i)(0)
               WScript.Echo i & ",1 contains: " & arrMain(i)(1)

         Next
       
         WScript.Echo  arrMain(1)(3)
       
      End Sub ' TwoDimensionArrayTest



      Only thing I need help with is it prompts for what it finds (I don't need or want a prompt window) and It does not rename the file from what it found nor do I think that this script will go through all the .txt files in the directory. Well at least now we know what we are looking for.

      Sorry for not specifying in original question but thanks for your posts.

      jmituzas

        Topic Starter


        Greenhorn

        Re: .bat or .vbs Find within text and Rename File
        « Reply #17 on: September 01, 2010, 09:37:28 AM »
        How do we test the code?

        Where is sample of the output? 

        I guess I don't have a sample of output because it doesn't output correctly.
        There are no errors just echos to screen not to file.

        I can give you this from my txt file I pointed this script to:
        here is my "File 2.txt" that hopefully I can get renamed to the RXC# :

        @Program   @RxBIN   @RXPCN   @RxGroup   @MemberID   @WebsiteE   @WebsiteS   @VerticalLogo   @TextLogo                     
        RXCUT   013824   RXCUT   RXC193   RXC5FHXF9   www.somesite    www.somesite       P:\RxCut\In Design Implementation\RXC193                        

        Thanks in advance,
        joe

        jmituzas

          Topic Starter


          Greenhorn

          Re: .bat or .vbs Find within text and Rename File
          « Reply #18 on: September 01, 2010, 11:06:32 AM »
          ok have a working script without those prompts!  ;D

          here it is with improvements:

             Call TwoDimensionArrayTest
           
          Sub TwoDimensionArrayTest
          ' Version 1.0
          ' Writtem by Krystian Kara
          ' Dated 25-Jan-2009
           
           
             Dim fso
             Dim oFile
             Dim arrline
             Dim arrItem
             Dim objFolder
             Dim i
             Dim arrMain()
             Dim sFileLocation, strResults
             
             Const forReading = 1
             
          ' The file contains on each line:
             ' Text1 (tab) Text2 (tab) Text3 (tab) Text4
             ' Text5 (tab) Text6 (tab) Text7 (tab) Text8
          'etc etc
           
           
             Set fso = CreateObject("Scripting.FileSystemObject")
                sFileLocation = "file 2.txt"
                
                Set oFile = fso.OpenTextFile(sFileLocation, forReading, False)
                
             Do While oFile.AtEndOfStream <> True
                strResults = oFile.ReadAll
             Loop
             
          ' Close the file
             oFile.Close
             
          ' Release the object from memory
             Set oFile = Nothing
             
          ' Return the contents of the file if not Empty
             If Trim(strResults) <> "" Then
                
                ' Create an Array of the Text File
                arrline = Split(strResults, vbNewLine)
             End If
           
             For i = 0 To UBound(arrline)
                If arrline(i) = "" Then
                   ' checks for a blank line at the end of stream
                   Exit For
                End If
                
                ReDim Preserve arrMain(i)
                
                   arrMain(i) = Split(arrline(i), vbTab)
                
             Next
           
             fso.MoveFile sFileLocation, arrMain(1)(3) & ".txt"
           
          End Sub ' TwoDimensionArrayTest



          Need help making it loop through the directory for all .txt files and running this script against them all.

          Please help,

          Thanks in advance,
          Joe
          « Last Edit: September 01, 2010, 12:07:05 PM by jmituzas »

          jmituzas

            Topic Starter


            Greenhorn

            Re: .bat or .vbs Find within text and Rename File
            « Reply #19 on: September 03, 2010, 10:31:54 AM »
            All fixed heres the final error free code:


               Call TwoDimensionArrayTest
            Sub TwoDimensionArrayTest

            Dim fso
               Dim oFile
               Dim arrline
               Dim arrItem
               Dim i
               Dim arrMain()
               Dim sFileLocation, strResults

               Const forReading = 1
               
            strFolder = "C:\Documents and Settings\jmituzas.NMCLLC\Desktop\desktop2\New Folder (2)\datafiles"
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            For Each objFile In objFSO.GetFolder(strFolder).Files
               If Right(LCase(objFile.Name), 4) = LCase(".txt") Then
               
                  ' The file contains on each line:
               ' Text1 (tab) Text2 (tab) Text3 (tab) Text4
               ' Text5 (tab) Text6 (tab) Text7 (tab) Text8
            'etc etc

            Set fso = CreateObject("Scripting.FileSystemObject")
                  sFileLocation = objFile.Name
                  
                  Set oFile = fso.OpenTextFile(objFile.Name, forReading, False)
                  
               Do While oFile.AtEndOfStream <> True
                  strResults = oFile.ReadAll
               Loop
               
               ' Close the file
               oFile.Close
               
            ' Release the object from memory
               Set oFile = Nothing
               
            ' Return the contents of the file if not Empty
               If Trim(strResults) <> "" Then
                  
                  ' Create an Array of the Text File
                  arrline = Split(strResults, vbNewLine)
               End If
             
               For i = 0 To UBound(arrline)
                  If arrline(i) = "" Then
                     ' checks for a blank line at the end of stream
                     Exit For
                  End If
                  
                  ReDim Preserve arrMain(i)
                  
                     arrMain(i) = Split(arrline(i), vbTab)

               Next
             
                  fso.MoveFile sFileLocation, arrMain(1)(3) & ".txt"
             End If
             Next
            End Sub ' TwoDimensionArrayTest