Saturday, September 19, 2015

windows - Does appending (merging) files together work with XCOPY?



I want to merge multiple (text) files having the same prefix into a single file. This task is part of a whole batch (in VBScript) but due to performance expectations, I'm more likely to use a native command rather than programming a loop (opening each file, reading the whole content, then writing it down to the destination file) in VBScript.



Currently facing a problem with COPY which seems to require you to flush the StdOut buffer (because it outputs all files' names it copies) I was looking towards XCOPY which has a lot more flags to configure its behavior and output (like /Q which Suppresses the display of xcopy messages).



Reading the doc:
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb491035(v=technet.10)




I came across this:




Appending files



To append files, specify a single file for destination, but multiple files for source (that is, by using wildcards or file1+file2+file3 format).




OK so I write: xcopy "My file pattern whatsoever.*.tmp" "My destination file.tmp"




Unfortunately it asks if My destination file.tmp is a file or a directory, to which I should answer with either F (for file) or D (for Directory). Note: it's language dependent (in my case - French - it's F or R).



I noticed this in the above doc:




Specifying whether Destination is a file or directory



If Destination does not contain an existing directory and does not end with a backslash (\), the following message appears:




Does destination specify a file name 
or directory name on the target
(F = file, D = directory)?


Press F if you want the file or files to be copied to a file. Press D if you want the file or files to be copied to a directory.



You can suppress this message by using the /i command-line option, which causes xcopy to assume that the destination is a directory if the source is more than one file or a directory.





Well, as this /i flag does not fit my need, I resort to a trick I read here & there, like (amongst answers): https://stackoverflow.com/a/45134466



So I try this: echo F|xcopy "My file pattern whatsoever.*.tmp" "My destination file.tmp"



OK now, the first file is copied to the destination file, but for every following file, a question pops up about replacing the destination file (?!!).
By the way all are discarded because the echo command does not provided any input for them.



If I use this flag:





/y : Suppresses prompting to confirm that you want to overwrite an existing destination file.




then I end up with a destination file containing only the content of the last file I want to merge (i.e. the last file matching the source pattern)!!!



I also tried checking if it works with only two files, e.g.: xcopy "My file pattern whatsoever.0001.tmp"+"My file pattern whatsoever.0002.tmp" "My destination file.tmp" but I get an error:File not found - My file pattern whatsoever.0001.tmp+My file pattern whatsoever.0002.tmp`



Same if I add spaces around the +; says Invalid number of parameters, probably because the file1+file2+file3 syntax doesn't work with long (including spaces) filenames, as suggested here: https://stackoverflow.com/questions/30651776/xcopy-returns-error-invalid-number-of-parameters-when-exclude-parameter-is-set



So what is wrong with what am I doing (or trying to)?



Answer



According to some of the comments on this Stack Overflow question, xcopy doesn't do the appending that its documentation says it does.



So you can use something like this instead.



type "My file pattern whatsoever.*.tmp" > "My destination file.tmp"


But type also outputs the filenames as it processes them, so you might run into the same problem that you are having with copy. To avoid that, you can redirect that unwanted output to NUL, like this:




type "My file pattern whatsoever.*.tmp" > "My destination file.tmp" 2>NUL


(Using 2>NUL, because type displays the filenames on StdErr.)



And if you are doing that anyway, you can go back to using copy, like this:



copy "My file pattern whatsoever.*.tmp" "My destination file.tmp" >NUL



(Using >NUL, because copy displays the filenames on StdOut.)


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...