Home > Blockchain >  How to organize files in folders
How to organize files in folders

Time:09-09

I have this script that currently works partially. Step 1 (Working): More precisely, it groups all my .mp4 files found in the directory. I choose the number of .mp4 files in each folder. so far all good.

Step 2 (Not Working): When I try to move the folders created together with the mp4 files inside and group them by 15 or as many as I need, I can't find the optimal solution.

@echo off
setlocal enabledelayedexpansion
PushD %~dp0
set "filesInfolder=60"
set "foldersInfolders=15"
set "fcount=0"
set "fcounta=0"
set idx=0
set idxa=0

REM here all the .mp4 files will be grouped 60 in each folder

for /F "delims=" %%I in ('dir /a-d /o:n /b *.mp4') do (
    REM echo Processing %%I
    set /a idx=idx %% filesinfolder  1
    if !idx! equ 1 set /a fcount =1
    md Videos-!fcount! 2>nul
    move "%%I" "Videos-!fcount!\"

  ) 
 
REM here I need the folders created together with the .mp4 files to be grouped by 15 in other folders.

for /D "delims=" %%A in (*.*) do  (
    REM echo Processing %%A
    set /Y idxa=idxa %% foldersinfolders  1
    if !idxa! equ 1 set /a fcounta =1
    md Canal-!fcounta! 2>nul
    move "%%~fA" "Canal-!fcounta!\"

  
)
 

Everything happens in a loop until all the .mp4 files are finished

CodePudding user response:

I managed to make it work. I am attaching the version of the code in this form and I will also come up with a version with a single for loop.

If someone wants to optimize this solution, it is free and I really encourage it. Thank you very much!

UPDATE: I made an optimized version. Thanks to @Mofi

@echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd "%~dp0"
set "filesInfolder=60"
set "foldersInfolders=15"
set "fcount=0"
set "fcounta=0"
set idx=0
set idxa=0

 
for /F "delims=" %%I in ('dir /a-d /o:n /b *.mp4') do (
    REM echo Processing %%I
    set /a idx=idx %% filesinfolder  1
    if !idx! == 1 set /a fcount =1
    md Videos-!fcount! 2>nul
    move "%%I" "Videos-!fcount!\"

  ) 
 
for /D %%A in (*.*) do  (
    REM echo Processing %%A
    set /a idxa=idxa %% foldersinfolders  1
    if !idxa! == 1 set /a fcounta =1
    md Canal-!fcounta! 2>nul
    move "%%~fA" "Canal-!fcounta!\"

  
)
endlocal
 

Later edit:

Now the directories ordered alphabetically by name. Videos-1, Videos-10, Videos-11, ... Video-19 I need them in a natural alphanumeric order

I found a temporary solution that renames all my internal folders in a row, but at the end I will explain in a folder structure how will be more nice for me to be.

I just modified:

md Videos-!fcount! 2>nul
move "%%I" "Videos-!fcount!\" 

and made it

md Videos-!fcount!-123 2>nul
move "%%I" "Videos-!fcount!-123\" 

I have the files in MP4-files folder

───MP4-files Folder
│   ├───file.mp4 - (1)
│   ├───file.mp4 - (10)
│   ├───file.mp4 - (11)
│   ├───file.mp4 - (2)
│   ├───file.mp4 - (3)
│   ├───file.mp4 - (4)
│   ├───file.mp4 - (5)
│   ├───file.mp4 - (6)
│   ├───file.mp4 - (7)
│   ├───file.mp4 - (8)
│   └───file.mp4 - (9)

Here I run the batch file above (with that small modification -123 ) and all .mp4 files are moved first in Videos-1, Videos-2 ... Videos- xx , depends how many .mp4 files I have in folder. Each of the Videos folders have mp4 files in it (deppends how many I wrote in script) set "filesInfolder=60" is now, but can be different.

After that the folders Videos-1 .. Videos-xx are grouped in other folders with the name of "Canal-1 .. Canal-10 .. Canal-x" in order. the number of folders deppends on how many Videos folders. How many I group is here set "foldersInfolders=15" can be 15 or other number.

I get this:

