Find the answer to your Linux question:
Results 1 to 5 of 5
I have folder/s I upload files to it/them, with 1000-20,000 files of deferent kinds (pdf, jpg, wmv...etc) with deferent file names and length with spaces in them ..etc I am ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Posts
    4

    Question Batch Files Rename sorted by date


    I have folder/s I upload files to it/them, with 1000-20,000 files of deferent kinds (pdf, jpg, wmv...etc) with deferent file names and length with spaces in them ..etc
    I am trying make Batch to rename them on a regular bases but I want them sorted by "oldest date first", and the new name will be in the format of YYYT000001.xxxx ... YYYY036242.xxxx (where YYYY is a fixed text "Jan" or Dec" (I will enter it manually in the script), and xxxx is the original file extensions)


    I've tried to use the input "for i in $(ls- tr)" as it will be sorted by oldest date, and tried to replace the file names using "basename $i" ..etc.

    I have searched the net but my thick head could not come up with a working script.


    Any ideas / help

    many thanks

  2. #2
    Linux Newbie
    Join Date
    Aug 2006
    Posts
    124
    hi. Instead of using the
    Code:
    for i in $(ls)
    , have a try building your script with
    Code:
    find dirname -iname '*' | while read i ; do
    your code
    your code
    your code
    done
    Many years ago, I tried some batch scripts like you with "for" or "cat" and couldn't get a positive result. As a gift, an example. This script sorts the files generated by my surveillance cameras and send them in different directories, by date :
    Code:
    #!/bin/bash
    d=`date +%Y%m%d`
    find /home/ftpusers/Incoming -iname '*.jpg' | while read a ; do
    	b=`expr $a : "^.*_[0-9]_\(.\{8\}\).*"`
    	c=`echo "/home/ftpusers/"$b`
    	if [ $b -lt $d ]; then
    		if [ -d $c ]; then
    			mv $a $c
    		else
    			mkdir -p "$c"
    			mv $a $c
    		fi
    	fi
    done

  3. #3
    Just Joined!
    Join Date
    Nov 2006
    Posts
    4
    many thanks Captain, I will use your script as a guidance.

    I get a help from "desert69" and post me the following script, but it can not handle spaces in the old file name, I can not figure out why, as all double quotes in place

    #!/bin/bash
    prefix="YYY"
    i=0
    for file in $(ls -tr)
    do
    filename=$(basename "$file")
    extension="${filename##*.}"
    paddedIndex=$(printf "%06d" $i)
    mv $file ${prefix}${paddedIndex}.${extension}
    i=$(($i + 1))
    done

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Aug 2006
    Posts
    124
    Quote Originally Posted by lo900 View Post
    many thanks Captain, I will use your script as a guidance.

    I get a help from "desert69" and post me the following script, but it can not handle spaces in the old file name, I can not figure out why, as all double quotes in place

    #!/bin/bash
    prefix="YYY"
    i=0
    for file in $(ls -tr)
    do
    filename=$(basename "$file")
    extension="${filename##*.}"
    paddedIndex=$(printf "%06d" $i)
    mv $file ${prefix}${paddedIndex}.${extension}
    i=$(($i + 1))
    done
    Hi

    You may try this :
    Code:
        
    paddedIndex=$(printf "%06d" $i)
    newfile=`echo ${prefix}${paddedIndex}.${extension}`
    mv "$file" "$newfile"

  6. #5
    Just Joined!
    Join Date
    Nov 2006
    Posts
    4
    many thanks for the help

    I will try it and post back

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •