I have this script:
#!/bin/sh
for file in "$@"
do
ext=${file##*.}
base=${file%.*}
num=${base##*v}
zeroes=${num%%[!0]*}
num=${num#$zeroes} #remove leading zeros, or it uses octal
num=$((num+1))
base=${base%v*}
new=$(printf '%sv%04d.%s' "$base" "$num" "$ext")
cp -nv "$file" "$new"
done
It works great with file names that are xxx_xxx_v001.aep, but it won't work on this: xxx_xxx_v001_001.aep.
I've tried and tried but just can't figure out why it won't work.
Any help is much appreciated.
Thanks.
I assume you still want to increment the Vnnn part on the 2nd longer filename.
Also note you can use 10# to avoid the octal issue:
for file in "$@"
do
ext=${file##*.}
base=${file%.*}
num=${base##*v}
rest=${num#*_}
if [ ${#rest} -eq ${#num} ]
then
rest=""
else
num=${num%%_*}
rest=_$rest
fi
num=$((10#$num+1))
base=${base%v*}
new=$(printf '%sv%04d%s.%s' "$base" "$num" "$rest" "$ext")
echo cp -nv "$file" "$new"
done
1 Like
That works but changes the first number.
I need one that changes the last number and ignores the first.
So, 093_180_v001_004.aep ; would become 093_180_v001_005.aep
The thing is that the first number will not always be v001 ...
Doable?
OK that is actually a little easier:
for file in "$@"
do
ext=${file##*.}
base=${file%.*}
num=${base##*[v_]}
base=${base%$num}
num=$((10#$num+1))
new=$(printf '%s%03d.%s' "$base" "$num" "$ext")
cp -nv "$file" "$new"
done
Yes! That works perfectly. Thank you!