Recursive copy files from folders and rename (folder name)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Recursive copy files from folders and rename (folder nam

#16 Post by Compo » 07 Aug 2014 23:03

Thanks for the reply foxidrive and the test, (I am unable to try much of what I do due to lack of a Windows OS).

Is there any reason why the addition of an EOF would corrupt a PDF?
Do you know if it would be specific to the pdf reader software used for creation or viewing of the resulting output file.
Would changing the copy command to read COPY /A prevent the reported corruption.

Code: Select all

If Not Exist "%~1%_ext%" Copy /A "%~1\*%_ext%" "%~1%_ext%">Nul
or

Code: Select all

If Not Exist "%~1%_ext%" Copy "%~1\*%_ext%" /A "%~1%_ext%">Nul
or COPY /B

Code: Select all

If Not Exist "%~1%_ext%" Copy "%~1\*%_ext%" "%~1%_ext%" /B>Nul

Thanks for your input.

penpen
Expert
Posts: 1996
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Recursive copy files from folders and rename (folder nam

#17 Post by penpen » 08 Aug 2014 04:22

Any PDF file data ends with "%%EOF" (without the quotes).
Most pdf generators append a newline character, which not needed and ignored because it is located after the data.
The same happens to any other character that is appended at the end of the pdf file.
So appending an EOF marker is not the main problem here.

But if the file contains an EOF marker (0x1A) somewhere within the file, then the copy command finishes at this marker.
The rest of the file will not be copied.

In WinXp home this happens (for example) if you are using wildcards for the source, and a filename for the destination argument.
If the destination is a directory, then the file is completely copied.
But this behaviour may be OS specific.
More info: https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/copy.mspx?mfr=true.

So this happens in detail (using WinXP home):
Compo wrote:Would changing the copy command to read COPY /A prevent the reported corruption.
No this copy mode (that is selected automatically by copy) causes the corruption, so the following happens:

Code: Select all

If Not Exist "%~1%_ext%" Copy /A "%~1\*%_ext%" "%~1%_ext%">Nul
This "/A" indicates that all files are text files.
The destination file ends with the first EOF marker.

Code: Select all

If Not Exist "%~1%_ext%" Copy "%~1\*%_ext%" /A "%~1%_ext%">Nul
This "/A" indicates that the source file is a text file.
The destination file ends with the first EOF marker, too.

Code: Select all

If Not Exist "%~1%_ext%" Copy "%~1\*%_ext%" "%~1%_ext%" /B>Nul
This "\B" indicates that copy does not add an end of file character.
As the source file is automatically selected to be a text file, this results in a file that ends one byte earlier than the files above.

So this is what you wanted to to (treat all files as binary files):

Code: Select all

If Not Exist "%~1%_ext%" Copy /B "%~1\*%_ext%" "%~1%_ext%">Nul


penpen

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Recursive copy files from folders and rename (folder nam

#18 Post by Compo » 08 Aug 2014 09:41

penpen wrote:If Not Exist "%~1%_ext%" Copy /B "%~1\*%_ext%" "%~1%_ext%">Nul

I was aware that /b is the default value for copy unless combining files, so is the suggestion that /B is being ignored in this case because the interpreter thinks I'm combining files based on the use of the wildcard and not on the number of files matched, (which as we know is only one).
Now for the bit that's confusing me, possibly because of what foxi' said, am I supposed to be including any EOF character

Code: Select all

If Not Exist "%~1%_ext%" Copy "%~1\*%_ext%" /B "%~1%_ext%">Nul
...or not adding an EOF character

Code: Select all

If Not Exist "%~1%_ext%" Copy "%~1\*%_ext%" "%~1%_ext%" /B>Nul

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

Re: Recursive copy files from folders and rename (folder nam

#19 Post by foxidrive » 08 Aug 2014 10:05

My test was with a tiny file which didn't have embedded EOF characters but penpen's point seems likely and that the PDF would be truncated with an ascii copy.

penpen
Expert
Posts: 1996
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Recursive copy files from folders and rename (folder nam

#20 Post by penpen » 08 Aug 2014 14:17

Compo wrote:the interpreter thinks I'm combining files based on the use of the wildcard and not on the number of files matched, (which as we know is only one).
You are right, although this is not the only cause.
The destination is a single file: This combination causes that behaviour.

Compo wrote:Now for the bit that's confusing me, possibly because of what foxi' said, am I supposed to be including any EOF character

Code: Select all

If Not Exist "%~1%_ext%" Copy "%~1\*%_ext%" /B "%~1%_ext%">Nul
...or not adding an EOF character

Code: Select all

If Not Exist "%~1%_ext%" Copy "%~1\*%_ext%" "%~1%_ext%" /B>Nul

I'm not sure if i understand what you mean:
The lower code should still truncate the file (in this case the EOF character is truncated, too).
The upper code should be ok for most combinations.
But if the copy command detects that the destination is a device it may lead to an additional EOF character.
This may only happen, if you write to a device,... or at least copy detects it.
(Maybe the file/path contains "nul" or something like that).

In addition if there is more than only one source file, all destination files get corrupted.

penpen

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Recursive copy files from folders and rename (folder nam

#21 Post by Samir » 29 Aug 2014 07:06

why not use xcopy vs copy to avoid any issues with the eof? I rarely use copy unless I need a specific feature from it.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Recursive copy files from folders and rename (folder nam

#22 Post by Compo » 29 Aug 2014 13:44

Samir wrote:why not use xcopy vs copy to avoid any issues with the eof? I rarely use copy unless I need a specific feature from it.

I cannot see any reason why not!
grab2.bat

Code: Select all

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set _ext=.pdf
PushD %~dp0
For /D %%a In (*) Do If Exist "%%a\*%_ext%" Call :Sub "%%a"
Exit/B

:Sub
Set _cnt=0
For %%a In ("%~1\*%_ext%") Do Set/A _cnt+=1
If %_cnt% NEq 1 GoTo :EOF
If Not Exist "%~1%_ext%" Echo(F|XCopy "%~1\*%_ext%" "%~dp0%~1%_ext%">Nul

BinaryBen
Posts: 7
Joined: 04 Aug 2014 10:48

Re: Recursive copy files from folders and rename (folder nam

#23 Post by BinaryBen » 30 Sep 2014 13:45

I was wondering - is it a big change if I want to keep each sub folders name? So if I have a folder in a folder with a pdf - is it a big change to copy the pdf back to the top but concatenate both the folder names?

Thanx

Ben

Post Reply