China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-22 05:54
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » A question about randomly obtaining file names. DigestI View 17,409 Replies 54
Floor 16 Posted 2006-07-27 20:38 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
I rewrote one, without text queries, using clearing the variables already written into the list to judge the loop-generated random numbers.
137 files completed within 30 seconds (if the random numbers are lucky then it will be faster; later on there are fewer valid variables so it gets slower):
don't include the red parts

pause
cls
@echo off
copy nul List.txt >NUL
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('dir/b *.mp3^|find ".mp3"') do (
set /a N=!N! + 1
set Name!N!=%%a)

echo.
echo Randomly generating the list, please wait...
echo.
echo Serial number - random filename
echo.
:loop
set N=%random:~-3%
if "%N%" == "000" goto loop
if "%N:~0,1%" == "0" set N=%N:~1%
if "%N:~0,1%" == "0" set N=%N:~1%
if %N% GTR 137 goto loop“137” file count
echo set Name=%%Name!N!%%>Temp.batcheck whether this variable has been cleared
call Temp.bat
if "%Name%" == "" goto loop

set /a M=%M%+1
echo echo %%M%% - %%Name!N!%%>Temp.batdisplay serial number - random filename
echo echo %%Name!N!%%^>^>List.txt>>Temp.bat
call Temp.bat
set Name!N!=clear the variable already written into the list
if %M% GEQ 137 goto End“137” file count
goto loop

:End
del Temp.bat
echo.
echo Done!
echo.
pause
exit


