How to save consecutive files with incremental numbering? (Solved)

Issues and / or general discussion relating to Puppy

Moderator: Forum moderators

Post Reply
User avatar
mikewalsh
Moderator
Posts: 6154
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 792 times
Been thanked: 1979 times

How to save consecutive files with incremental numbering? (Solved)

Post by mikewalsh »

Afternoon, kids.

Uncle Mike's got himself stumped on this one. I'm probably going about it all wrong, but reading through various file-system 'man' pages hasn't produced a usable answer.....and I suspect this is really basic stuff.

Say I have a directory, with three files already in it; "file1", "file2", and "file3". I want to ensure that the next few files, when saved or written to this directory, are auto-numbered "file4", "file5", "file6".....and so on & so forth.

Question:- How do we arrange for this when scripting? I know I've come across summat about this a long while ago, but I simply cannot remember how it's done. I don't mind admitting, even I'm at 'Beginner' status when it comes to some things!

Can some kind soul put me out of my misery and remind me how it's done, please? Cheers, all!

TIA.

Mike. ;)

HerrBert
Posts: 357
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 126 times

Re: How to save consecutive files with incremental numbering?

Post by HerrBert »

Not exactly what you ask for...

I made a script for home use to rename copies to avoid overwriting existing files in destination:

Code: Select all

#!/bin/bash
# make a numbered copy if destination file exists
# multicall script nummv -> numcp
# ex: find /home/spot/Downloads/ -type f -not -name "*.mp4" -exec ./nummv {} /root/Downloads \;

shopt -s nullglob
[ ${#@} -ne 2 -o ! -f "$1" ] && { echo "usage: numcp FILE DESTINATION"; exit; }
BASE="${1##*/}"
OUTNAME="${2%/}/$BASE"
if [ -f "$OUTNAME" ]; then
	case "$BASE" in
		*.tar.*) EXT="tar${BASE#*.tar.}";PTBASE="${BASE%%.tar.*}";;
		*) EXT="${BASE##*.}";PTBASE="${BASE%.*}";;
	esac
	[ "$EXT" = "$BASE" ] && EXT=""
	PT1="${BASE%(*}"
	PT2="${BASE##*)}"
	[ "$PT1" = "$PT2" ] && { PT1="${PTBASE}"; PT2="${EXT:+.$EXT}"; }
	i=0
	while true; do
		((i++))
		OUTNAME="${2%/}/${PT1}(${i})${PT2}"
		[ ! -f "$OUTNAME" ] && break
	done
fi
cp -a "$1" "$OUTNAME"
ERR=$?
[ "$(echo "$1" | grep -w spot)" ] && chown root:root "$OUTNAME"

[ "${0##*/}" = "nummv" -a "$ERR" -eq 0 ] && rm "$1"

It also sets owner and group to root if the source is located in spot.

On my Puppy i called it numcp with a symlink to it named nummv.
This way i can just copy from spot/Downloads or move from it.

Use at own risc...

williams2
Posts: 1062
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 305 times

Re: How to save consecutive files with incremental numbering?

Post by williams2 »

Not enough information.

In /tmp/
of the filenames file001 to file999
this will find the first file name of file001 to file999
that does not exist in /tmp/
and create that file.
Probably not the fastest or most elegant method.

Code: Select all

#!/bin/sh
cd /tmp/
for n in {001..999}
do
  if ! test -f file$n
  then
    echo file$n
    touch file$n
    exit
  fi
done
User avatar
mikewalsh
Moderator
Posts: 6154
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 792 times
Been thanked: 1979 times

Re: How to save consecutive files with incremental numbering?

Post by mikewalsh »

@williams2 :-

Not enough info, huh? O-kayyy.....

Some of us have been messing about with ways of creating a 'loopback' ALSA device for those rare computers where the default audio card doesn't apparently have one. My new-ish HP rig has been annoying me from day one, nearly 2½ years ago, with this seeming lack of functionality.....so Fred, rcrn51, jamesbond, myself and a couple of others have been working something out:-

viewtopic.php?t=6083

Bill - rcrsn51 - originally created a basic ALSA utility for recording the default audio card's output via 'arecord'; worked well, except you couldn't hear what you were recording while you were doing so. But it was a good starting-point. Fred then got involved, 'cos he had the same issue as me.....ffmpeg and .flac came into the mix, and all sorts of ideas got tried out.

jamesbond then joined in, and has donated an .asoundrc file from FatDog which, along with loading the 'snd_aloop' module via

Code: Select all

modprobe snd-aloop