─MP4-files Folder
│       ├───Canal-1 ->  (set "foldersInfolders=15")
│       │   ├───Videos-1-123  (inside are the .mp4 files) -> set "filesInfolder=60"
│       │   ├───Videos-10-123
│       │   ├───Videos-100-123
│       │   ├───Videos-101-123
│       │   ├───Videos-102-123
│       │   ├───Videos-103-123
│       │   ├───Videos-104-123
│       │   ├───Videos-105-123
│       │   ├───Videos-106-123
│       │   ├───Videos-107-123
│       │   ├───Videos-108-123
│       │   ├───Videos-109-123
│       │   ├───Videos-11-123
│       │   ├───Videos-110-123
│       │   └───Videos-111-123
│       ├───Canal-2
│       │   ├───Videos-12-123
│       │   ├───Videos-13-123
│       │   ├───Videos-14-123
│       │   ├───Videos-15-123
│       │   ├───Videos-16-123
│       │   ├───Videos-17-123
│       │   ├───Videos-18-123
│       │   ├───Videos-19-123
│       │   ├───Videos-2-123
│       │   ├───Videos-20-123
│       │   ├───Videos-21-123
│       │   ├───Videos-22-123
│       │   ├───Videos-23-123
│       │   ├───Videos-24-123
│       │   └───Videos-25-123
│       ├───Canal-3
│       │   ├───Videos-26-123
│       │   ├───Videos-27-123
│       │   ├───Videos-28-123
│       │   ├───Videos-29-123
│       │   ├───Videos-3-123
│       │   ├───Videos-30-123
│       │   ├───Videos-31-123
│       │   ├───Videos-32-123
│       │   ├───Videos-33-123
│       │   ├───Videos-34-123
│       │   ├───Videos-35-123
│       │   ├───Videos-36-123
│       │   ├───Videos-37-123
│       │   ├───Videos-38-123
│       │   └───Videos-39-123
 

Then I run the following batch file:

@Echo off
  
set Cnt=0
for /f "delims=" %%A in (
  'dir /B /S /AD Videos-* ^| findstr /iV "^Videos-[0-9][0-9]*$" '
) Do Call :RenInc "%%A"
PopD    
Goto :Eof

:RenInc
Set /A Cnt =1
if Exist "Videos-%Cnt%" goto :RenInc
Ren "%~1" "Videos-%Cnt%"

and I get the following folder structure:

──Video-15-sec
│       ├───Canal-1
│       │   ├───Videos-1
│       │   ├───Videos-2
│       │   ├───Videos-3
│       │   ├───Videos-4
│       │   ├───Videos-5
│       │   ├───Videos-6
│       │   ├───Videos-7
│       │   ├───Videos-8
│       │   ├───Videos-9
│       │   ├───Videos-10
│       │   ├───Videos-11
│       │   ├───Videos-12
│       │   ├───Videos-13
│       │   ├───Videos-14
│       │   └───Videos-15
│       ├───Canal-2
│       │   ├───Videos-16
│       │   ├───Videos-17
│       │   ├───Videos-18
│       │   ├───Videos-19
│       │   ├───Videos-20
│       │   ├───Videos-21
│       │   ├───Videos-22
│       │   ├───Videos-23
│       │   ├───Videos-24
│       │   ├───Videos-25
│       │   ├───Videos-26
│       │   ├───Videos-27
│       │   ├───Videos-28
│       │   ├───Videos-29
│       │   └───Videos-30
│       ├───Canal-3
│       │   ├───Videos-31
│       │   ├───Videos-32
│       │   ├───Videos-33
│       │   ├───Videos-34
│       │   ├───Videos-35
│       │   ├───Videos-36
│       │   ├───Videos-37
│       │   ├───Videos-38
│       │   ├───Videos-39
│       │   ├───Videos-40
│       │   ├───Videos-41
│       │   ├───Videos-42
│       │   ├───Videos-43
│       │   ├───Videos-44
│       │   └───Videos-45
│       ├───Canal-4

It is nice, but more nice for me if i can get the following folder structure after I run 1 single time the first Batch file attached in this Answer.

