Copy or Append

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
VespaGS150
Posts: 8
Joined: 13 Jul 2016 09:32

Copy or Append

#1 Post by VespaGS150 » 13 Jul 2016 09:41

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

skaa
Posts: 4
Joined: 25 Jun 2010 09:11
Location: USA
Contact:

Re: Copy or Append

#2 Post by skaa » 13 Jul 2016 12:33

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Copy or Append

#3 Post by Squashman » 13 Jul 2016 12:58

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Copy or Append

#4 Post by Squashman » 13 Jul 2016 13:22

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy or Append

#5 Post by foxidrive » 14 Jul 2016 08:41

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"

VespaGS150
Posts: 8
Joined: 13 Jul 2016 09:32

Re: Copy or Append

#6 Post by VespaGS150 » 15 Jul 2016 03:18

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy or Append

#7 Post by foxidrive » 16 Jul 2016 04:30

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.

VespaGS150
Posts: 8
Joined: 13 Jul 2016 09:32

Re: Copy or Append

#8 Post by VespaGS150 » 18 Jul 2016 06:32

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
Last edited by foxidrive on 18 Jul 2016 11:28, edited 1 time in total.
Reason: spelling

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy or Append

#9 Post by foxidrive » 18 Jul 2016 11:27

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

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Copy or Append

#10 Post by douglas.swehla » 18 Jul 2016 18:36

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy or Append

#11 Post by foxidrive » 18 Jul 2016 18:53

Thanks Douglas.

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

VespaGS150
Posts: 8
Joined: 13 Jul 2016 09:32

Re: Copy or Append

#12 Post by VespaGS150 » 19 Jul 2016 01:23

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy or Append

#13 Post by foxidrive » 19 Jul 2016 07:18

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.

VespaGS150
Posts: 8
Joined: 13 Jul 2016 09:32

Re: Copy or Append

#14 Post by VespaGS150 » 19 Jul 2016 07:28

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Copy or Append

#15 Post by Squashman » 19 Jul 2016 07:33

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]]

Post Reply