1

I have a ton of files stored in a single directory which I wish to sort into subfolders based on the first world in their files names.

So, I have thousands of files that are .pdf files that all have different first words (7 numeric characters).

I also have thousands of folders that have different first words 7 numeric characters in the same directory.

I am trying to move all the .pdfs into folders that have the same first word (7 characters).

So I need a batch file that essentially checks a pdf file and checks in the same directory to see if a folder already exists with the same first 7 numeric characters and if it does moves the .pdf into that folder.

If a folder does not exist for the .pdf, it creates a folder based on the .pdf file's first seven characters.

So for example I have a pdf named "1617832_XXXXXXXX_XXX.pdf" and a folder named "1617832_XXXXXXXX" in the same directory, I want the "1617832_XXXXXXXX_***.pdf" to be moved into the existing folder "1617832_XXXXXXXX". However, if folder ""1617832_XXXXXXXX" does not exist, I want a folder created that is titled "1617832" (the first seven characters of file).

Afterwards doing that, it would move on to the next .pdf "4530834__XXXXXXXX_XXX.pdf" and checks the folders to see if it has a matching folder that has the same first word(seven characters) and then moves the file into that folder (if it exists otherwise creates a new folder).

The only batch that comes close but takes too long to run and doesn't create folders if they don't exist is this one:

@echo on
setlocal enabledelayedexpansion
cls

pushd c:\folders\

for /f "tokens=*" %%1 in ('dir /a-d /b *') do (
set filename=%%1&set dirname=!filename:~0,7!

for /f "tokens=*" %%A in ('dir /ad /b') do (
    set dirid=%%A&set dirid=!dirid:~0,7!

if "!dirid!" equ "!dirname!" move %%1 %%A
  )        
)

Any way to do this more efficiently and effectively?

  • 2
    if not exist %%A (md %%A) should create the directories for you ... – DavidPostill Feb 2 at 9:38
  • Thank you for reply, i tested that but it did not work. Nothing happened. I want the batch to create the folders based on the first word if one doesn't already exist. – hiten Feb 2 at 15:51
  • What should happen if there are 2 folders that match the first 7 characters of the PDF. Ex: PDF: 1234567MyPDF.PDF Folder01: 1234567Folder01 Folder02: 1234567Folder02 – Ricardo Bohner Feb 2 at 16:01
  • Actually I already did such a batch file but it wasn't PDF to copy but JPG images, an in that case the if a more than a folder mached part of the name the JPG was copied to both folders. – Ricardo Bohner Feb 2 at 16:04
  • Yes, if two folders that match the first 7 characters I would want the file to be moved to both folders that have the same first 7 characters. – hiten Feb 2 at 16:09
0

Test this:

@echo off
setlocal EnableDelayedExpansion

REM Put here the path where the PDFs are:
set "Source=%userprofile%\Desktop\test"

for %%a in ("%Source%\*.PDF") do (
set "File=%%~na"
for /d %%b in ("%Source%\*") do (
set "Folder=%%~nb"
If "!File:~0,7!"=="!Folder:~0,7!" Move "%%a" "%%b"
)
if Exist "%%a" md "!Source!\!File:~0,7!"& move /y "%%a" "!Source!\!File:~0,7!"
)

enter image description here

| improve this answer | |
  • Thank you!! This works beautifully! However, the only issue I am having now is regarding path "Source=%userprofile%\Desktop\test". When I test with a path on my desktop it works wonderfully, however when I test with the following path which is on a different drive such as "O:\Path\Operations\Blank\Blanky\Blank Blank" it does not work. I am a noob, what can be used to replace "%userprofile%" so it can be used for other drives and not just the C:\ drive? – hiten Feb 3 at 2:21
  • set "Source=O:\Path\Operations\Blank\Blanky\Blank Blank" – Ricardo Bohner Feb 3 at 2:40
  • Maybe you put something like: set "Source="O:\Path\Operations\Blank\Blanky\Blank Blank" (1 quote too much) – Ricardo Bohner Feb 3 at 2:41
  • I created this exact same path as you except that I used my pendrive which is w: not o: and It worked: set "Source=W:\Path\Operations\Blank\Blanky\Blank Blank" The quotes goes before the variable name (Source) to prevent erros in case the path has special characters... – Ricardo Bohner Feb 3 at 3:25
  • Perfect, it works!!! Thank you so much! – hiten Feb 3 at 14:27
0
  • Updated considering this comment: Yes, if two folders that match the first 7 characters I would want the file to be moved to both folders that have the same first 7 characters

You cannot use the move command because moving will make it impossible to make another copy of it (moved file) to another folder where the first 7 characters match, after being moved ... The command would have to make copies, not move files ...


@echo off && setlocal enabledelayedexpansion 

cd /d "O:\Path\Operations\Blank\Blanky\Blank Blank" && cls 

for /f tokens^=* %%i in ('dir /a:d .\* /b ')do set "_folder=%%~ni" && if exist ".\%%~ni*.pdf" (
for /f tokens^=* %%F in ('dir /a:d /b ".\!_folder:~0,7!*"')do copy ".\%%~ni*.pdf" "%%~dpnF\." )

for %%F in (*.pdf)do set "_file=%%~nF" && call set "_file=!_file:~0,7!" && (
if not exist .\!_file!\. md .\!_file!\ && copy /y "%%~dpnxF" ".\!_file!\." )

:: Deleting these files was copied to multiple folders instead of being moved to folder...
(echo/ if exist .\*.pdf del .\*.pdf /q /f ) & %__APPDIR__%timeout.exe -1 & endlocal && goto :EOF

First treat the folder and see if any files match; after that, check the remaining files in the folder and without destination


If all files have been copied to the respective folder, you need to delete this: Just remove the echo / command:


(echo/ if exist .\*.pdf del .\*.pdf /q /f ) &  %__APPDIR__%timeout.exe -1 & endlocal && goto :EOF
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.