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

Author Topic: How to search a certain file and repeat until found  (Read 5156 times)

0 Members and 1 Guest are viewing this topic.

seigen

    Topic Starter


    Starter

    How to search a certain file and repeat until found
    « on: December 30, 2009, 12:11:39 AM »
    Hello, I am new to the forums and thanks everyone for your help.

    I am having trouble creating a simple batch file that has to look over the root directory for a file that is located into the removable media, and if found, rename that file.

    To be more precise, I have a freeware Antivirus for removable drives named Mx One. The trouble right here, is that I was creating an Auto-Update option with Autorun, that executes the program, and then having the choice for the user to update it for himself whenever he inserts his USB. But my trouble right ahead, is that when the Update function of Mx One Antivirus launches, it deletes the previous sequential file, and continues the sequence and creating a new file.

    For example I open: Mx one 0001.exe I update, so Mx one 0001.exe no longer exists, and Mx one 0002.exe is created instead ONLY if update is selected.


    I want to execute a batch file that looks for the next sequential file, (something repeated in a FOR loop) ending at a MAXIMUM integer or until found, having something like this:

    for index=1  index<99(or until found)
     IF EXIST filename+index

    rename filename > newfilename

    EXIT


    Thanks for your help.


    Grimbear13



      Rookie

      Re: How to search a certain file and repeat until found
      « Reply #1 on: December 30, 2009, 09:05:53 AM »
      From my limited knowledge of batching there is no incremental function.  So using an index is tricky.  However you can run the for loop to loop for all of a certain file type in a folder.  For example:

      Code: [Select]
      for %%I in (*.sql) do {} (
      will search through the directory for all .sql files and do whatever is encapsulated in the do { }

      A work around I found for incrementing if you need a numeric value is putting this at the end of the loop:

      Code: [Select]
      echo 1 >> i.txt
      This will make a i.txt file with a series of 1's for each time the loop reaches the end.  Use this in tandum with:

      Code: [Select]
      find /c "1" i.txt > i2.txt
      This will result in the number of 1's in i.txt and print it to i2.txt (I haven't attempted to put the value in a variable but I don't see why it wouldn't work) but there is your number that has been incremented.

      Hope this is somewhat helpful.

      Helpmeh



        Guru

      • Roar.
      • Thanked: 123
        • Yes
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 8
      Re: How to search a certain file and repeat until found
      « Reply #2 on: December 30, 2009, 01:27:17 PM »
      Here is how to increment in batch:

      for /l %%a in ('1,1,100') do echo %%a

      I think...Lol
      Where's MagicSpeed?
      Quote from: 'matt'
      He's playing a game called IRL. Great graphics, *censored* gameplay.

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: How to search a certain file and repeat until found
      « Reply #3 on: December 30, 2009, 01:47:51 PM »
      Here is how to increment in batch:

      for /l %%a in ('1,1,100') do echo %%a

      I think...Lol

      Something along those lines.

      I was exploring a solution for this thread with that particular construct but realized that it would be more difficult than that: the numbers would need to have an appropriate number of zero's prepended. the bleeding obvious VB equivalent,
      Code: [Select]
      strnewname= "mx one " & String(4 - Len(Trim(Str(Number))), "0") + Trim(Number) & ".exe" doesn't work in batch.

      Oh... heh, I could always write a little script, I suppose; I'm not certain what the desired result is; that is, what "newfilename" is to be (?)
      I was trying to dereference Null Pointers before it was cool.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: How to search a certain file and repeat until found
      « Reply #4 on: December 30, 2009, 02:18:47 PM »
      Making a good batch file can be a lot of fun. But a person is better motivated to do a good job if they have a good reason.

      Looking over your original post, I don't see a very good reason for doing this. Pardon me, but that freeware program you're referring to works very well just the way it is. So I don't get the point. You already know that it's going to give itself another name and delete the prior version. So what's wrong with that?
      But, if you insist, it's quite easy to find a file that has a certain name or has some ambiguity in the name. Now the question would be; Suppose to found 100 files that have ambiguous names. Do you want to delete them all? You want to rename the mall? Or just rename the last one and delete all the others?
      You objective is either not clear or in unreasonable. Remember, Reason must precede logic.

      seigen

        Topic Starter


        Starter

        Re: How to search a certain file and repeat until found
        « Reply #5 on: December 30, 2009, 04:28:10 PM »
        Making a good batch file can be a lot of fun. But a person is better motivated to do a good job if they have a good reason.

        Looking over your original post, I don't see a very good reason for doing this. Pardon me, but that freeware program you're referring to works very well just the way it is. So I don't get the point. You already know that it's going to give itself another name and delete the prior version. So what's wrong with that?
        But, if you insist, it's quite easy to find a file that has a certain name or has some ambiguity in the name. Now the question would be; Suppose to found 100 files that have ambiguous names. Do you want to delete them all? You want to rename the mall? Or just rename the last one and delete all the others?
        You objective is either not clear or in unreasonable. Remember, Reason must precede logic.

        Thanks for the help. The purpose of this batch file, is that when the common box dialog that Autorun launches, it calls this batch file. I was creating the ability for the user to open the program every time it clicks on the desired option of the Autorun Select Option Dialog. As I said, I can do this everytime I want if the file I'm looking for is named Mx One 0001, for example, as I said.

        But when I do want to execute it, via the command line, in order for it to be called, executable files cannot have spaces in their names, so that's why I have to rename Mx?One?0001.exe to MxOne_Antivirus.exe, that is a valid executable file name at Command prompt.

        The reason I'm doing, is for a school project, hehe sorry to not mention that.

        My logic behind this, is to look the file name with an Index, as BC_Programmer stated with a VB Script instead of a Batch file; first I rename it (obviously a command prompt shows up as usual) and then, it renames Mx One 0001.exe IF Found

        IF NOT EXIST, then I have to look over the removable drive (only at its root) for the *.exe name containg at Least Mx One+an Index over it, then again, IF FOUND, rename that file to MxOne_Antivirus.exe that is a valid executable filename and last, launch it via command prompt (batch file), repeating this operation several times with a limit. (similar to search until found, obviously this limit has to be a little bigger).

        If this is more complex, and there's another portable, or practical solution like C, C++ or Java, or another programming language, please suggest  ::)

        Thanks


        PD With an Index I mean that the file has an ambiguity in its name, following your advice. This ambiguity is that the file deletes itself when you update, and changes its version that follows a sequence. So I do want to look up for the filename with an Index over its String name, and if found rename... bla..      :D

        « Last Edit: December 30, 2009, 04:55:18 PM by seigen »

        seigen

          Topic Starter


          Starter

          Re: How to search a certain file and repeat until found
          « Reply #6 on: December 30, 2009, 05:00:33 PM »
          Something along those lines.

          I was exploring a solution for this thread with that particular construct but realized that it would be more difficult than that: the numbers would need to have an appropriate number of zero's prepended. the bleeding obvious VB equivalent,
          Code: [Select]
          strnewname= "mx one " & String(4 - Len(Trim(Str(Number))), "0") + Trim(Number) & ".exe" doesn't work in batch.

          Oh... heh, I could always write a little script, I suppose; I'm not certain what the desired result is; that is, what "newfilename" is to be (?)



          The new filename to be is, as my previous reply with a valid name, in order to be executed.

          It searches for Mx One 0001.exe, IF Found it renames this *.exe file for MxOne_Antivirus.exe, then it launches MxOne_Antivirus.exe that is the new Filename prior to the last one that was Mx One 0001.exe. This obviously, always works due that it only has to look for Mx One 0001.exe, but when you update it, a new filename with the next sequential version creates, deleting the previous one and leaving only the new one. So as I firstly explained: Mx One 0001.exe no longer exists, and Mx One 0002.exe is created instead. This is the UPDATED filename, with the newest deffinitions.

          I want to look over this ambiguity, leaving the ability for it to be an Auto-Execute option, that is called from my Modified Autorun.inf file, that calls this particular batch file.


          Thanks for any further help ;)

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: How to search a certain file and repeat until found
          « Reply #7 on: December 30, 2009, 05:09:51 PM »
          If I understand correctly, your filenames are in the form:

          "Mx One 0001.exe" with different 4 character numbers on the end or what have you.

          you can rename them ALL at once! I just remembered our good friend, the ? wildcard:

          Code: [Select]
          ren "?? ??? ????.exe" "??_???_????.exe"

          of course, if for some reason you have other exe files with 2 characters, a space, three characters, a space, and four characters, it will rename these, as well. But that seems unlikely.
          I was trying to dereference Null Pointers before it was cool.

          seigen

            Topic Starter


            Starter

            Re: How to search a certain file and repeat until found
            « Reply #8 on: December 30, 2009, 06:05:18 PM »
            If I understand correctly, your filenames are in the form:

            "Mx One 0001.exe" with different 4 character numbers on the end or what have you.

            you can rename them ALL at once! I just remembered our good friend, the ? wildcard:

            Code: [Select]
            ren "?? ??? ????.exe" "??_???_????.exe"

            of course, if for some reason you have other exe files with 2 characters, a space, three characters, a space, and four characters, it will rename these, as well. But that seems unlikely.

            Yes, that was exactly I was looking for, didn't know MS-DOS had that pattern ;). Ok, one last issue and it is minor.
            Obviously anyone who gets into their USB and launches my particular option I created with Autorun it will open a Command prompt window. I want it to be hidden, or show Up, but when the Antivirus program is called, I mean when this sentence is completed succesfully, close it immediately by itself, but leaves the Antivirus program opened.

            Can this be done? I've heard something about hstart execute file you have download, but it has a lot of features and more complex than I want to do.

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: How to search a certain file and repeat until found
            « Reply #9 on: December 30, 2009, 06:06:41 PM »
            Not sure I understand, but you can use Exit in a batchfile to close it; programs it started will still execute.
            I was trying to dereference Null Pointers before it was cool.

            macdad-



              Expert

              Thanked: 40
              Re: How to search a certain file and repeat until found
              « Reply #10 on: January 01, 2010, 01:57:47 PM »
              Here is how to increment in batch:

              for /l %%a in ('1,1,100') do echo %%a

              I think...Lol
              FYI:
              That way would only work if the OS he ran the batch on was NT or later.

              But BC's method is more methodical(and never knew about the ? wildcard  ;))
              If you dont know DOS, you dont know Windows...

              Thats why Bill Gates created the Windows NT Family.