Seems there is a small problem: sometimes it exits with failure:(

[ Last edited by zxcv on 2006-7-28 at 01:19 ]
Floor 17 Posted 2006-07-27 20:57 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Here's one that automatically determines the file count and sets the random digit length:
@echo off
pause
cls

for /f "tokens=1 delims= " %%i in ('dir *.mp3^|find "个文件"') do set 文件数量=%%i
if %文件数量% GEQ 100 set X=3
if %文件数量% GEQ 100 set NN=000
if %文件数量% LSS 100 set X=2
if %文件数量% LSS 100 set NN=00
if %文件数量% LSS 10 set X=1
if %文件数量% LSS 10 set NN=0
copy nul List.txt >NUL
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('dir/b *.mp3^|find ".mp3"') do (
set /a N=!N! + 1
set Name!N!=%%a)

echo.
echo Randomly generating the list, please wait...
echo.
echo Serial number - random (%文件数量% files)
echo.

echo :loop>Loop.bat
echo set N=%%random:~-%X%%%>>Loop.bat
echo if "%%N%%" == "%NN%" goto loop>>Loop.bat
echo if "%%N:~0,1%%" == "0" set N=%%N:~1%%>>Loop.bat
echo if "%%N:~0,1%%" == "0" set N=%%N:~1%%>>Loop.bat
echo if %%N%% GTR %文件数量% goto loop>>Loop.bat
echo echo set Name=%%%%Name%%N%%%%%%^>Temp.bat>>Loop.bat
echo call Temp.bat>>Loop.bat
echo if "%%Name%%" == "" goto loop>>Loop.bat

:loop
call Loop.bat
set /a M=!M!+1
echo echo %%M%% - %%Name!N!%%>Temp.bat
echo echo %%Name!N!%%^>^>List.txt>>Temp.bat
call Temp.bat
set Name!N!=
if %M% GEQ %文件数量% goto :End
goto loop

:End
del Loop.bat
del Temp.bat
echo.
echo Done!
echo.
pause
exit
Floor 18 Posted 2006-07-27 21:44 ·  中国 广东 中山 广东瑞江科技有限公司BGP数据中心
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
With so many people's code here, it should already be enough...

I'll take it all down and study it slowly, and hopefully piece together something faster and more efficient... ^_^

Thanks, everyone...
Floor 19 Posted 2006-07-27 22:20 ·  中国 辽宁 葫芦岛 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Re voiL
Here, the execution result is different every time, and there are no "numbers" before the filenames. Please post your results so I can take a look.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 20 Posted 2006-07-27 22:42 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Moderator 无奈何's is pretty good, and fast too
But filenames with spaces get cut off (incomplete); it should probably be possible to revise it a bit
Floor 21 Posted 2006-07-27 23:00 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by doscc at 2006-7-27 20:38:
My code should not produce duplicate lines
Because the filtering is done in the following line
echo %y% | findstr /r "\<%r%\>" >NUL || set y=%y% %r% & call :ls %r%
y records each different random number
When r does not appear in y, then r is recorded and :ls is called to append line r from tem.txt into list.txt


  I more or less figured out your filtering rule.

  There is a problem in your code in post #16. I don't know whether the red text is comments or code; if it's code, then after execution, list.txt will contain a large number of lines whose contents are “ clear the variable already written into the list”; if it's comments, then after removing the comment part, list.txt will contain a large number of lines whose contents are "ECHO is off."

  You said this code sometimes exits with failure. I guess it's because there are too many lines to process, leading to too many name!N! variables, and CMD seems to have a limit on the number of variables.

[ Last edited by namejm on 2006-7-28 at 09:13 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 22 Posted 2006-07-27 23:09 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by zxcv at 2006-7-27 22:42:
Moderator 无奈何's is pretty good, and fast too
But filenames with spaces get cut off (incomplete); it should probably be possible to revise it a bit


  Other than the one place in post #9, where else is there an issue in Moderator 无奈何's code? I also want to take it and run it.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 23 Posted 2006-07-27 23:40 ·  中国 北京 海淀区 IDC机房
中级用户
★★
Credits 256
Posts 93
Joined 2006-03-26 22:12
20-year member
UID 52853
Gender Male
From 广东
Status Offline
Originally posted by namejm at 2006-7-27 23:00:


  I more or less figured out your filtering rule.

  There is a problem in your code in post #16. I don't know whether the red text is comments or code; if it's code, then after execution, list.txt will contain a large number of lines whose contents are “ clear the variable already written into the list”; if it's comments, then list.txt will contain a large number of lines whose contents are "ECHO is off."

  You said this code sometimes exits with failure. I guess it's because there are too many lines to process, leading to too many name!N! variables, and CMD seems to have a limit on the number of variables. There is a problem in your code in post #16. I don't know whether the red text is comments or code; if it's code, then after execution, list.txt file釠...

The code in post #16 is by zxcv.

Post #11 is my code. Updated now!
Floor 24 Posted 2006-07-27 23:51 ·  中国 辽宁 葫芦岛 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Re zxcv
Thanks for pointing it out. Filenames containing spaces really do have a problem. Here's a simple fix.
Re namejm
Please see the corrected code in this post.


  1. @echo off
  2. if "%1" NEQ "$" (
  3. for /f "tokens=1,2 delims=:" %%a in ('"%~0" $^|sort') do @echo %%b
  4. ) else for /f "delims=" %%i in ('dir /b /a-d') do @call :sub %%i
  5. goto :EOF
  6. :sub
  7. echo %random%:%*
  8. goto :EOF
Posted by 无奈何 2006-07-27 23:44
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 25 Posted 2006-07-28 00:00 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Moderator 无奈何's code is very fast. With 137 files it took less than 3 seconds.
@echo off
copy nul List.txt >NUL

if "%1" NEQ "$" (

for /f "tokens=1,2 delims=:" %%a in ('"%~0" $^|sort') do @echo %%b>>List.txt

) else for /f "delims=" %%i in ('dir /b /a-d *.mp3') do @call :sub %%i

goto :EOF

:sub

echo %random%:%*

goto :EOF


[ Last edited by zxcv on 2006-7-28 at 00:02 ]
Floor 26 Posted 2006-07-28 00:50 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Re doscc:

  sorry, I misread your code. My bad.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 27 Posted 2006-07-28 01:11 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Originally posted by namejm at 2006-7-27 23:00:


  I more or less figured out your filtering rule.

  There is a problem in your code in post #16. I don't know whether the red text is comments or code; if it's code, then after execution, list.txt file釠...

The text after that is comments, don't put it in the BAT

I tested name!N! variables and even 548 was no problem. I guess it's
echo set Name=%%Name!N!%%>Temp.bat
call Temp.bat
This part sometimes doesn't have time to respond

[ Last edited by zxcv on 2006-7-28 at 01:18 ]
Floor 28 Posted 2006-07-28 09:12 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Re zxcv:

  If your red text is comments, then that comment format is wrong, and in some places it can even cause execution errors. For example, the line set Name!N!=clear the variable already written into the list will assign the value "clear the variable already written into the list" to the variable Name!N!. I hope you can correct it.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 29 Posted 2006-07-28 09:25 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
  Moderator 无奈何's code in post #25 is extremely concise, but also extremely puzzling. I stared at it for ages and still couldn't make heads or tails of it. Could it really be like what he once claimed before, that code should be written as concisely and as obscurely as possible? If it's just concise, that would be a blessing for people like us; but if you add “as obscure and hard to understand as possible,” good grief, I guess a whole batch of newbies like me will faint on the spot. I hope Moderator 无奈何 can give a little explanation of your code, so we can understand your train of thought. I don't feel satisfied with just the few words in post #9.

  I found an interesting phenomenon: if you remove the opening line @echo off from the code in post #24, the contents of list.txt will increase greatly, with added content in the form of "\current folder name>echo 20454". Generally speaking, if there are N files, then this kind of content will also increase by N lines. Logically, the @echo off statement is only supposed to suppress echoing, but in this piece of code it actually affects the execution result, which is rather baffling. By the way, removing the @ in @echo %%b>>List.txt and @call :sub %%i in the code seems not to affect how the code behaves when executed. I don't know why Moderator 无奈何 still keeps the @.

[ Last edited by namejm on 2006-7-28 at 10:59 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 30 Posted 2006-07-28 09:42 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Originally posted by namejm at 2006-7-28 09:12:
Re zxcv:

  If your red text is comments, then that comment format is wrong, and in some places it can even cause execution errors. For example, the line set Name!N!=clear the variable already written into the list will栮..

Honestly, I added those when posting to explain what each line does. They were never in the real BAT to begin with (which is why I said: don't include the red parts); but you should be able to tell which parts are not supposed to be there, right?

I don't really know how to add comments afterward without affecting the code
Forum Jump: