Copy or Append
Moderator: DosItHelp
-
- Posts: 8
- Joined: 13 Jul 2016 09:32
Copy or Append
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
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
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
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
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.
Then you will end up with output like this.
Code: Select all
FileB.txtFileA.txt
Re: Copy or Append
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"
-
- Posts: 8
- Joined: 13 Jul 2016 09:32
Re: Copy or Append
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
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
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.
-
- Posts: 8
- Joined: 13 Jul 2016 09:32
Re: Copy or Append
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
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
Reason: spelling
Re: Copy or Append
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
Is this one unexpected for you in some way? Require_resulting_file.bak
-
- Posts: 75
- Joined: 01 Jun 2016 09:25
Re: Copy or Append
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
Thanks Douglas.
VespaGS150, can you put the wrong file in a zip archive too please?
VespaGS150, can you put the wrong file in a zip archive too please?
-
- Posts: 8
- Joined: 13 Jul 2016 09:32
Re: Copy or Append
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
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.
-
- Posts: 8
- Joined: 13 Jul 2016 09:32
Re: Copy or Append
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
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]]