Solved

help with cmd/bat file

Posted on 2012-12-21
5
467 Views
Last Modified: 2012-12-21
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
Comment
Question by:pucko73
  • 3
  • 2
5 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 38713154
@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
 

Author Comment

by:pucko73
ID: 38714323
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
 

Author Comment

by:pucko73
ID: 38714345
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
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 38714735
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
 

Author Closing Comment

by:pucko73
ID: 38714745
works like a charm.

Thanks. !
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question