Bash help: octal roadblock

So I’m just trying to script some file renaming to 0001, 0002, 0003 etc…
Of course, once I get to 0008, it borks because leading zeros making it think octals.

I think I need to stick a base-10 10# flag in here somewhere, but everywhere I’ve tried I get empty base names like .wav

Help?

                    renum=0001
					# move previously processed 1-9 to digit only names
					rename=$(find "./$targetpath/$font/$effect" -type f -name '*.wav')
					for i in ${rename[@]}; do
						echo "Moving "$i" to -> "$targetpath/$font/$effect/$renum".wav"
						echo
						rsync -ab --no-perms --remove-source-files "$i" "$targetpath/$font/$effect/$renum.wav"
						((renum++))                   	# increment
						renum=000$renum             	# add leading zeros - NEED BASE 10 THOUGH
						renum="${renum: -4}"          	# use only last 4 characters
						
					done

Why are you using rsync to rename files? Something wrong with “mv” ?

How about something like this:

seq -w 1 9999 | for i in "./$targetpath/$font/$effect/"*.wav
do
  read renum
  mv "$i" "$targetpath/$font/$effect/$renum.wav"
done

PS. you can use mv -v to have it print out what it’s doing, and then maybe you don’t need the extra echo statements.

I think there was cross platform issue using mv, which I had at first. Windows bash wasn’t happy.
Even the flag case -a or -A matters, which shouldn’t, but some people had errors with it lower case.
Anyway, I will give your suggestion a shot, thank you.

I’m confused, mv doesn’t have a -a or -A flag.

Archive option for rsync.
MacOS bash works fine with -a.
Windows didn’t use -a for archive, but I can’t man rsync at the moment so I don’t remember what it was instead. Needed to be -A
Not important at this point.

Oh, and if there’s an inadvertant second running of the script, I was looking to have the files not be overwritten, but moved to a ~file instead. That’s why rsyc also i think.
None of which really should happen anyway.
I need to cut back on the amount of “what if” I think and just make it work, not foolproof.
I’m the only one probably actually using it :slight_smile:

If you really want to make shell scripts that work everywhere, you should try to stick to POSIX commands. Unfortunately it seems to be difficult to figure out which commands and flags are mandated by POSIX.2 without buying a book.

unfortunately seq is not POSIX, but we could work around that like so:

NUMBERS="0 1 2 3 4 5 6 7 8 9"
seq4() {
  for a in $NUMBERS; do
    for b in $NUMBERS; do
      for c in $NUMBERS; do
        for d in $NUMBERS; do
          echo $a$b$c$d
       done
      done
    done
  done | tail -n +2
}

seq4 | for i in "./$targetpath/$font/$effect/"*.wav
do
  read renum
  mv -v "$i" "$targetpath/$font/$effect/$renum.wav"
done

This would make it only use POSIX commands, which should guarantee that it works on linux, mac, and anything else vaguely unix-like.

