2

I am having trouble getting them to work now. I am trying to create a text file with the random order of the files. I tried but it seems the order in the .text file is not as random as I want. Please help me give a solution. Thank you.

@echo off
setlocal EnableDelayedExpansion
set /A rand=%random%
for %%i in (*.mp4) do (
    ren "%%i" "!rand!_%%i"
)
(for %%i in (*.mp4) do @echo file '%%i') > mylist.txt

Please see the screenshot below if you find it difficult to understand.

Batch Screenshot

Anaksunaman
  • 18,227

2 Answers2

0

Two Windows 10 64-bit batch files to rename files with the same random number or rename files with a different random number.

I am not sure what you are trying to do so I've given you a couple of examples and I changed the single quote to double quotes.

BEGIN TEST

If you want every renamed file to begin with the same random number

@echo off
del mylist.txt
for %%i in (*.mp4) do echo "%%i" "%random%_%%i" >> mylist.txt
exit /b

If you want every renamed file to begin with a different random number

@echo off
setlocal EnableDelayedExpansion
del mylist.txt
REM Prevent !random! from being applied more than once.
for /f %%i in ('dir /b *.mp4') do echo "%%i" "!random!_%%i" >> mylist.txt
exit /b

END TEST

BEGIN RENAME

If you want every renamed file to begin with the same random number

@echo off
for %%i in (*.mp4) do ren "%%i" "%random%_%%i" 
dir /b *.mp4 > mylist.txt
exit /b

If you want every renamed file to begin with a different random number

@echo off
setlocal EnableDelayedExpansion
REM Prevent !random! from being applied more than once.
for /f %%i in ('dir /b *.mp4') do ren "%%i" "!random!_%%i" 
dir /b *.mp4 > mylist.txt
exit /b

END RENAME

If you are talking about how explorer sorts files try this: Windows Explorer sort by options

Find some attribute of your .mp4 that gives you the order you want. If no attribute gives you the order you want alter an attribute of every file to give you the order you want.

I think I understand what you want now.

(for /f %%i in ('dir /b /od *.mp4') do echo "%%i") > mylist.txt

Gives:

"5.mp4"
"2.mp4"
"1.mp4"
"6.mp4"
"3.mp4"
"7.mp4"
"4.mp4"

The sort order is based on the date and modification time. Your files most likely have different creation dates and modification times than my files so your results will be different. Does this give you what you are looking for?

somebadhat
  • 1,240
0

_ // Same answer for mp3 led zero in name \\ _


Considering the prevention case where the random number has already been chosen in the processing itself and some file has the same name/number, a new random will be generated to ensure that each rename accordingly to get a unique name/number ...

goto :Leed_by_Zero
@echo off & setlocal enabledelayedexpansion & title <nul & title ...\%~pn0

for /f delims^= %%a in ('where.exe ".:*.mp4"')do call :] "%%~dpa" "%%~nxa"
:^]
for /f %%f in ('set /a _num^=!random!*1000/32768+1')do set "_name_num=%%f"
if not exist "%~1_name_num!.mp4" ren "%~1%~2" "!_name_num!.mp4" 2>nul && (
exit /b ) || ( if "%~1-%~2" == "-" ( goto :^[ ) else (rem./ && goto :^] ))

:^[
>paths.txt (for /f tokens^=* %%i in ('dir/b/od *.mp4')do echo=file '%%~fi')
>names.txt (for /f tokens^=* %%j in ('dir/b/od *.mp4')do echo=file '%%~nxj')

rem./ the paths.txt file will save in for loop the full file path: file.mp4
rem./ the names.txt file only get in loop the file name.extension: file.mp4
  • Leed by Zero
@echo off & setlocal enabledelayedexpansion & title <nul & title ...\%~pn0

for /f delims^= %%a in ('where.exe ".:*.mp4"')do call :] "%%~dpa" "%%~nxa"
:^]
for /f %%f in ('set /a _num^=!random!*1000/32768+1')do set "_name_num=%%f"
call set "_name_num=000!_name_num!" &&call set "_name_num=!_name_num:~-4!"
if not exist "%~1_name_num!.mp4" ren "%~1%~2" "!_name_num!.mp4" 2>nul && (
exit /b ) || ( if "%~1-%~2" == "-" ( goto :^[ ) else (rem./ && goto :^] ))

:^[
>paths.txt (for /f tokens^=* %%i in ('dir/b/od *.mp4')do echo=file '%%~fi')
>names.txt (for /f eol^=^| %%j in ('dir/b/o:d *.mp4')do echo=file '%%~nxj')

rem./ the paths.txt file will save in for loop the full file path: file.mp4
rem./ the names.txt file only get in loop the file name.extension: file.mp4

Io-oI
  • 9,237