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

Author Topic: Editing file names using bat files?  (Read 3162 times)

0 Members and 1 Guest are viewing this topic.

DMDr01

    Topic Starter


    Starter

    Editing file names using bat files?
    « on: April 14, 2009, 03:30:53 PM »
     First off I would like to thank the forum and knowledgable poster for there help here.
    I got my little file done by reading here in the help batch files section and reading many other post,
    but the last thing I would like to do but I just can not figure out, and that is how to read a file name into
    a variable so I can edit the file name, not just rename it.
     What I want to do is read an existing file name which consist of a name and a version# and replace the name
    part of the file but keep the version #.
    EX.  oldfilename_001.abc  to newfilename_001.abc

     Thanks

    devcom



      Apprentice

      Thanked: 37
      Re: Editing file names using bat files?
      « Reply #1 on: April 14, 2009, 04:10:56 PM »
      you mean:
      Code: [Select]
      ren oldfilename_*.abc newfilename_*.abc
      ??

      anyway you can use for to get filenames...

      Code: [Select]
      for /F %%F in ('dir /b *.abc') do echo %~nF
      Download: Choice.exe

      DMDr01

        Topic Starter


        Starter

        Re: Editing file names using bat files?
        « Reply #2 on: April 14, 2009, 04:22:40 PM »
        you mean:
        Code: [Select]
        ren oldfilename_*.abc newfilename_*.abc
        ??

        anyway you can use for to get filenames...

        Code: [Select]
        for /F %%F in ('dir /b *.abc') do echo %~nF

        Thanks for the reply, your 2nd example is beyond me, I did see that in another post and it did work as far as echoing to the screen but I was not able to modify it to work for me, however your first example( can`t believe I did not think of it) should do just fine.

         Thanks

        DMDr01

          Topic Starter


          Starter

          Re: Editing file names using bat files?
          « Reply #3 on: April 15, 2009, 09:28:20 PM »
           Well the rename as simple as it seemed I was not able to use as it is a little more complicated than just renaming a file. :'(
          In my bat file, which runs under a CMD window in Vista, the original file gets copied then archived away. The copied file has
          changes made to it and is then named for use. At this point I really have no way of telling which file goes with which file so I
          hoping to take the version # out of the original file and have it added to the new file name. Simply renaming does not work.
           The 2nd method devcon provided me does not work under my CMD window.  I get "The syntax of the command is incorrect". After trying it in immeditate mode on the command line the %%F causes a "%%F was unexpected at this time error".  Removing one of the % does allow it to work and changing the command "echo" to "set f =" does make it an environmental variable showing the file name. That is what I was looking for and I assume at this point it would be easier to edit and just take the version # from the orginal file name. However that only worked when type on the command line.
           Any idea what is causing the syntax error when the bat file is run?

           Thanks

          Reno



            Hopeful
          • Thanked: 32
            Re: Editing file names using bat files?
            « Reply #4 on: April 15, 2009, 10:57:43 PM »
            what is it that not working?

            ren oldfilename_*.abc newfilename_*.abc
            this will work as long as string length of oldfilename equal to string length of newfilename

            type: dir/b *.abc
            and post the few lines of the screen output here

            single % is used on command prompt, when you write the code into batch file you use double percent %%.

            Geek-9pm


              Mastermind
            • Geek After Dark
            • Thanked: 1026
              • Gekk9pm bnlog
            • Certifications: List
            • Computer: Specs
            • Experience: Expert
            • OS: Windows 10
            Re: Editing file names using bat files?
            « Reply #5 on: April 15, 2009, 11:21:46 PM »
            Again, I am lost.  :o
            What did you need FOR  for?
            Code: [Select]
            RENAME *_???.abc NewName_???.abc

            macdad-



              Expert

              Thanked: 40
              Re: Editing file names using bat files?
              « Reply #6 on: April 16, 2009, 06:11:34 AM »
              Seems to be that Dev's for loop is to find all files in the current directory and sub-dirs that have the abc extension, and everytime it finds a file with that then show the name of the file.

              if he means %~nl instead of %~nF
              If you dont know DOS, you dont know Windows...

              Thats why Bill Gates created the Windows NT Family.

              DMDr01

                Topic Starter


                Starter

                Re: Editing file names using bat files?
                « Reply #7 on: April 16, 2009, 09:02:24 AM »
                Thanks guys without your help as I would never had gotten this without it.

                Code: [Select]
                for /F %%F in ('dir /b *.zip') do set 1 = %%F
                 This gives me environmental variable 1, as seen under set command, = oldfilename_date_version_type.zip.
                I assume I can edit the variable 1 to take date_version_type from the old file and add that to the new file
                after changes are made to the file to give me newfilename_date_version_type.abc.

                 Thanks for the help and I`ll see if I can edit the variable tonight.