0

I'm trying to group files arranged in some order so that I can zip them. I have m*n files in my folder and wish to move n files to m folders such that they're arranged in alphabetical order (or date created order, whichever is doable). So for files {A,B,C,D,E,F} I wish to create two new folders 1 and 2 such that 1 contains {A,B,C} and 2 contains {D,E,F}. If there's code to do the same thing on a mac, that would be great too, but I'm primarily looking to do this on windows.

I found a similar question, however I'm not able to create new folders within the loop or move the files to them.

@echo off
set /a "filesPerChunk=3, idx=0"
set /a fcount=1

for /F "delims=" %%I in ('dir /a-d /b') do (
    echo Processing %%I
    set /a "idx=idx+1"
    set /a "mod=idx %% filesPerChunk"
    if !mod! equ 0 (
    md File-%fcount%
        echo --- END OF CHUNK %fcount% ---
        pause
    )
    move I File-%fcount%
    if !mod! equ %filesPerChunk%-1(
    set /a fcount+=1
    )
)

I was trying to extract values from fcount to make new folders, but as I mentioned, I don't have a lot of experience writing batch scripts so I don't know if this even makes sense.

EDIT: So I had to move a few commands here and there because I realized I wouldn't be able to move the files if I had just the final check when creating the file, but I'm not sure if it works

Community
  • 1
  • 1
Sam
  • 3
  • 2
  • 1
    to get help with your code, please show your code. – Stephan Mar 16 '20 at 18:41
  • I don't have any code to start with, I was working with what they had in the question I put a link to above. I'm not familiar with batch scripts ar all and am trying to do this so that I can upload feedback to students on my all-of-a-sudden online courses. – Sam Mar 16 '20 at 18:55
  • I am just asking for help and thought posting code I wrote, which is mostly using the one in the link above wouldn't help a lot. I've added what I have if that helps – Sam Mar 16 '20 at 19:36
  • you have enabled [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028), but you don't use it consequently. `fcount` has to be delayed too. (And you forgot to move the files) – Stephan Mar 16 '20 at 19:41
  • I'm still trying to make the folders, the moving part shouldn't be hard but I'll try fixing that line and see if it works, thanks – Sam Mar 16 '20 at 19:48
  • don't forget to update the question with your changed code (it doesn't look that bad so far) – Stephan Mar 16 '20 at 19:50
  • `dir /s` lists files recursive (including files in sub (and sub-sub...) folders). Is that what you want? Also add `/a-d` to ignore folders and show files only. – Stephan Mar 16 '20 at 19:52
  • I changed the command but I realized I'll have to update fcount after I'm done moving. I don't want to recursively list the files no, I'll change that to /a-d instead – Sam Mar 16 '20 at 20:02

1 Answers1

1

I took the freedom to slightly simplify your logic:

@echo off
setlocal enabledelayedexpansion

set "filesPerChunk=3"
set "fcount=0"
set idx=0

for /F "delims=" %%I in ('dir /a-d /b') do (
    REM echo Processing %%I
    set /a idx=idx %% filesperchunk +1
    if !idx! equ 1 set /a fcount+=1
    ECHO md File-!fcount! 2>nul
    ECHO move "%%I" "File-!fcount!\"
    REM pause
)

After verifying it does what you want, remove both ECHO commands to actually enable the md and move commands.

Note: you can choose the sort order of your files with the /o switch. See dir /? for details.

Stephan
  • 53,940
  • 10
  • 58
  • 91