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.
- 11-16-2012 #1Just Joined!
- Join Date
- Nov 2006
- Posts
- 4
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
- 11-16-2012 #2Linux Newbie
- Join Date
- Aug 2006
- Posts
- 115
hi. Instead of using the
, have a try building your script withCode:for i in $(ls)
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:find dirname -iname '*' | while read i ; do your code your code your code done
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
- 11-16-2012 #3Just 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
- 11-18-2012 #4Linux Newbie
- Join Date
- Aug 2006
- Posts
- 115
- 01-01-2013 #5Just Joined!
- Join Date
- Nov 2006
- Posts
- 4
many thanks for the help
I will try it and post back


Reply With Quote

