UNSUPPORTED CODE These sample scripts were generated by Microsoft Copilot and whilst you shouldn't have any problems with them and are free to adapt them to suit your needs, they are not supported by Dinamich Ltd. Use them at your own risk, testing thoroughly on sample data before using them on your own files and folders


What's provided here

These two batch files can be used to collapse your folders so that you have one folder of emails in each filing location.


Why is this useful

Customers of legacy systems will sometimes have very granular and complex filing structures because it was the best way to ensure that email could be found. For example, the project folder for Daisy Court might have multiple sub-folders each with its own sub-folders for incoming and out going emails.


This puts pressure on staff to file to the right places and the sad reality is that they can't be relied upon to do it consistently, if only because they can't agree on which is the correct folder.


CloudFiler's Pico search tool can do the heavy lifting, most customers choose to have one email folder for each project. This makes it easier on staff because they just file to Daisy Court and don't need to hunt for the right sub-folder, and because it's easy and the benefits to them are great, staff are more likely to file their emails.



So the simplified structure might be:


P:\Projects\Daisy Court\email

P:\Projects\Dalton Viaduct\email

P:\Projects\Fulton Way\email

etc.



The Bulk Uploader is ideal for importing folders that are structured like this, but you first need to create an 'email' sub-folder for each project location and then move all emails that are below that project folder into the newly created 'email' sub-folder, which is what the script does.


Why are there two batch files?

Depending on your circumstances you may wish to collapse one set of folders at a time, or you may choose to do them in bulk.


move_files.bat - collapses one folder at a time.


process_folders.bat - processes a whole directory of folders by repeatedly calling move_files.bat on each sub-folder.


move_files.bat


Save the code below into a file named move_files.bat



@echo off

setlocal


:: Check if the correct number of arguments are provided

if "%~3"=="" (

    echo Usage: %0 PATH NAME FILTER

    exit /b 1

)


:: Assign arguments to variables

set "PATH=%~1"

set "NAME=%~2"

set "FILTER=%~3"


:: Create the sub-folder

set "DEST=%PATH%\%NAME%"

if not exist "%DEST%" (

    mkdir "%DEST%"

)


:: Find and move files

for /r "%PATH%" %%f in (%FILTER%) do (

    move "%%f" "%DEST%"

)


echo Files moved successfully to %DEST%

endlocal



It takes three arguments:


PATH - The path to the folder that need collapsing

NAME - The name that you would like to give to the sub-folder which it will put all the collected emails into e.g. email

FILTER - The filter for the files that it needs to find and move e.g. *.msg


So if you want to collapse all the MSG files below a given folder, open a Command Prompt and then run the script in the form:


move_files.bat PATH NAME FILTER


For example:

move_files.bat "P:\Projects\ProjectFolder" email *.msg



process_folders.bat

Where you have a root folder with a number of sub-folders on which you want to execute the move_files.bat, use process_folders.bat to do them in bulk.


Save the code below into a file named process_folders.bat and place it in the same folder as your move_files.bat



@echo off

setlocal


:: Check if the correct number of arguments are provided

if "%~3"=="" (

    echo Usage: %0 ROOT_FOLDER NAME FILTER

    exit /b 1

)


:: Assign arguments to variables

set "ROOT_FOLDER=%~1"

set "NAME=%~2"

set "FILTER=%~3"


:: Loop through each sub-folder in the ROOT_FOLDER

for /d %%d in ("%ROOT_FOLDER%\*") do (

    call move_files.bat "%%d" "%NAME%" "%FILTER%"

)


echo All sub-folders processed.

endlocal



It takes three arguments:


ROOT_FOLDER - The path to the folder which contains the folders that need collapsing

NAME - The name that you would like to give to the sub-folder which it will put all the collected emails into e.g. email

FILTER - The filter for the files that it needs to find and move e.g. *.msg


You use it as follows: 


process_folders.bat ROOT_FOLDER NAME FILTER


So if you want to collapse all the project folders so that they each have a sub-folder called email that contains all of the MSG files, you would open a Command Prompt and then run the script:


process_folders.bat "P:\Project\Root\Folder" email *.msg