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-21 17:24
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] Seeking help: How to batch randomly change file names View 3,180 Replies 9
Original Poster Posted 2007-02-05 00:23 ·  中国 广东 广州 海珠区 电信
新手上路
Credits 8
Posts 3
Joined 2007-02-05 00:01
19-year member
UID 78641
Gender Male
Status Offline
Hello everyone, I'm currently facing a very tough requirement:
There are N txt files in the current directory, and I need to generate random numbers,
so that each file is renamed with a random number name, and the suffix remains unchanged.
The batch naming programs I searched for earlier can't modify like this. Especially when there are few files, the generated random numbers are very easy to repeat, and I don't know how to avoid repetition. Please help everyone.



@echo off&setlocal ENABLEDELAYEDEXPANSION
for %%a in (*.txt) do call:mytest "%%a"


:mytest

set a=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
set /a a_=(%random%+62)%%62
set /a b_=(%random%+62)%%62
set /a c_=(%random%+62)%%62
set /a d_=(%random%+62)%%62
set /a e_=(%random%+62)%%62
set /a f_=(%random%+62)%%62
echo rename %1 !a:~%a_%,1!!a:~%b_%,1!!a:~%c_%,1!!a:~%d_%,1!!a:~%e_%,1!!a:~%f_%,1!.txt
rename %1 !a:~%a_%,1!!a:~%b_%,1!!a:~%c_%,1!!a:~%d_%,1!!a:~%e_%,1!!a:~%f_%,1!.txt


[ Last edited by janeter on 2007-2-4 at 11:04 PM ]
Floor 2 Posted 2007-02-05 00:51 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
If you search the owner, you can see that there are many such posts in the forum.
By the way, ask the owner, since you are afraid of repetition, why not arrange the files in serial number form? How neat that would be. For example, 1.txt, 2.txt...
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2007-02-05 01:00 ·  中国 广东 广州 海珠区 电信
新手上路
Credits 8
Posts 3
Joined 2007-02-05 00:01
19-year member
UID 78641
Gender Male
Status Offline
Hehe, can't search it out. The requirement is to randomly rename the files in the directory regularly, so that's why I thought of making a script to execute regularly.
Floor 4 Posted 2007-02-05 01:05 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
How should you change this to generate non - repeating random numbers, haha
Floor 5 Posted 2007-02-05 01:51 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Just treat it as posting water posts












[ Last edited by ccwan on 2007-2-5 at 03:54 AM ]
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 6 Posted 2007-02-05 09:58 ·  中国 广东 广州 海珠区 电信
新手上路
Credits 8
Posts 3
Joined 2007-02-05 00:01
19-year member
UID 78641
Gender Male
Status Offline
```
@echo off
for %%a in (*.txt) do call:mytest "%%a"

:mytest
rename %1 %random%.txt



Studied hard for an afternoon, and finally I wrote an executable code, haha......
After execution, it can randomly rename, but the result is always:
The syntax of the command is incorrect.
Don't know how to do it. Thank you to everyone who has helped me, and wish you all a happy new year!
```
Floor 7 Posted 2007-02-05 11:18 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The following code can rename the txt files in the current directory to filenames between 1 and 32767, and no longer process the files that have been renamed. You can give it a try:

@echo off
for /f "delims=" %%i in ('dir /a-d /b *.txt^|findstr /v "^*\.txt$"') do call :ren_ "%%i"
pause
exit

:ren_
ren %1 %random%.txt 2>nul||goto ren_
goto :eof
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2007-02-05 11:39 ·  中国 吉林 四平 联通
高级用户
★★★
Credits 859
Posts 413
Joined 2006-08-14 21:55
19-year member
UID 60532
Status Offline
The new usage and format of `findstr /v "^[0-9]*\.txt$"` are learned
Floor 9 Posted 2007-05-10 15:32 ·  中国 广东 深圳 电信
新手上路
Credits 16
Posts 8
Joined 2007-04-27 13:00
19-year member
UID 86672
Gender Male
Status Offline
Floor 10 Posted 2007-05-10 15:57 ·  中国 新疆 乌鲁木齐 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
Named with date and time
Forum Jump: