Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Rename only a single file at random in a target directory to 1.MP4  (Read 3235 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Trying to find a way to rename only a single file at random from a target directory to 1.MP4 so that my shortcut on my desktop points to same file name but the movie played is at random each time the batch is run.

Tried a few different methods from stuff I found online, but ran into issues with them.

This one here I was thinking about using to rewrite it into a single file targeted with a test file storing its original name so that files can take on their original name when batch is run a second time. https://www.howtogeek.com/57661/stupid-geek-tricks-randomly-rename-every-file-in-a-directory/ but this one renames all in the target directory with a random number name.

Was thinking that the best method would be one that selects a file from DIR output at random and then saves the original file name and then renames that file to 1.MP4 and then this way the shortcut at the desktop points to a random movie file and each time the batch is run the original name is written back to the last file that was named 1.MP4 and another file at random taken and renamed as 1.MP4 in the target location. However at a loss on how to get the random selection of a file name from DIR output or maybe there is a better method to use to achieve this other than a random generator that the number output maximum random value is equal to file count from DIR and some other means of selecting a file at random to be renamed as 1.MP4

Geek-9PM's MP3 prepend project had me thinking about a random selection for movies and that a movie of a static name that a shortcut name doesnt change but the file itself can change its contents as a means of random movie viewing from a shortcut on desktop.

Earlier tonight I gutted the batch from the link above to rename all files in a target to number names, but that creates all sorts of problems such as if the random generator chose  the same number more than once before the batch ends iterations, then files can be deleted by 2 files give the same name where the last rename overwrites the prior file contents.

Other issue I ran into was setting the min-max random values the min/max parameters were writing to the name when when setting the random min max to a variable and then calling to variable as !number!

Salmon Trout

  • Guest
Re: Rename only a single file at random in a target directory to 1.MP4
« Reply #1 on: August 29, 2018, 09:45:35 AM »
A few things... surely better to copy (rather than rename) a file to 1.mp4 ... ? That way, each time you go round the cycle again, you are only deleting a copy, and all your files are unchanged.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Rename only a single file at random in a target directory to 1.MP4
« Reply #2 on: August 29, 2018, 09:58:58 AM »
COOL! I like that idea. A file can be chosen at random from one directory and place into a different location then that copy renamed.

Definitely a much safer approach to this. And it would make the batch way easier not having to retain the original file name in a text file to rename that file back to its original name before adding a new file name to that text file and then renaming that file to 1.MP4

Been searching around for how to parse a DIR output and chose a file name at random from that. I was thinking that the file count can be used to set the random max value so if there was 184 files the random max would be 184 and if movies are added or deleted it can maintain to the scope of the actual file count for that directory and set the random max to that file count.

Here below is what i was messing around with last night that I tombstoned because its too dangerous and if more than one file is given the same random number file name it could destroy files.

WARNING: Anyone checking out this batch, dont run it unless you know what your doing, it will rename all files within the target directory where it is run to random number file names.

I was originally going to use this with setting min/max random scope and ifexist for 1.mp4 testing so that if a file with random number of 1 is not generated, run again until a file is renamed as 1.MP4... BUT that is too dangerous and I like the safer approach of selecting a file at random from one directory to be placed into another and then the copy renamed as 1.MP4  :)

Code: [Select]
@ECHO OFF


SETLOCAL EnableExtensions EnableDelayedExpansion


FOR /F "tokens=*" %%A IN ('DIR /A:-D /B') DO (
IF NOT %%A==%~nx0 (

SET FileExtension=%%~xA

REM concatonate new random name with prior filename's file extension
SET NewName=!RANDOM!!FileExtension!

                        REM Display to user
ECHO %%A/!NewName!

                        REM Rename with newname
RENAME "%%A" "!NewName!"


)
)
pause

Salmon Trout

  • Guest
Re: Rename only a single file at random in a target directory to 1.MP4
« Reply #3 on: August 29, 2018, 10:40:46 AM »
This should work... there seems to be a bug in the batch random number generator, where if you start a batch by double clicking it in Explorer, you always get the same "random" number, so I use a vbs helper script. None of the echo statements are essential except the one I draw attention to below, and the final PAUSE is not essential either.

@echo off
setlocal enabledelayedexpansion

REM don't delete this echo line unless you already have rando.vbs in the folder
echo randomize:wscript.echo int(((wscript.arguments(0)-1+1)*Rnd+1)) > rando.vbs

if exist 1.mp4 del 1.mp4
set nfiles=0
REM cycle through mp4 files in this folder
REM (1) to count them
for %%A in (*.mp4) do (
   echo [!nfiles!] %%A
   set /a nfiles+=1
   )
echo There are %nfiles% mp4 files in this folder
set minval=1
set maxval=%nfiles%
echo choose random number between 1 and %nfiles%
for /f "delims=" %%R in ('cscript rando.vbs %nfiles%') do set randnum=%%R
echo Chose random number: %randnum%
REM cycle through mp4 files in this folder
REM (2) to select file to copy
set tfile=0
for %%A in (*.mp4) do (
   set /a tfile+=1
   if "!tfile!"=="%randnum%" (
      echo copy "%%A" -^> 1.mp4
      copy "%%A" 1.mp4
      )
   )
pause
« Last Edit: August 29, 2018, 11:03:08 AM by Salmon Trout »

Salmon Trout

  • Guest
Re: Rename only a single file at random in a target directory to 1.MP4
« Reply #4 on: August 29, 2018, 01:17:31 PM »
A quick modification... your system's cscript.exe may be in default mode, that is showing a logo so to make sure this does not cause problems, make this change

Code: [Select]
for /f "delims=" %%R in ('cscript //nologo rando.vbs %nfiles%') do set randnum=%%R
cscript behaviour if logo is showing:

Code: [Select]
E:\randmp4>cscript rando.vbs 10
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

2

cscript behaviour if logo is NOT showing:

Code: [Select]
E:\randmp4>cscript //nologo rando.vbs 10
8

set nologo permanently (logo never shows unless you use cscript //logo)

Code: [Select]
E:\randmp4>cscript //nologo //s
Command line options are saved.

set logo show permanently (logo always shows unless you use cscript //nologo)

Code: [Select]
E:\randmp4>cscript //logo //s
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

Command line options are saved.

Salmon Trout

  • Guest
Re: Rename only a single file at random in a target directory to 1.MP4
« Reply #5 on: August 29, 2018, 02:37:23 PM »
Of course, when I say show/unshow logo 'permanently', I mean 'until you change the option'.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Rename only a single file at random in a target directory to 1.MP4
« Reply #6 on: August 29, 2018, 08:33:44 PM »
Hello Salmon... Thank You for this solution!  :)

Testing it out now and gonna relax to a random movie before bed.  8)


Update: So it works if for /f "delims=" %%R in ('cscript rando.vbs %nfiles%') do set randnum=%%R is used, but if I replace that line with for /f "delims=" %%R in ('cscript //nologo rando.vbs %nfiles%') do set randnum=%%R it doesnt create the 1.MP4 file copy

I guess it doesnt like the //nologo, so I'm running it with the line of for /f "delims=" %%R in ('cscript rando.vbs %nfiles%') do set randnum=%%R

Initially when it didnt create the 1.MP4 I was thinking that spaces in the file name may be to blame, but looking in the batch its encased in " " so it should be immune to spaces in file names. Then decided to try it without the //nologo and then it worked.

System is Windows 10 64-bit if that matters at all.

Now to watching the random episode of "My Name is Earl" that it selected from random ;D   Thanks Salmon!  8)