.....allows you to 'monitor' what's going on as you record it. It also permits easy swapping between cards without the need to re-boot every time.

-----------------------------------

Long story short, I've cobbled together a simple audio recorder, complete with a wee GUI for my personal use. It originally used 'arecord'; it's now using 'ffmpeg', and saves the file as 'audio.mp3'.

After recording has finished, it will let me play-back the recorded file using mplayer; this allows me to check the recording is OK, and upon coming to the end, OR being stopped via 'killall' keybound to the "Pause/Break" button, sleeps for 1 second, then auto-moves the just-recorded 'audio.mp3' file into a sub-directory called 'SAVED' in the same directory as the GUI scripts & function scripts.

With me so far?

Currently, after moving the 'audio.mp3' file into the 'SAVED' directory, it then opens ROX on that sub-directory ready for me to manually re-name it with a number suffix, so it doesn't get over-written by the next recording that comes in.

What I would like to do is to automatically name the 'incoming' file to the next number up from the highest-numbered file already present. In other words - for example - if the 'SAVED' directory already contains "audio-1.mp3", "audio-2.mp3" & "audio-3.mp3", I would like for it to automatically rename the next file to "audio-4.mp3" as it moves it into the 'SAVED' directory. (Or immediately after doing so; I don't mind which. Whatever's simpler to implement.)

Does that make sense? I assume this can be done, but from what research I've done so far it appears to involve some quite complicated scripting.....most likely beyond my current "pay-grade"!

Mike. ;)

User avatar
MochiMoppel
Posts: 1232
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 437 times

Re: How to save consecutive files with incremental numbering?

Post by MochiMoppel »

mikewalsh wrote: Tue Jun 07, 2022 11:49 pm

I assume this can be done, but from what research I've done so far it appears to involve some quite complicated scripting

Probably not. HerrBert and williams2 have already pointed you into the right direction and you may have missed their point.

Please post the portion of your script that "auto-moves the just-recorded 'audio.mp3' file into a sub-directory called 'SAVED' in the same directory as the GUI scripts & function scripts". It's probably just one line, but here is where you have to rename, not afterwards after 'audio.mp3' was moved to SAVED. This would also be possible and would reflect what you are presently doing manually by renaming in ROX-filer, but that would be inefficient and more complicated than it needs to be.

User avatar
mikewalsh
Moderator
Posts: 6154
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 792 times
Been thanked: 1979 times

Re: How to save consecutive files with incremental numbering?

Post by mikewalsh »

@MochiMoppel :-

Hallo, Mochi.

Here's the entirety of 'PLAYBACK.sh'. It's not very long:-

Code: Select all

#!/bin/sh
#
# Play back recorded audio file...
#
HERE="$(dirname "$(readlink -f "$0")")"
#
mplayer $HERE/audio.mp3
#
sleep 1
#
mv $HERE/audio.mp3 $HERE/SAVED/
sleep 1
/usr/local/bin/rox ~/AudioRecord/SAVED

You're welcome to take a look at this, but it won't be long before I'm turning in for the night.....and it'll then be at least 8 hours before I'm about again. Sorry about that.

(The scripts - GUI and function, plus images - reside in a directory called 'AudioRecord'. 'SAVED' is a sub-directory within this.)

It's all incredibly basic stuff, I'm afraid.....nothing exciting! :oops:

Mike. ;)

User avatar
MochiMoppel
Posts: 1232
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 437 times

Re: How to save consecutive files with incremental numbering?

Post by MochiMoppel »

change

Code: Select all

mv $HERE/audio.mp3 $HERE/SAVED/

to

Code: Select all

cnt=1
while [[ -e $HERE/SAVED/audio${cnt}.mp3 ]];do
((cnt++))
done
mv $HERE/audio.mp3  $HERE/SAVED/audio${cnt}.mp3

Haven't tested but should work.

User avatar
mikewalsh
Moderator
Posts: 6154
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 792 times
Been thanked: 1979 times

Re: How to save consecutive files with incremental numbering?

Post by mikewalsh »

@MochiMoppel :-

I have just one thing to say to you; thank you SO much!

Short & sweet, like all the best solutions.....and works perfectly. :thumbup:

