寻找一种重新播种批次%random%的方法

发布于 2024-12-20 20:06:23 字数 248 浏览 3 评论 0原文

我正在批量寻找一个随机函数,可以将其附加到文件名的开头。我正在创建数千个符号链接,并且打算通过在文件名的开头附加一个随机数来随机化排序结果。

我在嵌套 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久光 2024-12-27 20:06:24

使用延迟扩展。即放在

setlocal enabledelayedexpansion

批处理文件的开头,然后使用 !random! 而不是 %random%。有关该主题的详细说明,请参阅帮助集

这里的要点是,当 for 循环被解析时,%random% 会被扩展;因此,对于后续迭代(即当循环实际上运行时),不再有变量,只有值。这是通过延迟扩展来解决的。

Use delayed expansion. I.e. put

setlocal enabledelayedexpansion

at the start of your batch file and then use !random! instead of %random%. See help set for a detailed explanation of the topic.

The point here is that %random% gets expanded when the for 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文