Loading ...
Sorry, an error occurred while loading the content.
 

Batch renaming files using VIM

Expand Messages
  • Srinivas Rao. M
    Hi Everyone, I have my photos kept in the folder, They are having file names as DSC101.jpg DSC102.jpg DSC103.jpg Is there a way in which i can rename these
    Message 1 of 4 , Nov 30, 2005
      Hi Everyone,

      I have my photos kept in the folder, They are having file names as
      DSC101.jpg
      DSC102.jpg
      DSC103.jpg

      Is there a way in which i can rename these files by
      ":s/DSC/SriniPhotos/g" operation.

      When i execute this command on the folder, it gives me the error
      message: "E21: Cannot make changes, 'modifiable' is off"

      I wish to retain the serial number(101,102,103,...etc) of photos as it
      is. Is there a way to make this work, from within VIM environment ?

      TIA,
      Srini...



      --
      website: http://www.geocities.com/saksblr
      egroups: http://in.groups.yahoo.com/group/saksblr
      --
      RESPONSIBILITY starts with the willingness to experience yourself as
      cause in the matter.
      --
    • A. S. Budden
      ... A bit of a fudged approach to this (that I use all the time) is to ... There is probably an equivalent that will work with windows using ... imagine that
      Message 2 of 4 , Nov 30, 2005
        On 30/11/05, Srinivas Rao. M <srinirao@...> wrote:
        > Hi Everyone,
        >
        > I have my photos kept in the folder, They are having file names as
        > DSC101.jpg
        > DSC102.jpg
        > DSC103.jpg
        >
        > Is there a way in which i can rename these files by
        > ":s/DSC/SriniPhotos/g" operation.

        A bit of a fudged approach to this (that I use all the time) is to
        open a new file and do something like:

        :r!ls
        :%s/DSC\([0-9]*\)/mv \0.jpg SriniPhotos\1/
        :%!bash

        There is probably an equivalent that will work with windows using
        :r!dir etc. If you want to do it from the file explorer, then I'd
        imagine that you need some fairly fundamental changes to its
        operation, but I might be wrong...

        Al
      • Peter Palm
        ... You are probable editing the directory containing the images, right? You can rename files using R , but this only works for a single file (see :help
        Message 3 of 4 , Nov 30, 2005
          Op woensdag 30 november 2005 11:34, schreef Srinivas Rao. M:
          > Hi Everyone,
          >
          > I have my photos kept in the folder, They are having file names as
          > DSC101.jpg
          > DSC102.jpg
          > DSC103.jpg
          >
          > Is there a way in which i can rename these files by
          > ":s/DSC/SriniPhotos/g" operation.
          >
          > When i execute this command on the folder, it gives me the error
          > message: "E21: Cannot make changes, 'modifiable' is off"

          You are probable 'editing' the directory containing the images, right?

          You can rename files using 'R', but this only works for a single file
          (see :help file-explorer).

          > I wish to retain the serial number(101,102,103,...etc) of photos as it
          > is. Is there a way to make this work, from within VIM environment ?

          Probably, but vim is not a file-manager. If you really want to do it in vim,
          I'd do the following:
          r! ls
          %s/DSC\(.\{-\}\)\.jpg/mv & SriniPhotos\1.jpg/

          and then execute the resulting file (using :!sh % after writing the file)

          But, using a shell, it's much simpler:
          for i in *
          do
          mv $i ${i/DSC/SriniPhotos}
          done

          (depending on your shell, tested in zsh and bash)


          Peter
        • Tim Chase
          ... Peter s suggestion is a fabulous one. I always forget that Bash (and zsh) have that variable substitution feature...and even when I *do* remember that
          Message 4 of 4 , Nov 30, 2005
            > But, using a shell, it's much simpler:
            > for i in *
            > do
            > mv $i ${i/DSC/SriniPhotos}
            > done
            >
            > (depending on your shell, tested in zsh and bash)

            Peter's suggestion is a fabulous one. I always forget that Bash
            (and zsh) have that variable substitution feature...and even when
            I *do* remember that it's possible, I have to go hunting through
            the man-pages for bash to dig up the syntax for it.

            If, however, your shell doesn't support such features, you can
            easily use sed to do the dirty work for you:

            ls DSC* | sed 's/DSC\(.*\)/mv & SriniPhotos\1/' | sh

            should do the trick for you.

            If you've got a brain-dead version of sed that doesn't recognize
            the \(...\) notation, there are workaround for that
            too...something like changing the above "sed" command to

            sed -e 's/DSC//' -e 's/.*/mv DSC& SriniPhotos&/'

            which should also extract the incrementing part.

            Ah, the many-ways-to-do-things way of *nix.

            If it's just vim on a Win32 box, without sed around, you can use

            :r! dir /b

            to read in the files, then massage it with

            :%s/DSC\(.*\)/ren & SriniPhotos\1

            The results can be saved as a batch file (runme.bat) and then
            just run that batch file.

            Given what I know about most cameras, there aren't any embedded
            spaces in any of those file names. If so, you'll likely have to
            quote them in the resulting mv/ren statement.

            -tim
          Your message has been successfully submitted and would be delivered to recipients shortly.