寻找一种重新播种批次%random%的方法
我正在批量寻找一个随机函数,可以将其附加到文件名的开头。我正在创建数千个符号链接,并且打算通过在文件名的开头附加一个随机数来随机化排序结果。
我在嵌套 for 循环中使用了这个函数(迭代所有子目录中的所有文件):
mklink "%LINKDIR%\%random%%%f" "%%f"
它几乎返回我想要的内容。不幸的是,每个符号链接都有相同的起始随机数。有没有办法重新设定 %random% 值?
I'm looking for a random function in batch that I can tack onto the beginning of a filename. I'm creating several thousand symbolic links and I intend to randomize the sorted results by appending a random number to the beginning of the filename.
I have used this function in my nested for loops (iterating through all files in all subdirs):
mklink "%LINKDIR%\%random%%%f" "%%f"
It returns almost what I want. Unfortunately each symbolic link has the same starting random number. Is there anyway to reseed the %random% value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用延迟扩展。即放在
批处理文件的开头,然后使用
!random!
而不是%random%
。有关该主题的详细说明,请参阅帮助集
。这里的要点是,当
for
循环被解析时,%random%
会被扩展;因此,对于后续迭代(即当循环实际上运行时),不再有变量,只有值。这是通过延迟扩展来解决的。Use delayed expansion. I.e. put
at the start of your batch file and then use
!random!
instead of%random%
. Seehelp set
for a detailed explanation of the topic.The point here is that
%random%
gets expanded when thefor
loop is parsed; thus for subsequent iterations (i.e. when the loop is actually run) there is no variable there anymore, just the value. This is fixed by delayed expansion.