Cool. Well, seq -w worked, swapping that POSIX friendly way in gives empty filenames.
If you really want to see the mess I’m making:
*edit - updated to handle files already in NNNN format.

	echo "Do you wish to rename a single soundfont (enter '1') or a folder containing several soundfonts in subfolders (enter '2')?" 
	echo "If you choose 2, Make sure each sub-folder only contains one soundfont. Otherwise the soundfonts will get mixed!"
	read selection

	if [ "$selection" = "1" ]; then
		echo "You chose to rename a single soundfont. Please enter the name of the font folder containting the soundfont files."
		echo "You may need to enter the path if it's a subfolder of where you are (such as 'SoundfontMaker/Font')"
		read input
		dirs=$(find "$input" -maxdepth 0 -type d)
		
		echo "Found the following soundfont folder:"
		echo $dirs
		echo "Does this folder only contain one soundfont? (y/n)"
		read input2
		if [[ "$input2" = "y" || "$input2" = "Y" ]]; then
			echo " "
		else
			echo "Aborting program"; exit
		fi
	elif [ "$selection" = "2" ]; then 
		echo "You chose to rename several soundfonts. Each soundfont must be in a single subfolder."
		echo "Please enter the name of the folder containing the soundfont folders."
		read input
		dirs=$(find "$input" -mindepth 1 -maxdepth 1 -type d)
		echo " "
		echo "Found the following directories for soundfonts:"
		echo $dirs
		echo "Does each of these folders only contain one soundfont? (y/n)"
		read input2
		if [[ "$input2" = "y" || "$input2" = "Y" ]]; then
			echo " "
		else
			echo "Aborting program"; exit
		fi
	else
		echo "Your selection is invalid. Aborting program";	exit
	fi

	echo "Do you want to automatically add config.ini and smoothsw.ini files if they're missing? (y/n)"
	read addini
	echo " "
	echo "Do you wish a detailed renaming progess report ('1') or only the imporant steps ('2')?"
	echo "Warning, the detailed report may produce a lot of console output!"
	read verbosity

	if [ "$verbosity" = "1" ]; then
		echo "Logging progress to console"
	else
		echo "Logging only important steps"
	fi

	for font in ${dirs[@]}; do
		sounds=$(find "$font" -type f -name '*.wav')
		echo " "
		echo "Sounds to rename/organize:"
		echo "${sounds[*]}"
		otherfiles=$(find -s "$font" -type f ! -name '*.wav' -and ! -name '.*')
		echo " "
		echo "Other files to move:"
		echo "${otherfiles[*]}"
		echo " "
		echo "Converting soundfont in $font".

		targetpath="Converted_to_Proper_Proffie"
		mkdir -p "$targetpath/$font"

		if [[ "$addini" = "y" || "$addini" = "Y" ]]; then
			if [[ ! " ${otherfiles[*]} " =~ $font'/config.ini' ]]; then
	    		echo "Adding missing config.ini"
	    		rsync -ab --no-perms "./inis/config.ini" "$targetpath/$font"
	    	fi
			if [[ ! " ${otherfiles[*]} " =~ $font'/smoothsw.ini' ]]; then
	    		echo "Adding missing smoothsw.ini"
	    		rsync -ab --no-perms "./inis/smoothsw.ini" "$targetpath/$font"
	    	fi
	    fi

		if [[ "${sounds[*]}" == *xtra* ]]; then
			mkdir -p "$targetpath/$font/extras"
			echo "Moving all extras to -> extras folder"
			rsync -rab --no-perms "$font/extras/" "$targetpath/$font/extras"
		fi

		if [[ "${sounds[*]}" == *rack* ]]; then
			mkdir -p "$targetpath/$font/tracks"
			echo "Moving all tracks to -> tracks folder"
			rsync -rab --no-perms "$font/tracks/" "$targetpath/$font/tracks"
		fi

		for o in $otherfiles; do
			echo "Moving "$o" to -> "$targetpath/$font/${o##*/}
			rsync -ab --no-perms "$o" "$targetpath/$font"
		done

		extracounter=1
		hiddencounter=1
		trackcounter=1
		oldeffect="old"

		for src in ${sounds[@]}; do
			# Move extras folder as-is.
			if [[ $src == *._* ]]; then
				if [[ $hiddencounter = 1 ]]; then
					echo "- Hidden files found and ignored."
					hiddencounter=$((hiddencounter+1))	
				fi
				continue;
			fi
			if [[ $src == *xtra* ]]; then
				if [[ $extracounter = 1 ]]; then
					echo "Already moved extras."
					extracounter=$((extracounter+1))	
				fi
				continue;
			fi
			# Move tracks folder as-is.
			if [[ $src == *rack* ]]; then
				if [[ $trackcounter = 1 ]]; then
					echo "Already moved tracks."
					trackcounter=$((trackcounter+1))	
				fi
				continue;
			fi
			# Strip digits, path, and extension from filename
			effectfile="${src//[0-9]/}"
			effectnopath="${effectfile##*/}"
			effect="${effectnopath%.*}"
			if [ "$effect" == "" ];then
				parentdir="$(dirname "$src")"
				mkdir -p "$targetpath/$font/${parentdir##*/}"
				rsync -ab --no-perms  $src $targetpath/$font/${parentdir##*/}
				echo "Effect is numbers only in subdirectory."
				echo "Moving over as-is : $src -> $targetpath/$font/${parentdir##*/}/${src##*/}"
			else
				# Reset counter for new effect type
				# Post-process NNNN formatted files from last round if needed
				if [[ $effect != $oldeffect ]]; then
					counter=1
					if [ ${#targetfile} -gt 12 ]; then
						echo "Effect exceeds 8 characters, converting to NNNN format"
						renum=0001
						seq -w 1 9999 | for i in "$subdir/"*.wav
						do
						  read renum
						  mv -v "$i" "$subdir/$renum.wav"
						done
					fi
				fi			
				# Make subfolder for multiples, or leave single in root
				if [ $counter = 2 ]; then
					mkdir -p "$targetpath/$font/$effect"
					subdir=$targetpath/$font/$effect
					# $target is still from previous loop below
					if [ ${#effect} -gt 6 ]; then
						targetfile=$(printf %q "${effect}1.wav")
					else
						targetfile=$(printf %q "${effect}01.wav")
					fi
					rsync -ab --no-perms --remove-source-files "$target" "./$targetpath/$font/$effect/$targetfile"
					echo "Addidional $effect sounds found."
					echo "Moving ${target##*/} into a $effect subfolder and adding first numerical to base name = $targetpath/$font/$effect/$targetfile"
				fi
				# set target filename with correct digit format
				# singular sound
				if [ $counter == 1 ]; then 
					targetfile=$(printf %q "$effect.wav")
				else
					# Check if leading zero is possible, don't exceed 8 chars
					if [ ${#effect} -gt 6 ] || [ $counter -gt 9 ]; then 
						targetfile=$(printf %q "$effect$counter.wav")
					else
						targetfile=$(printf %q "${effect}0$counter.wav")
					fi
				fi
				# 
				if [ $counter -ge 2 ]; then
					target="./$targetpath/$font/$effect/$targetfile"
				else
					target="./$targetpath/$font/$targetfile"
				fi
				if [ $verbosity = "1" ]; then
					echo "Converting source file ${src} to target $target"
				fi
				rsync -ab --no-perms  $src $target
				# increment counter for next effect sound
			fi
			counter=$((counter+1))
			oldeffect="$effect"
		done
				
		echo " "
		echo "Converted soundfont saved in "$targetpath
		echo " "
	done

	echo " "
	echo " "
	echo "Soundfont conversion complete."
	echo " "
	echo " --- MTFBWY ---"

Which is working well as is converting an all-in-one folder of Proffie font files