Windows批处理文件 - 选择(最多)文件夹中的四个随机文件 [英] Windows batch file - Pick (up to) four random files in a folder

查看:583
本文介绍了Windows批处理文件 - 选择(最多)文件夹中的四个随机文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我试图从文件夹中选取四个随机文件(壁纸),以便进一步处理。该文件夹不包含子文件夹,只有* .jpg,*。bmp和* .png(它可能包含一个Thumbs.db文件,但我已经照顾过了)。<
我用来读取循环的所有文件,类似于数组,然后我想运行另一个用于循环,用于使随机数字成为选择文件的索引。


 setlocal enabledelayedexpansion 
setwps = 1 2 3 4
set / a ind = 0

for / ftokens = * delims =%% g in('dir C:\ Wallpapers / a:-hs / b / s')do(
set / a ind + = 1
set!ind!= %% ((!!random!& 1)* 1073741824)(%b



$ $($!$!$!$!$!$!$!$!$!$!$!$!$!$!



当然回声的线只输出壁纸1是#118 - 标题: 118而不是壁纸1是#118 - 标题:C:\ Wallpapers \Miami Skyline.jpg >
所以我的具体问题是:如何在中为循环扩展一个变量? >

[注意#1:创建随机数的行需要这么长,因为它给出了一个很好的随机分布值] >
[注意#2:我需要 wps 以这种方式存储,因为有时我可能只需要三个壁纸,不一定按照数字顺序]

解决方案

传输!num! (!!!)中的%% N做为echo变量: - )

  do echo Wallpaper %% g is# %% N  - 标题:!%% N! 

几个额外的指针:


  • 正如您所写,您的DIR命令是包含文件夹。您需要添加 / AD 选项



    dir C:\壁纸/ a :-hsd / b / s


  • 您的数字变量名称适用于您现有的代码。但是,如果您尝试使用正常扩展来访问它们,那么您将遇到问题,因为%1%将作为第一批参数而不是环境变量扩展。一般情况下,你应该避免用一个数字开始一个环境变量。您可以使用:

    $ p $ c $ set $ w $ b $!$%$ g $
    ...

    echo壁纸%% g是#!num! - Title:!wp %% N!


  • 您可以通过在底部递增计数器(而不是顶部),那么你就不需要在你的随机数字中加一个(这个技巧是微不足道的,更重要的是风格)


  • 你不需要tokens = *delims =,我推荐后者。 delims =将保留整个字符串。 tokens = * 如果任何文件名包含<$ c $,那么您的代码将失败。
  • c>!
    字符,因为扩展延迟,感叹号在 %% g 的扩展过程中会被破坏,通过切换延迟扩展和使用FOR变量来传递整个ENDLOCAL屏障的值。


    这里是代码所有建议都已到位

      @echo off 
    setlocal disableDelayedExpansion
    setwps = 1 2 3 4
    set / a ind =

    for / ftokens = * delims =%% g in('dir C:\ Wallpapers / a:-hsd / b / s')do(
    setlocal enableDelayedExpansion
    for(!ind!)do(
    endlocal
    setwp %% N = %% g

    set / a ind + = 1


    setlocal enableDelayedExpansion
    for %% g in(%wps%)do(
    set / anum =(((! &安培; (!!!!)* 1073741824)+(!random!* 32768)+!random!)%%%ind%
    (!num!)%% N do echo Wallpaper %% g is#%% N - 标题:!wp %% N!


    As the title says, I'm trying to pick up to four random files (wallpapers) from a folder, for further processing. The folder does not contain subfolders, just *.jpg's, *.bmp's and *.png's (it may contain a Thumbs.db file, but I already took care of that).

    I read all the files with a for loop making something similar to an array, then I'd like to run another for loop for making the random numbers that will act as indexes for choosing the files.

    setlocal enabledelayedexpansion
    set "wps=1 2 3 4"
    set /a ind = 0
    
    for /f "tokens=* delims=" %%g in ('dir C:\Wallpapers /a:-h-s /b /s') do (
        set /a ind += 1
        set "!ind!=%%g"
    )
    
    for %%g in (%wps%) do (
        set /a "num = (((!random! & 1) * 1073741824) + (!random! * 32768) + !random!) %% %ind% + 1"
        echo Wallpaper %%g is #!num! - Title: "!!num!!"
    )
    


    Of course the line that echoes just outputs Wallpaper 1 is #118 - Title: "118" instead of Wallpaper 1 is #118 - Title: "C:\Wallpapers\Miami Skyline.jpg".

    So my specific question is: how can I double expand a variable inside a for loop?

    [Note #1: the line that creates the random number needs to be so long because it gives a good random distribution of values]
    [Note #2: I need wps to be stored that way, because sometimes I could need just three wallpapers, not necessarily in numerical order]

    解决方案

    Transfer the !num! value to a FOR variable :-)

    for %%N in (!num!) do echo Wallpaper %%g is #%%N - Title: "!%%N!"
    

    A few additional pointers:

    • As written, your DIR command is including folders. You need to add the /A-D option

      dir C:\Wallpapers /a:-h-s-d /b /s

    • Your numeric variable names work in your existing code. But you will have fits if you ever try to access them using normal expansion because %1% will expand as the 1st batch argument instead of the environment variable. In general practice you should avoid beginning an environment variable with a number. You could use:

      set wp!ind!=%%g
      ...
      echo Wallpaper %%g is #!num! - Title: "!wp%%N!

    • You can make your array 0 based by incrementing your counter at the bottom of the loop instead of the top. Then you don't need to add one to your random number. (This tip is trivial. More a matter of style.)

    • You don't need both "tokens=*" and "delims=". I recommend the latter. "delims=" will keep the entire string. tokens=* will keep the entire string after it strips leading spaces and tabs. A file name can begin with spaces.

    • Your code will fail if any of the file names contain ! characters. The exclamation will be corrupted during expansion of %%g because of delayed expansion. It is fairly easy to fix by toggling delayed expansion on and of and using a FOR variable to transfer the value across the ENDLOCAL barrier.

    Here is the code with all of the recommendations in place

    @echo off
    setlocal disableDelayedExpansion
    set "wps=1 2 3 4"
    set /a ind = 0
    
    for /f "tokens=* delims=" %%g in ('dir C:\Wallpapers /a:-h-s-d /b /s') do (
      setlocal enableDelayedExpansion
      for %%N in (!ind!) do (
        endlocal
        set "wp%%N=%%g"
      )
      set /a ind += 1
    )
    
    setlocal enableDelayedExpansion
    for %%g in (%wps%) do (
      set /a "num = (((!random! & 1) * 1073741824) + (!random! * 32768) + !random!) %% %ind%"
      for %%N in (!num!) do echo Wallpaper %%g is #%%N - Title: "!wp%%N!"
    )
    

    这篇关于Windows批处理文件 - 选择(最多)文件夹中的四个随机文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