──Video-15-sec
│       ├───Canal-1
│       │   ├───Videos-1
│       │   ├───Videos-2
│       │   ├───Videos-3
│       │   ├───Videos-4
│       │   ├───Videos-5
│       │   ├───Videos-6
│       │   ├───Videos-7
│       │   ├───Videos-8
│       │   ├───Videos-9
│       │   ├───Videos-10
│       │   ├───Videos-11
│       │   ├───Videos-12
│       │   ├───Videos-13
│       │   ├───Videos-14
│       │   └───Videos-15
│       ├───Canal-2
│       │   ├───Videos-1
│       │   ├───Videos-2
│       │   ├───Videos-3
│       │   ├───Videos-4
│       │   ├───Videos-5
│       │   ├───Videos-6
│       │   ├───Videos-7
│       │   ├───Videos-8
│       │   ├───Videos-9
│       │   ├───Videos-10
│       │   ├───Videos-11
│       │   ├───Videos-12
│       │   ├───Videos-13
│       │   ├───Videos-14
│       │   └───Videos-15
│       ├───Canal-3
│       │   ├───Videos-1
│       │   ├───Videos-2
│       │   ├───Videos-3
│       │   ├───Videos-4
│       │   ├───Videos-5
│       │   ├───Videos-6
│       │   ├───Videos-7
│       │   ├───Videos-8
│       │   ├───Videos-9
│       │   ├───Videos-10
│       │   ├───Videos-11
│       │   ├───Videos-12
│       │   ├───Videos-13
│       │   ├───Videos-14
│       │   └───Videos-15
│       ├───Canal-4

Thank you a lot

CodePudding user response:

There could be used the following batch file to organize the video files in directory of the batch file by moving them to dynamically created directories as long as no MP4 file contains an exclamation mark in file name.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0"
set "FilesInFolder=60"
set "FoldersInFolders=15"
set "FileCount=%FilesInFolder%"
set "CanalIndex=0"
set "VideosIndex=%FoldersInFolders%
if not exist "*!*.mp4" goto MoveFiles
echo ERROR: Moving video files not possible because of file names with ! in name.
echo/
echo Please rename first the following files(s):
echo/
dir *!*.mp4 /A-D /B
echo/
pause
goto EndBatch

:MoveFiles
setlocal EnableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir *.mp4 /A-D /B /ON 2^>nul') do (
    if !FileCount! == %FilesInFolder% (
        set FileCount=0
        if !VideosIndex! == %FoldersInFolders% (
            set /A CanalIndex =1
            set VideosIndex=1
        ) else set /A VideosIndex =1
        set "TargetFolder=Canal-!CanalIndex!\Videos-!VideosIndex!"
        md "!TargetFolder!" 2>nul
    )
    move /Y "%%I" "!TargetFolder!\" >nul
    set /A FileCount =1
)
endlocal

:EndBatch
popd
endlocal

The environment variable TargetFolder with the two dynamic changed numbers CanalIndex and VideosIndex can be further modified to the wanted format.

Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.

Please note that the ordering of the MP4 files by name caused by option /ON by command DIR is strictly alphabetic and not natural alphanumeric. The command line executed by FOR by starting one more cmd.exe in background with option /c and the command line appended as argument string(s) could be modified to get MP4 file names output and so processed next in a different order.

Example of the FOR command line with moving MP4 files with the file names file (1).mp4, ... file (9).mp4, file (10).mp4, ... file (99).mp4, file (100).mp4, ... file (999).mp4, file (1000).mp4, ... file (9999).mp4:

for /F "eol=| delims=" %%I in ('dir *.mp4 /A-D /B /ON 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /R "([0-9])\.mp4$" ^& dir *.mp4 /A-D /B /ON 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /R "([0-9][0-9])\.mp4$" ^& dir *.mp4 /A-D /B /ON 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /R "([0-9][0-9][0-9])\.mp4$" ^& dir *.mp4 /A-D /B /ON 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /R "([0-9][0-9][0-9][0-9])\.mp4$"') do (

Please note that [0-9] matches also the characters ¹²³ which should be no problem here.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • dir /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • move /?
  • pause /?
  • popd /?
  • pushd /?
  • set /?
  • setlocal /?
  • Related