Solved

help with cmd/bat file

Posted on 2012-12-21
5
461 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
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
works like a charm.

Thanks. !
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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.
A short article about problems I had with the new location API and permissions in Marshmallow
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

744 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

16 Experts available now in Live!

Get 1:1 Help Now