Loops 自动将一定数量的图片移动到CMD中相应的特定文件夹中

Loops 自动将一定数量的图片移动到CMD中相应的特定文件夹中,loops,batch-file,Loops,Batch File,我把头撞碎了 现在我有了一个包含空子文件夹和图片的文件夹 这些图片被随机命名为相机生成的默认名称 如果我想将这些图片分为几批,然后将它们移动到子文件夹中 例如: 假设我总共有100张图片和5个子文件夹 子文件夹_1中的前20张图片 将随后的25张图片放入子文件夹_2 随后将23张图片放入子文件夹_3 随后将12张图片放入子文件夹_4 最后,将剩余的20张图片放入子文件夹_5 所以,我在考虑做循环。由于图片的数量不是恒定的,我打算提示用户定义每次要移动的图片数量 我无法弄清楚的主要问题是,如何控制

我把头撞碎了

现在我有了一个包含空子文件夹和图片的文件夹

这些图片被随机命名为相机生成的默认名称

如果我想将这些图片分为几批,然后将它们移动到子文件夹中

例如:

假设我总共有100张图片和5个子文件夹

子文件夹_1中的前20张图片

将随后的25张图片放入子文件夹_2

随后将23张图片放入子文件夹_3

随后将12张图片放入子文件夹_4

最后,将剩余的20张图片放入子文件夹_5

所以,我在考虑做循环。由于图片的数量不是恒定的,我打算提示用户定义每次要移动的图片数量

我无法弄清楚的主要问题是,如何控制要执行的循环数

我知道如何使用GOTO函数来中断FOR循环。但我不知道如何在我的情况下做到这一点

事实上,我现在仍然对这个概念感到困惑,我尝试使用一个较短的FOR循环来包含一个较长的FOR循环,如下所示:

这只是尝试将前20张图片放入子文件夹_1

对于1,1,20 Do中的/L%%A

对于包含图片和子文件夹%\*的\u文件夹的%dir\u中的%%B

移动*.jpg子文件夹_1

这些代码不起作用。也许它必须使用GOTO函数?有人能帮忙吗?非常感谢

@echo off

    setlocal enableextensions enabledelayedexpansion

    set "folder=%cd%"

    rem For each of the present subfolders
    for /d %%a in ("%folder%\*") do (

        rem Count the number of remaining files
        set "nFiles=0"
        for /f %%b in ('dir "%folder%\*" /a-d /b 2^>nul ^| find /c /v ""') do set "nFiles=%%b"
        if !nFiles! lss 1 goto :done

        rem Ask the number of files to move
        echo(
        echo(There are !nFiles! files left. How many to move to %%a ?
        set "nFiles=0"
        set /p "nFiles="
        set /a "nFiles+=0" 2>nul

        rem Move the indicated number of files
        if !nFiles! gtr 0 for %%c in ("%folder%\*") do if defined nFiles (
            echo move "%%~fc" "%%~fa" 
            set /a "nFiles-=1"
            if !nFiles! equ 0 set "nFiles="
        )
    )

:done
    endlocal
    exit /b

虽然不是最有效的代码,但这是构建的基本框架。move命令的前缀为echo,用于测试。如果控制台的输出正确,请删除回显。

使用数据结构(如数组或列表)可以更好地解决此类问题。例如:

@echo off
setlocal EnableDelayedExpansion

rem Initialize counters
set /A numFolders=0, numFiles=0

rem Save file names in an array
for %%a in (*.*) do (
   set /A numFiles+=1
   set "file[!numFiles!]=%%a"
)

rem Save folder names in a list
set "list="
for /D %%a in (*) do (
   set /A numFolders+=1
   set "list=!list! %%a"
)

rem Ask the user for the distribution
:askDistribution
echo There are %numFiles% files and %numFolders% folders
echo Enter the number of files for each folder (must sum %numFiles%)
echo Folders: %list%
set /P "distribution=Files:    "
set total=0
for %%a in (%distribution%) do set /A total+=%%a
if %total% neq %numFiles% goto askDistribution

rem Distribute the files
set i=0
for %%n in (%distribution%) do (
   rem Get current folder and shift the rest
   for /F "tokens=1*" %%a in ("!list!") do (
      set folder=%%a
      set list=%%b
   )
   rem Move the files
   for /L %%i in (1,1,%%n) do (
      set /A i+=1
      for /F %%i in ("!i!") do ECHO move "!file[%%i]!" !folder!
   )
)

有关更多详细信息,请参阅:

OK。我想我在问题中显示的代码完全是垃圾,因为这只是告诉计算机重复我的内部循环20次!