I'd found several similar sorts of solutions on StackExchange, and other knowledge fora, but try as I might I just could NOT adapt them so they would actually work for me. I was either attempting to do the numbering thing before moving, or immediately after moving. It didn't occur to me to do so during the move operation.... (*d'ohh..!*)

No, we all have our strong points, and our weak areas. I'm good at what I do - well, I think I am - but not so hot at what I think of as "complex" scripting (though as you say, this is not THAT complex). Most of mine is fairly simple file-manipulation, though of course I'm now doing stuff I wouldn't have dreamt of attempting, say, 5 years ago. I guess I AM slowly improving, at that....but I'm never too proud to admit when I'm stumped, and ask advice.

This is the thing about any community. You help each other out.....in the long run, it makes you stronger as a group.

Thanks again.

(EDIT:- One question, if I may? What's the "-e" for, inside the double square brackets?)

Mike. ;)

williams2
Posts: 1062
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 305 times

Re: How to save consecutive files with incremental numbering?

Post by williams2 »

Type help test in a terminal.

test , [ and [[ do roughly the same thing.

User avatar
MochiMoppel
Posts: 1232
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 437 times

Re: How to save consecutive files with incremental numbering? (Solved)

Post by MochiMoppel »

@mikewalsh You're welcome. If this is what you were after, then it's fine, but there is a catch:
If your list of saved files is
audio1.mp3
audio2.mp3
audio3.mp3
audio4.mp3
then the next file will be named audio5.mp3. So far, so good.

However if you have delete a file from your list of saved files the list may look like this:
audio1.mp3
audio2.mp3
audio4.mp3
In this case the next file will be named audio3.mp3. and not audio5.mp3 as you might expect.
In other words, the script uses the next available number, not the highest used number + 1
If this is what you want the code needs to be a bit more complex .... but not much.

User avatar
mikewalsh
Moderator
Posts: 6154
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 792 times
Been thanked: 1979 times

Re: How to save consecutive files with incremental numbering? (Solved)

Post by mikewalsh »

@MochiMoppel :-

No, that's ideal as it is, Mochi. I'm using this directory as a temporary 'sort' area, if I make several recordings one after another.

This allows me to save each file with a different identity, so at least I can sort through them.....rather than keeping the same file name all the time.......which then means I will have to move every file as it's made, in order to prevent it from being overwritten by the next "incoming file".

It will let me sort through several at a time this way, so I can decide what I want to keep and what I want to discard......thus, it doesn't matter about keeping things in a particular order, or with specific numbers.

:thumbup:

Mike. ;)

User avatar
rcrsn51
Posts: 1389
Joined: Sun Aug 23, 2020 4:26 pm
Been thanked: 357 times

Re: How to save consecutive files with incremental numbering? (Solved)

Post by rcrsn51 »

Another approach would be to save the files with a time stamp, like: audio-$(date +%F-%T).mp3

User avatar
mikewalsh
Moderator
Posts: 6154
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 792 times
Been thanked: 1979 times

Re: How to save consecutive files with incremental numbering? (Solved)

Post by mikewalsh »

rcrsn51 wrote: Wed Jun 08, 2022 11:30 am

Another approach would be to save the files with a time stamp, like: audio-$(date +%F-%T).mp3

Mm-hm. I did find a couple of scripts along these lines for other folk's use-cases that involved time/date-stamps. One guy wanted to index complete web-pages as he saved them, for instance.....and date-stamps were suggested in his case.

A simple numerical approach is all I really need.

Mike. ;)

User avatar
rcrsn51
Posts: 1389
Joined: Sun Aug 23, 2020 4:26 pm
Been thanked: 357 times

Re: How to save consecutive files with incremental numbering? (Solved)

Post by rcrsn51 »

mikewalsh wrote: Wed Jun 08, 2022 11:51 am

A simple numerical approach is all I really need.

My point was that you could eliminate the looping code and just use

Code: Select all

mv $HERE/audio.mp3 $HERE/SAVED/audio-$(date +%F-%T).mp3
User avatar
mikewalsh
Moderator
Posts: 6154
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 792 times
Been thanked: 1979 times

Re: How to save consecutive files with incremental numbering? (Solved)

Post by mikewalsh »

rcrsn51 wrote: Wed Jun 08, 2022 11:59 am
mikewalsh wrote: Wed Jun 08, 2022 11:51 am

A simple numerical approach is all I really need.

My point was that you could eliminate the looping code and just use

Code: Select all

mv $HERE/audio.mp3 $HERE/SAVED/audio-$(date +%F-%T).mp3

Yah, I take your point. Right. I'll probably give that a try, too, at some stage. Thanks for the suggestion! (Ever since we got the audio loopback recording stuff finally working, it's sparked-off a whole slew of new ideas I want to try out, so it's going to be a case of as-and-when, I think... :D )

Mike. :thumbup:

Post Reply

Return to “Users”