0

So I have a ton of folders in a single directory (around 12,000) , many of which have the same first 7 seven numeric characters. I want to merge the folders that have the same first 7 characters without manually going through and searching for the folders since like I said, I have thousands of folders in the same directory.

I need a batch file that essentially checks a folder and checks in the same directory to see if folders have the same first 7 numeric characters and if they do, the batch merges all folders and the output folder is named based on the folders' first 7 numeric characters. For example, say there is a folder titled "1234567_National" and another folder titled "1234567_Nat" and a third folder titled "1234567_HIT" I want all three folders and their contents merged into a folder called 1234567

Afterwards, it would move on to the next folder and so on..

Update - This is what I've come up with but it is only merging folders that have "_" after the first word. I am trying to merge folders based on the first seven numeric characters regardless of any character that comes after.

    @Echo off
pushd "C:\Users\sa27964\Desktop\Testing folder"
for /f "Tokens=1* Delims=_" %%A in (
  'Dir /B /AD *_*'
) Do If Not Exist "%%A" (
  Ren "%%A_%%B" "%%A"
) Else (
  Move /Y "%%A_%%B\*" "%%A\"
  RmDir "%%A_%%B"
)
PopD
  • 2
    Delims=- defines a hyphen-minus character as delimiter however your examples show an underscore. – JosefZ Mar 8 '20 at 19:54
  • +1 First thing I spotted also. – Michael Harvey Mar 8 '20 at 19:54
  • Okay thanks for pointing that out. So i fixed that issue but now I am noticing that it only combines folders with a "_' but I want it combine folders that don't have anything after it or has a space after it so all possible scenarios. For example if a folder is 0527549_National and a folder "0527549 - National" and a folder "0527549-National". Currently it is just combining folders that have the underscore but I want it to combine all folders that have the first 7 characters. – hiten Mar 8 '20 at 20:21
  • If you're on Windows 10 & have no working knowledge of either batch or PowerShell, you should ignore batch & learn PowerShell. It's really fun. – Keith Miller Mar 8 '20 at 22:02
  • Need some more info.... 1) If file sample.txt exists in both ..\1234567_Nat\sample.txt and ..\1234567_HIT\sample.txt can they be overwritten or do they need to be re-named somehow? 2) When you say "merged", do you mean copied or moved? – Durry42 Mar 9 '20 at 10:30
1

The script below should do what you are after. If any duplicates are found in source and destination, then the source file will be skipped and the file along with its folder structure will not be deleted.

In it's current form, when run, it will only write it's 'planned' actions to the screen and log file.....but will not actually move/change any files or folders.

To run it and allow it to make the moves/changes, remove the /L from the end of the robocopy.exe line.

Note: RoboCopy is included in Windows 7 & 10, so you do not need to download it.

@echo off
setlocal enabledelayedexpansion

:: Variables.

set "rootDir=C:\test\"
set "logName=RoboCopy.log"


:: Ensures %rootDir% value contains trailing slash.

if not [%rootDir:~1%] EQU [\] set "rootDir=%rootDir%\"


:: Jumps to the rootDir so that for loop will only display folder names and not the full path.

pushd %rootDir%


:: Loop through all folders located at %rootDir% and for each folder...
::    Use Robocopy.exe to MOVE(and delete original folder) contents of the folder to a new folder named with just first 7 characters of original folder, and delete the original folder.
::    Log the actions of RoboCopy to the screen and also a file called %logName% in the %rootDir% location.

for /d %%a in (*) do (
    set "currentDir=%%a"
    robocopy.exe "%rootDir%%%a" "%rootDir%!currentDir:~0,7!" /MOVE /E /R:3 /W:10 /V /LOG+:%rootDir%%logName% /FP /TEE /XC /XN /XO /L
)


popd
endlocal

echo.
echo The task has finished.
echo.
pause

:eof

The output log will look similar to this..

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------

  Started : Monday, 9 March 2020 10:50:39 PM
   Source : C:\test\1234567_test3\
     Dest : C:\test\1234567\

    Files : *.*

  Options : *.* /V /FP /TEE /S /E /DCOPY:DA /COPY:DAT /MOVE /XO /XN /XC /R:3 /W:10 

------------------------------------------------------------------------------

                       2    C:\test\1234567_test3\
    *EXTRA Dir        -1    C:\test\1234567\dir2\
    *EXTRA Dir        -1    C:\test\1234567\dir3\
      *EXTRA File              9    C:\test\1234567\test2.txt
             newer             9    C:\test\1234567_test3\test1.txt
        New File               9    C:\test\1234567_test3\test3.txt
  0%  
100%  
                       1    C:\test\1234567_test3\dir1\
      *EXTRA File             13    C:\test\1234567\dir1\test1dir1.txt
        New File              13    C:\test\1234567_test3\dir1\test3dir1.txt
  0%  
100%  
      New Dir          1    C:\test\1234567_test3\dir4\
        New File              13    C:\test\1234567_test3\dir4\test3dir4.txt
  0%  
100%  

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         3         1         2         0         0         2
   Files :         4         3         1         0         0         2
   Bytes :        44        35         9         0         0        22
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00


   Speed :               11666 Bytes/sec.
   Speed :               0.667 MegaBytes/min.
   Ended : Monday, 9 March 2020 10:50:39 PM

Example 1

C:\test\1234567_test1\file1.txt
C:\test\1234567 test2\file2.txt
C:\test\1234567890\file3.txt

Would become...

C:\test\1234567\file1.txt
C:\test\1234567\file2.txt
C:\test\1234567\file3.txt

Example 2

C:\test\1234567_test1\file1.txt
C:\test\1234567_test2\file2.txt
C:\test\1234\file3.txt

Would become...

C:\test\1234567\file1.txt
C:\test\1234567\file2.txt
C:\test\1234\file3.txt

Example 3

C:\test\1234567_test1\file1.txt
C:\test\1234567_test2\file2.txt
C:\test\1234567_test3\file1.txt

Would become...

C:\test\1234567\file1.txt
C:\test\1234567\file2.txt
C:\test\1234567_test3\file1.txt

Example 4

C:\test\1234567_test1\file1.txt
C:\test\1234567_test2\file2.txt
C:\test\1234567_test2\sub1\file1.txt
C:\test\1234567_test3\sub1\file1.txt
C:\test\1234567_test3\sub1\file2.txt

Would become...

C:\test\1234567\file1.txt
C:\test\1234567\file2.txt
C:\test\1234567\sub1\file1.txt
C:\test\1234567_test2\sub1\file1.txt
C:\test\1234567\sub1\file2.txt
  • Thank you! I tried running this by changing the path to set "rootDir=C:\Users\sa27964\Desktop\Testing folder" and also removing the /L from the end of the robocopy.exe line but nothing happens... – hiten Mar 10 '20 at 21:34
  • Try right click it? and choose Run As Admin – Durry42 Mar 10 '20 at 22:19

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.