View Single Post
  #13  
Old December 16th 18, 06:22 AM posted to alt.windows7.general
Paul[_32_]
external usenet poster
 
Posts: 11,873
Default rename photo by random size

Jean Fredette wrote:
posted:

It sounds like you're trying to override the playback
order in a screensaver, picture frame, or smart TV.


Yes. I want to jumble images to not clump.
IV doesn't really have anything that is random.

By
appending a prefix to the file name, the alphabetical
order can be overridden.


Thank you.
The gawk looks good but does it do more than RandomNames?
https://www.howtogeek.com/57661/stup...n-a-directory/

That script has the option to undo and to prepend to the file name.
https://www.howtogeek.com/wp-content...andomNames.zip

REM 0 = Rename the file randomly.
SET PrependOnly=0

REM 1 = Prepend the existing file name with randomly generated string.
SET PrependOnly=1

REM 1 = Undo changes according to the translation file.
SET Undo=1


I specifically did not make this a full featured
"add" "re-randomize" "remove" program.

I could do that, but elected not to.

This is what you call a "quick and dirty".

The point of presenting it at all, is to as
much show an approach, as to give you finished
software. Note that:

1) The "program" I provided, does not rename files
itself. It only generates an MSDOS batch file
containing rename commands. This is intended to
take the "danger" out of the program. It also saves
me adding a ton of "if-then-else" that might
obscure the simple nature of the program.
You can actually do the rename step in gawk too,
by calling Windows shell routines to do it. I elected not
to attempt it. If I spent 24 hours working on the
code, I could add everything.

2) The "program" reuses code. I was too lazy to learn
how to shuffle a deck, so I borrowed some code to do it.

3) The "program" has a minimum of command line options
(effectively zero options). They can be added,
but that is "feature creep". And the program isn't
really all that functional to begin with, because
the real work is done when you execute the output
batch file.

Anything that renames or deletes files, I am *really*
*really* careful. It takes a lot of safety testing
to make sure I didn't screw up. And I *have* lost files
(only one or two) via programming accidents in the past.
That's where my careful methodical approach for full
featured programs come from. From losing files...

The intended usage scenario is this:

1) Copy selected files for the screensaver into a new
folder. I.e. *Don't* use your only copy for this purpose.

2) cd /d /path/to/folder
dir input.txt
run the gawk program to generate a batch
notepad output.txt
remove lines that don't belong, then save
ren output.txt output.bat
./output.bat # folder re-randomized

3) If needing to re-randomize the folder, repeat
step two. If a file begins with 0001_goodbye.jpg,
the gawk program can place 0005_goodbye.jpg as
its new name, making it the fifth image to play
on the screensaver. I tried to make the gawk program
so it could be run more than once, without adding
command line parameters to pass to it.

*******

There is no collision detection code in the program,
as my reasoning is, it isn't necessary.

Don't for example, *manually* place the following in the
folder. This destroys my going-in assumptions about the
need for collision detection code. My little program
can't do this, without human interference to spoil my
assumptions about uniqueness.

0001__goodbye.jpg
0005__goodbye.jpg

The random change, could attempt to

ren 0001__goodbye.jpg 0005__goodbye.jpg

while "0005__goodbye.jpg" already exists. As long as
you toss names with no conflicts in there in the first place

cat.jpg
dog.jpg
squirrel.jpg

everything should work.

If adding files, you *still* want the root filename to
be unique. Here, I added an elephant to a previously-randomized
run. You can add files to the folder, just as long as
the filename was as unique as it would have been sitting
in the original folder.

If you could not copy the elephant to the original folder
without causing some other elephant to be deleted, then
placing that elephant in this working folder will also
cause a problem. In the following example, the elephant
is unique, so there is no problemo.

0001__dog.jpg
0002__cat.jpg
0003__squirrel.jpg
elephant.jpg

gives...

0001__cat.jpg
0002__elephant.jpg
0003__dog.jpg
0004__squirrel.jpg

Adding collision detection is easy too, but it's
feature creep and part of a more thorough effort.
Adding if-then-else to cure "stupid stuff" is essential
to well-written programs. This isn't a well written
program. It's a Q&D demo.

If I needed "sufficient" control over the program, I'd
write it in C code. I have stat() and other resources there.

In terms of input format, each line of text in the input.txt
is assumed to be a file name. Let's run a thought experiment...

in de gotta da vida.jpg

will be renamed to

ren in de gotta da vida.jpg 0001__in de gotta da vida.jpg

Yup, a disaster. It needs double quotes added to the printf.

ren "in de gotta da vida.jpg" "0001__in de gotta da vida.jpg"

So here is a five line "chunk" to snip out and replace,
to add the quotes. Modify the previously-posted file with this.

if (head in prechk) { # substitute out the lead chars
printf( "ren \"%s\" \"%04d__%s\"\n" , array[ m ], m, tail )
} else { # add new scheme as prefix to name
printf( "ren \"%s\" \"%04d__%s\"\n" , array[ m ], m, array[m] )
}

And here is the fix running my test case.
Now the rename has quotes to protect file names.
I'm using interactive mode to enter a two line test case here.

https://i.postimg.cc/HsFdmLmj/add-quotes.gif

I only write a couple of these pieces of code per year,
so it's not like I'm a code-writing machine or anything.
Some will "enjoy a new hobby" more than others :-)

Paul
Ads