Solved

help with cmd/bat file

Posted on 2012-12-21
5
463 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 68

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 68

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

864 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now