为什么这个脚本实际上并不是应该随机化?

时间:2016-06-25 16:15:03

标签: batch-file random

以下代码应从 let fr:NSFetchRequest<TestEntity>=TestEntity.fetchRequest() do { let results=try self.moc!.fetch(fr) if results.count==0 { print("no resutls. i need to add something") let newTestEntity=TestEntity(context: self.moc!) newTestEntity.testAtt="testovaci text" do { try self.moc!.save() }catch{ } }else{ print("already some results") for result in results { print(result.testAtt!) } } }catch { print(error) } 中的每个子目录中取出1个文件并将其放入d:\test。它这样做但随机化部分不起作用。每个文件夹中的每个文件都会根据创建日期进行挑选,每个文件夹中的最后一个“已创建”文件始终是f:\source中显示的文件。

任何想法为什么?

f:\source

1 个答案:

答案 0 :(得分:0)

所有 %VariableName%已由Windows命令处理器在解析第一个 FOR 循环的整个命令块时进行了扩展。其中包括%random%%rand%

我建议使用子程序尽可能避免延迟扩展,并使代码更简单。

@echo off
pushd d:\test
for /D %%i in (*) do (
    cd "%%i"
    call :CopyRandomFile
    cd ..\
)
popd
pause
goto :EOF

:CopyRandomFile
setlocal EnableDelayedExpansion
set "FileNumber=0"
for %%f in (*.*) do (
    set /A FileNumber+=1
    set "File[!FileNumber!]=%%f"
)
set /A "rand=(FileNumber * %random%) / 32768 + 1"
copy "!File[%rand%]!" f:\source
endlocal
goto :EOF

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /?
  • cd /?
  • copy /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • pause /?
  • popd /?
  • pushd /?
  • set /?
  • setlocal /?