1

I have cracked my head up...

Now I have a folder containing empty subfolders and pictures.

These pictures are randomly named as their default name generated by the camera.

If I want to sort these pictures out into a few batches and then move them into the subfolders

For example:

lets say in total I have 100 pictures and five 5 subfolders.

first 20 pictures into subfolder_1

subsequent 25 pictures into subfolder_2

subsequent 23 pictures into subfolder_3

subsequent 12 pictures into subfolder_4

lastly the remaining 20 pictures into subfolder_5

So, I am thinking of doing it in loops. and since the number of pictures are not constant, and I intend to prompt the user to define number of pictures to be moved each time.

The main thing that I couldn't figure out is that HOW DO I CONTROL THE NUMBER OF LOOPS TO BE DONE?

I know about using GOTO function to break a FOR loop. But I am not sure how to do it in my case.

In fact, I am now still stucked with this concept I currently have, where I tried to use a shorter FOR loop to contain an longer FOR loop like this:

(this is just to try for the first 20 pictures into subfolder_1)

FOR /L %%A in (1,1,20) Do (

FOR %%B in ("%dir_of_folder_which_contains_the_pictures_and_subfolders%\*") Do (

MOVE *.jpg subfolder_1

)

)

These codes don't work. Perhaps it has got to be using GOTO function? Can anyone help? THANKS ALOT..

LCS
  • 13
  • 5
  • OK. I figured the codes that I have shown in my question are total craps because that just simply tell the computer to repeat my inner FOR loops 20 times! – LCS Jun 18 '14 at 08:15

2 Answers2

1
@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

Not the most efficient code, but this is a basic skeleton to build from. The move command has been prefixed with a echo for testing. If the output to console is correct, remove the echo.

MC ND
  • 69,615
  • 8
  • 84
  • 126
0

This type of problems may be solved in a better way with the use of data structures, like arrays or lists. For example:

@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!
   )
)

For further details, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108