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

0 Members and 1 Guest are viewing this topic.

jmituzas

    Topic Starter


    Greenhorn

    .bat or .vbs Find within text and Rename File
    « on: August 31, 2010, 12:56:20 PM »
    I have outputted some text files all in the same directory. Each .txt file has within a group number, this number always starts with RXC and can go upwards of 5 characters afterwards, giving us RXCXXXXX i need the script to find this RXC number and rename the file to its corresponding group number, then do the same for all files in the same directory.

    would really like this to be a .bat but a .vbs will also work.

    Thanks in advance,
    Joe

    Fields



      Beginner

      Thanked: 3
      Re: .bat or .vbs Find within text and Rename File
      « Reply #1 on: August 31, 2010, 02:37:26 PM »
      number always starts with RXC and can go upwards of 5 characters afterwards, giving us RXCXXXXXi

      C:test>type  rxc.bat
      @echo  off

      rem dir /b rxc*  >>  rxc.txt
      echo. >> rxc.txt
      type rxc.txt

      setlocal enabledelayedexpansion
      for /f  delims= %%i in (rxc.txt) do (
      set x=%%i
      set  x=!x:~3!
      echo !x!
      rem copy %%i  !x!
      rem ren %%i  !x!
      )
      Output:

      C:test>rxc.bat
      rcx12345
      rcx23456
      rcx34567

      12345
      23456
      34567


      C:test>
      Member of the Human Race; Citizen of the World.

      T.C.



        Beginner

        Thanked: 13
        Re: .bat or .vbs Find within text and Rename File
        « Reply #2 on: August 31, 2010, 04:10:48 PM »
        Hmmmm!

        Quote from: OP
        I have outputted some text files all in the same directory. Each .txt file has within a group number

        I think this means that the group number (RCXnnnnn) is part of the contents of the text file, in other words is part of the text not of the filename which could be anything..

        ghostdog74



          Specialist

          Thanked: 27
          Re: .bat or .vbs Find within text and Rename File
          « Reply #3 on: August 31, 2010, 05:57:20 PM »
          Code: [Select]
          Set objFS = CreateObject("Scripting.FileSystemObject")
          strFolder="c:\test"
          Set objFolder = objFS.GetFolder(strFolder)
          For Each strFile In objFolder.Files
          If objFS.GetExtensionName(strFile) = "txt" Then    
          strFileName = strFile.Name
          Set objFile = objFS.OpenTextFile(strFileName)
          Do Until objFile.AtEndOfStream
          strLine=objFile.ReadLine
          If InStr(strLine,"RXC" ) > 0 Then
          number=Mid(strLine,4,5) 'Get 5 characters afterwards
          objFile.Close
          strFile.Name = Trim(number)&".txt"
          Exit Do
          End If
          Loop     
          End If
          Next
          « Last Edit: August 31, 2010, 06:30:41 PM by ghostdog74 »

          Fields



            Beginner

            Thanked: 3
            Re: .bat or .vbs Find within text and Rename File
            « Reply #4 on: August 31, 2010, 06:13:31 PM »

            I think this means that the group number (RCXnnnnn) is part of the contents of the text file, in other words is part of the text not of the filename which could be anything.


            C:test>Display    rxc2.bat

            @echo  off
            echo rxc12345 > rxc1.txt
            echo rxc23456 > rxc2.txt
            echo rxc34567 > rxc3.txt

            echo. >  run.txt

            findstr rxc  *.txt >> run.txt

            echo Display  run.txt
            type  run.txt
            echo.

            setlocal enabledelayedexpansion
            rem Will need double quote before skip and after delims=:

            for /f  skip=1 tokens=1,2 delims=: %%i in (run.txt) do (
            set filename=%%i
            set newname=%%j
            echo filename=!filename!
            echo newname=!newname!.txt
            rem copy !filename! !newname!
            rem ren !filename! !newname!
            )
            rem will need double quote around filename and newname for copy or ren

            Output:

            C:test>rxc2.bat
            Display  run.txt

            rxc1.txt:rxc12345
            rxc2.txt:rxc23456
            rxc3.txt:rxc34567

            filename=rxc1.txt
            newname=rxc12345.txt
            filename=rxc2.txt
            newname=rxc23456.txt
            filename=rxc3.txt
            newname=rxc34567.txt
            C:test>
            Member of the Human Race; Citizen of the World.

            ghostdog74



              Specialist

              Thanked: 27
              Re: .bat or .vbs Find within text and Rename File
              « Reply #5 on: August 31, 2010, 06:38:29 PM »
              Code: [Select]
              C:test>Display    rxc2.bat

              @echo  off
              echo rxc12345 > rxc1.txt
              echo rxc23456 > rxc2.txt
              echo rxc34567 > rxc3.txt
              echo. >  run.txt
              findstr rxc  *.txt >> run.txt


              how do you know OP has only RXCXXXXX on a line by itself? He may have a line like this "troll RXC12345 is here".
              and then your script breaks.

              Fields



                Beginner

                Thanked: 3
                Re: .bat or .vbs Find within text and Rename File
                « Reply #6 on: August 31, 2010, 06:52:19 PM »

                How do you know OP has only RXCXXXXX on a line by itself? He may have a line like this nooutput  RXC12345 is here.
                and then your script breaks.


                The tokens and delims can be adjusted to account for  as many tokens as needed,
                We can also use more than one delims.

                for /f  skip=1 tokens=1,2 delims=: %%i in (run.txt) do (



                Member of the Human Race; Citizen of the World.

                ghostdog74



                  Specialist

                  Thanked: 27
                  Re: .bat or .vbs Find within text and Rename File
                  « Reply #7 on: August 31, 2010, 06:58:32 PM »
                  The tokens and delims can be adjusted to account for  as many tokens as needed,
                  We can also use more than one delims.

                  for /f  skip=1 tokens=1,2 delims=: %%i in (run.txt) do (





                  but do you know where RXC12345 will occur in the text? beginning of line? end of line? If it can occur anywhere in the text,  then how do you go about setting your delims and tokens?

                  Fields



                    Beginner

                    Thanked: 3
                    Re: .bat or .vbs Find within text and Rename File
                    « Reply #8 on: August 31, 2010, 07:39:01 PM »
                    But do you know where RXC12345 will occur in the text? beginning of line? end of line? If it can occur anywhere in the text,  then how do you go about setting your delims and tokens?

                    The original code did not fail. There is a longer name for the new file. The adjustments I mentioned will
                    work but not needed now. If OP request the adjustments we will do it.




                    Member of the Human Race; Citizen of the World.

                    ghostdog74



                      Specialist

                      Thanked: 27
                      Re: .bat or .vbs Find within text and Rename File
                      « Reply #9 on: August 31, 2010, 08:03:00 PM »
                      The original code did not fail. There is a longer name for the new file.
                      yes it does. Like your example rxc4.txt,  you now have "nooutput rxc45678 is here" as the new file name. It should be "45678.txt" as the new file name.  Here's a suggestion :

                      Code: [Select]
                      for .... ( txtfile ) do (
                          rem set delim to whitspaces.
                          rem go through all the tokens, look for "RXC",
                          rem once found, get the substring from 4th character onwards.
                          rem create the new file.
                      )

                      Fields



                        Beginner

                        Thanked: 3
                        .bat or .vbs Find within text and Rename File
                        « Reply #10 on: August 31, 2010, 08:52:57 PM »
                        yes it does. Like your example rxc4.txt,  you now have \"nooutput rxc45678 is here\" as the new file name. It should be \"45678.txt\" as the new file name.  Here\'s a suggestion :

                        Code: [Select]
                        for .... ( txtfile ) do (
                            rem set delim to whitspaces.
                            rem go through all the tokens, look for \"RXC\",
                            rem once found, get the substring from 4th character onwards.
                            rem create the new file.
                        )

                        Yes that will work but I will not look at all the tokens until the OP asks for it.

                        The long file name is ok with me.
                         
                        Many OPs ask a question and never return.

                        By the way,  I dont know enough VBS to get your VBS code to run. I have not spent much time with your VBS.  Wll try later.
                        Member of the Human Race; Citizen of the World.

                        ghostdog74



                          Specialist

                          Thanked: 27
                          Re: .bat or .vbs Find within text and Rename File
                          « Reply #11 on: August 31, 2010, 09:05:51 PM »
                          The long file name is ok with me.
                          its not ok as OP's required specs. He wants to find the RXC number following RXC. (without other alphabets/words)
                           
                          Quote
                          By the way,  I dont know enough VBS to get your VBS code to run. I have not spent much time with your VBS.  Wll try later.
                          you don't have to know much to learn how to run a vbscript.
                          Code: [Select]
                          cscript //nologo myscript.vbs

                          Fields



                            Beginner

                            Thanked: 3
                            .bat or .vbs Find within text and Rename File
                            « Reply #12 on: September 01, 2010, 04:22:46 AM »
                            Code: [Select]
                            Set objFS = CreateObject(\"Scripting.FileSystemObject\")


                            I was able to run your vbs file.  The output did not come to the screen  but the vbs did change the names of the original file names.
                            The original file rxc4.txt with text [ nooutput  rxc45678 is here]
                            was changed  to utput.txt  and not to 45678.txt.  This is clearly a flaw with the vbs solution
                            The ops specs were not followed. Please correct


                            C:\\test>type  ghost831.vbs
                            Set objFS = CreateObject(\"Scripting.FileSystemObject\")
                            strFolder=\"c:\\test\"
                            Set objFolder = objFS.GetFolder(strFolder)
                            For Each strFile In objFolder.Files
                                    If objFS.GetExtensionName(strFile) = \"txt\" Then
                                            strFileName = strFile.Name
                                            Set objFile = objFS.OpenTextFile(strFileName)
                                            Do Until objFile.AtEndOfStream
                                                    strLine=objFile.ReadLine
                                                    If InStr(strLine,\"rxc\" ) > 0 Then
                                                            number=Mid(strLine,4,5) \'Get 5 characters afterwards
                                                            objFile.Close
                                                            strFile.Name = Trim(number)&\".txt\"
                                                            Exit Do
                                                    End If
                                            Loop
                                    End If
                            Next

                            Input:

                            C:test>findstr  rxc  *.txt

                            rxc1.txt:rxc12345
                            rxc2.txt:rxc23456
                            rxc3.txt:rxc34567
                            rxc4.txt:nooutput  rxc45678 is here

                            Output:

                            C:\\test>dir  *  |  sort

                            09/01/2010  04:55 AM                11 12345.txt
                            09/01/2010  04:55 AM                11 23456.txt
                            09/01/2010  04:55 AM                11 34567.txt
                            09/01/2010  04:55 AM                29 utput.txt
                            09/01/2010  04:55 AM               101 1.txt.txt

                            C:test>type  12345.txt
                            rxc12345

                            C:test>type  utput.txt
                            nooutput  rxc45678 is here

                            C:test>type  1.txt.txt

                            rxc1.txt:rxc12345
                            rxc2.txt:rxc23456
                            rxc3.txt:rxc34567
                            rxc4.txt:nooutput  rxc45678 is here
                            Member of the Human Race; Citizen of the World.

                            ghostdog74



                              Specialist

                              Thanked: 27
                              Re: .bat or .vbs Find within text and Rename File
                              « Reply #13 on: September 01, 2010, 04:59:10 AM »
                              that's easy to change. So now, are you going to change your batch script to do the same?
                              Code: [Select]
                              Set objFS = CreateObject("Scripting.FileSystemObject")
                              strFolder="c:\test"
                              Set objFolder = objFS.GetFolder(strFolder)
                              For Each strFile In objFolder.Files
                              If objFS.GetExtensionName(strFile) = "txt" Then    
                              strFileName = strFile.Name
                              Set objFile = objFS.OpenTextFile(strFileName)
                              Do Until objFile.AtEndOfStream
                              strLine=objFile.ReadLine
                              m=InStr(strLine,"RXC" )
                              If m > 0 Then
                              number=Mid(strLine,m+3,5)
                              WScript.Echo number
                              objFile.Close
                              strFile.Name = Trim(number)&".txt"
                              Exit Do
                              End If
                              Loop     
                              End If
                              Next

                              Fields



                                Beginner

                                Thanked: 3
                                .bat or .vbs Find within text and Rename File
                                « Reply #14 on: September 01, 2010, 07:24:21 AM »
                                Thats easy to change.

                                Same error as before:  45678.txt is  not created.  utput.txt is created  from the input file rxc4.txt with the
                                line [ nooutput  rxc45678 is here ] inside of the rxc4.txt file.

                                The fix for the ghost.vbs did not work.

                                C:test>Display    ghost901.vbs
                                Set objFS = CreateObject(Scripting.FileSystemObject)
                                strFolder=c:test
                                Set objFolder = objFS.GetFolder(strFolder)
                                For Each strFile In objFolder.Files
                                        If objFS.GetExtensionName(strFile) = txt Then
                                                strFileName = strFile.Name
                                                Set objFile = objFS.OpenTextFile(strFileName)
                                                Do Until objFile.AtEndOfStream
                                                        strLine=objFile.ReadLine
                                                        If InStr(strLine,rxc ) > 0 Then
                                                                number=Mid(strLine,4,5) Get 5 characters afterwards
                                                                objFile.Close
                                                                strFile.Name = Trim(number)&.txt
                                                                Exit Do
                                                        End If
                                                Loop
                                        End If
                                Next


                                C:test>cscript //nologo  ghost901.vbs

                                Output:

                                09/01/2010  08:15 AM                11 12345.txt
                                09/01/2010  08:15 AM                11 23456.txt
                                09/01/2010  08:15 AM                11 34567.txt
                                09/01/2010  08:15 AM                29 utput.txt
                                09/01/2010  08:15 AM               101 1.txt.txt

                                C:test>type 12345.txt
                                rxc12345

                                C:test>type  utput.txt
                                nooutput  rxc45678 is here


                                C:test>type 1.txt.txt

                                rxc1.txt:rxc12345
                                rxc2.txt:rxc23456
                                rxc3.txt:rxc34567
                                rxc4.txt:nooutput  rxc45678 is here
                                Member of the Human Race; Citizen of the World.