Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

help with cmd/bat file

Hello.

I have a directory with some text files I like to merge

that can be done by copy  c:\myfolder\*.txt  destfile.txt

But what I need to do is to have the files in a specific order (ie only a few of them is important)

So what I want to do is having  a file  fileorder.txt (not in the same folder)
that contains the files that should be first in that order

so the cmd file shoud read the  fileorder.txt

and merge all the files that are in there into destfile.txt
then all the other files that are not in fileorder.txt should be appended after
those files.

How can i do that?
0
pucko73
Asked:
pucko73
  • 3
  • 2
1 Solution
 
QlemoC++ DeveloperCommented:
@echo off
cd /d C:\Targetfolder
copy nul DestFile.txt >nul
for /F "tokens=*" %%F in (c:\FileOrder.txt) do copy DestFile.txt+"C:\MyFolder\%%~F" >nul
for /F "tokens=*" %%F in ('dir C:\MyFolder\*.txt /b ^| findstr /V /G:C:\FileOrder.txt') do ^
copy DestFile.txt+"%%~F" >nul

Open in new window

0
 
pucko73Author Commented:
Seems like it dont work for me.

I have this:

c:\fileorder.txt containing

b.txt
c.txt

in c:\myfolder i have   4 files.  a.txt  b.txt  c.txt d.txt

The DestFile.txt ends up with som strange charaters and only the content of b.txt joined with c.txt.
0
 
pucko73Author Commented:
IE the file contains :


Row 1 :  content of b.txt
Row 2.  Character for ascii value 26   and the content of c.txt
Row 3.  3 times the chaqracter for ascii value 26
0
 
QlemoC++ DeveloperCommented:
ASCII 26 is EOF, but that is no longer required or used. It has been appended by the COPY command, but in an inconsistent way ...
I have changed the COPY command to use binary mode (and not add anything), and invented variables to set the different names and locations.
@echo off
set fileOrder=C:\FileOrder.txt
set source=C:\MyFolder
set target=C:\Targetfolder

cd /d %target%
copy nul DestFile.txt >nul
for /F "tokens=*" %%F in (%fileOrder%) do copy /B DestFile.txt+"%Source%\%%~F"
for /F "tokens=*" %%F in ('dir %Source%\*.txt /b ^| findstr /V /G:%fileOrder%') do ^
copy /B DestFile.txt+"%Source%\%%~F" 

Open in new window

0
 
pucko73Author Commented:
works like a charm.

Thanks. !
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now