Page 1 of 2

Copy or Append

Posted: 13 Jul 2016 09:41
by VespaGS150
Hi all,

I would create a batch file that copy a file to another directory but if the destination file still exists, the batch must append text to the existing destination file.
In other words:

Source: C:\Folder1\FileA.txt ; this file will change every day
Destination: C:\Folder2\FileB.txt

The FileB.txt will be moved by another program/software to another folder but, what I would to do, is that if this software fails to move the FileB.txt, the FileA.txt must be append to FileB.txt with a carriage return between the two appended texts-

Many thanks in advance!
DF

Re: Copy or Append

Posted: 13 Jul 2016 12:33
by skaa

Code: Select all

set snfFrom=c:\Folder1\File1.txe
set snfTo=c:\Folder2\File2.txe

if exist %snfTo% goto mExist
copy %snfFrom% %snfTo%
goto mEx
:mExist
copy %snfTo%+%snfFrom% %snfTo%
:mEx

Re: Copy or Append

Posted: 13 Jul 2016 12:58
by Squashman
skaa wrote:

Code: Select all

set snfFrom=c:\Folder1\File1.txe
set snfTo=c:\Folder2\File2.txe

if exist %snfTo% goto mExist
copy %snfFrom% %snfTo%
goto mEx
:mExist
copy %snfTo%+%snfFrom% %snfTo%
:mEx


VespaGS150 wrote:FileA.txt must be append to FileB.txt with a carriage return between the two appended texts


Your code will also append an EOF character to the end of the file.

Re: Copy or Append

Posted: 13 Jul 2016 13:22
by Squashman
The other problem this could run into is if the last line of the files do not have a CRLF.

Then you will end up with output like this.

Code: Select all

FileB.txtFileA.txt

Re: Copy or Append

Posted: 14 Jul 2016 08:41
by foxidrive
This will put the data from one file into a new file or append it if the file already exists.

Code: Select all

type "C:\Folder1\FileA.txt" >> "C:\Folder2\FileB.txt"

Re: Copy or Append

Posted: 15 Jul 2016 03:18
by VespaGS150
Thanks guys, it works well but when it appends multiples files, I have this situation:

04203119160715090500
->02203115160418065800
02203113160418065900

where the -> is the CR/LF character.

It is possible to append the two files without this chatacter?

Example.

File 1:
04203119160715090500

File 2:
02203115160418065800
02203113160418065900


Append result

04203119160715090500
02203115160418065800
02203113160418065900

Thanks
Best
GS150

Re: Copy or Append

Posted: 16 Jul 2016 04:30
by foxidrive
VespaGS150 wrote:where the -> is the CR/LF character.

It is possible to append the two files without this chatacter?


type does exactly what you want.

The text that is inside the file affects how it appears later, and copy adds a EOF character as Squashman noted, unless you use the /B switch.

We have no idea about the makeup of your files and so any advice would be based upon guessing.

Re: Copy or Append

Posted: 18 Jul 2016 06:32
by VespaGS150
Here you are the 3 files:

FILEA+FILEB and the result of the append like expected

https://we.tl/naNq2EVB3g

As you can see, the resulting file doesn't have any visible CR/LF charcater
With the script above, I have a visible CR/LF character (also if I open the file with notepad)

Thanks a lot!
GS

Re: Copy or Append

Posted: 18 Jul 2016 11:27
by foxidrive
Can you explain what the problem is with the files in that ZIP archive?

Is this one unexpected for you in some way? Require_resulting_file.bak

Re: Copy or Append

Posted: 18 Jul 2016 18:36
by douglas.swehla
VespaGS150 wrote:Thanks guys, it works well but when it appends multiples files, I have this situation:

04203119160715090500
->02203115160418065800
02203113160418065900

where the -> is the CR/LF character.




foxidrive wrote:Can you explain what the problem is with the files in that ZIP archive?


I don't think there is a problem with those files; he's saying that they show what he wants and isn't getting. In the example above, the complaint is that a "visible CR/LF character" is present at the start of the second line. I've never heard of a text editor displaying CR/LF at the front of a line, even with non-printing characters displayed, so he may actually be seeing a TAB character. Notepad++ displays tab as an arrow graphic when using ANSI encoding, so maybe he's getting an unexpected TAB at the end of each file, and it offsets the text of the file appended.

Re: Copy or Append

Posted: 18 Jul 2016 18:53
by foxidrive
Thanks Douglas.

VespaGS150, can you put the wrong file in a zip archive too please?

Re: Copy or Append

Posted: 19 Jul 2016 01:23
by VespaGS150
foxidrive wrote:Thanks Douglas.

VespaGS150, can you put the wrong file in a zip archive too please?



Thanks folks!

Here you are the wrong file and also the screenshot of the same wrong file opened with Notepad and Notepad++

https://we.tl/yYwWelbOb2

Re: Copy or Append

Posted: 19 Jul 2016 07:18
by foxidrive
VespaGS150 wrote:Here you are the wrong file and also the screenshot of the same wrong file opened with Notepad and Notepad++


You have EOF markers in there that you are concerned about. A hex viewer will show you the ascii number of each character and in hexadecimal the EOF character is 1A

The flaw in your question is that you didn't show which code you are using.

The copy command using the /B switch will not add the EOF but it's not clear at which stage they are being added, because you have two of them and a file normally only has one. The copy command would remove the top one and only leave the bottom one when the /b switch is not used.

Re: Copy or Append

Posted: 19 Jul 2016 07:28
by VespaGS150
foxidrive wrote:
VespaGS150 wrote:Here you are the wrong file and also the screenshot of the same wrong file opened with Notepad and Notepad++


You have EOF markers in there that you are concerned about. A hex viewer will show you the ascii number of each character and in hexadecimal the EOF character is 1A

The flaw in your question is that you didn't show which code you are using.

The copy command using the /B switch will not add the EOF but it's not clear at which stage they are being added, because you have two of them and a file normally only has one. The copy command would remove the top one and only leave the bottom one when the /b switch is not used.


Thanks!

Here you are my code. Where I can put the /B switch?



set snfFrom=C:\FolderA\File.bak
set snfTo=C:\FolderB\File.bak

if exist %snfTo% goto mExist
copy %snfFrom% %snfTo%
goto mEx
:mExist
copy %snfTo%+%snfFrom% %snfTo%
:mEx

Re: Copy or Append

Posted: 19 Jul 2016 07:33
by Squashman
VespaGS150 wrote:Here you are my code. Where I can put the /B switch?



set snfFrom=C:\FolderA\File.bak
set snfTo=C:\FolderB\File.bak

if exist %snfTo% goto mExist
copy %snfFrom% %snfTo%
goto mEx
:mExist
copy %snfTo%+%snfFrom% %snfTo%
:mEx


If you happened to read the help for the COPY command you will notice that the /B option can be used for SOURCE and DESTINATION.

Code: Select all

H:\>copy /?
Copies one or more files to another location.

COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]
     [+ source [/A | /B] [+ ...]] [destination [/A | /B]]