Solved

help with cmd/bat file

Posted on 2012-12-21
5
471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 70

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 70

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Starting up a Project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

688 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