Search code examples
windowsbatch-filedirectory-structure

Create Folders based on part of file name and move files into the created folder


I am absolutely not a developer and I need your help. I looked around in this site and came across examples which I thought could match my needs but it doesn't work actually. First example or another one Second Exapmle I created this test batch file :

@ECHO OFF
SETLOCAL
SET "sourcedir=C:\Test_Directory"
PUSHD %sourcedir%
FOR /f "tokens=1* delims=-" %%a IN (
 'dir /b /a-d "*.pb*"'
 ) DO (  
 ECHO MD %%a
 ECHO MOVE "%%a-%%b" .\%%a\
)
POPD
GOTO :EOF

I have the following files created for test purposes :

  • 777 In this one I have no delimiter.pb4
  • Any easy one now & not hard to solve - There we go.pb4
  • Any easy one now & not hard to solve - There we go.pb5
  • Could be Also (The) - Something-like-this.pb4
  • Even-Worse - what to do.pb
  • Example - Easy.pb
  • Fake, File - Yet another one.pb4
  • Here, There - I don't know what to do.pb3
  • Here, There - whatever.pb3
  • This first 1 - other things here (2).pb4
  • This first 1 - other things here.pb4
  • This one move also same 777 since no delimiter.pb3

I have a delimiter that is " - ". I need to create a folder that will have the name of what is before the 3 characters delimiter, and the move to that folder all the files that start with the same name as before the delimiter.

Now the first problem is that 'delims' just accept one character. WHat shall I do since I have 3?

The second problem is that in some files this pattern " - " doesn't exist. Then the files must be moved in a directory named "0-TBC".

The third problem is when I have a "-" character alone in the file name then it doesn't match the pattern I'm looking for. In that case I should find the pattern and if it is not in the file name then the file should also be moved into the "0-TBC" folder.

The forth problem is that when I run the above batch file then the result displayed is just fine (except it doesn't solve the 2 problems above). Yet when I remove the 'ECHO' in front of 'MD' command then it takes the SPACE as the delimiter and not the '-' character and therefore it absolutely doesn't do what it is expected to do.

Sorry for being so long. And thanks a lot for your kind help.


Solution

  • You could replace the string SPACE + - + SPACE by a single character that cannot occur in file names, like |, for instance:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    
    rem // Change into root directory:
    pushd "C:\Test_Directory" && (
        rem // Loop through all matching files:
        for /F "delims= eol=|" %%F in ('
            dir /B -A:-D-H-S "*.pb*"
        ') do (
            rem // Store current file name:
            set "FILE=%%F"
            rem // Toggle delayed expansion to avoid troubles with `!`:
            setlocal EnableDelayedExpansion
            rem // Replace ` - ` by `|` and split file names there:
            for /F "tokens=1* delims=| eol=|" %%I in ("!FILE: - =|!") do (
                endlocal
                rem // Check file name:
                if "%%J"=="" (
                    rem // File name contains no ` - `:
                    ECHO md "0-TBC" 2> nul
                    ECHO move "%%F" "0-TBC\"
                ) else (
                    rem // File name does contain `|`:
                    ECHO md "%%I" 2> nul
                    ECHO move "%%F" "%%I\"
                )
            )
        )
        rem // Return from root directory:
        popd
    )
    
    endlocal
    exit /B