Windows Batch file to move a set "N" number of files from a single source folder to a destination folder. Then merged multiple files together as one file.

Request for a window batch file that can be run from command/CMD prompt.  Windows 7 environment.

Windows Batch file to move a set "N" number of files from a single source folder to a destination folder.  The single source directory may contain 1 up to a million files.  Need to copy 200 files at a time to a destination folder_N+ that should contain <= 200 files per destination directory.

Once all the files are moved to the destination folders, would like to merge the multiple files per individual destination folder into one single file. (copy *.hl7 newfile.hl7)

File extension: *.hl7
Source directory - C:\temp\source
Destination directory - C:\temp\outputN+

For the purpose of demonstration I have attached (5) sample hl7 files.  
These (5) files would be all be in the (C:\temp\source) directory.  
Expected output would result in the following:

 dir: new 1.hl7, new 2.hl7
 copy *.hl7 newfile.hl7

 dir: new 3.hl7, new 4.hl7
 copy *.hl7 newfile.hl7

 dir: new 5.hl7
 copy *.hl7 newfile.hl7

Note: The folder names may contain spaces, also the file names may contain spaces.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
Have a go with this... have it set to make a dir for each of 4 files at the moment but amend int he variables at the top:

@echo off
Setlocal enabledelayedexpansion

REM Amend these variables for controlling the actions of the batch file
REM Steve Knight - - Oct 2018

SET Source=c:\temp\source
SET Dest=c:\temp\output
SET FileType="HL7"
SET Split=4
SET DirNum=

MD "%Dest%" 2>NUL

SET count=0
FOR %%f IN ("%source%\*.%fileType%") do (
  IF !count! GEQ %split% (
    ECHO !Split! files have been written to "%dest%!DirNum!" - moving to next directory
    REM Combine files to newfile as this directory is full
    COPY "%dest%!DirNum!\*.%FileType%" "%dest%!DirNum!\NewFile.%FileType%" >NUL

    REM Move onto next dir number and reset counter
    IF "!DirNum!"=="" SET DirNum=0
    SET /A Dirnum=DirNum+1
    MD "%Dest%!DirNum!" 2>NUL
    SET count=0
  MOVE "%%~f" "%dest%!DirNum!" >NUL
  TITLE Moving file !Count! in directory !DirNum!
  SET /A count=count+1 

REM Deal with any files left over if less than a full directory
IF %count% LSS %Split% (
    ECHO !count! files have been written to last directory "%dest%!DirNum!"
    REM Combine files to newfile as this directory is full
    COPY "%dest%!DirNum!\*.%FileType%" "%dest%!DirNum!\NewFile.%FileType%" >NUL

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mreid3847Author Commented:
Thanks Steve, I will give it a spin. I may have so thick headed questions, but thank you for putting in comments/notes.
mreid3847Author Commented:
Steve, this indeed did work.  It produced the expected and asked for output.  Thank you.  And as I was testing it out the vendor came back with an edit/modification request.  It is along the same lines, I tried to edit your code and failed as a newbie.  I am posting a 2nd request so full points can be contributed for a change in scope, if you don't mind reviewing .

But for this question you were spot on with the code, it produced the expected output.  Thank you.
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

mreid3847Author Commented:
Great work, Steve thanks for the review, assistance and turn around.
Steve KnightIT ConsultancyCommented:
No problem, and looks like this question tipped me over into a "genius" rating

"You've earned Genius rank in Windows Batch.
Windows Batch points: 1,001,156
1,498,844 more points to become an Ace in Windows Batch!"

After taking 16 years on EE to get that I don't think I'll bother aiming for another 1.5 million to get another t-shirt...
Steve KnightIT ConsultancyCommented:
I can't see another question from you yet, post a link here if you want and will take a look when I see it.

mreid3847Author Commented:
2nd Question posted
"Windows Batch file to count a set "N" number of files from a single source folder,merged multiple files together into a single output file"
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.