Batch file new line problems

2019-08-09 20:31发布

问题:

I'm tearing my hair out now.

I have a batch file that searches through sub directories looking for jpg files and then parses some simple HTML code using the file names it finds and pushes this to a text file.

It is vitally important that each line of every file created is separated by a blank line.

This is why I have included the "Echo." lines. See batch file code below:

pushd \\SRVAPP1\Image Archive
Echo %CD%
PAUSE
for /R %CD% %%F in (*.jpg) do (
Echo %%~nF
Echo.
Echo ^<html^>^<img src='^.%%~pnxF' ^>^<a href='^.%%~pnxF'^>Download file^<^/a^>^<^/html^>
Echo.
Echo ^[^[Open file^|file^:^.%%~pnxF^]^]
Echo.
Echo ^[^[Show in folder^|file^:^.%%~pF^]^]
) > ".\Documentation\%%~nF.tid"
popd
PAUSE

The loop iterates through the first sub directory fine and creates a perfect output. example below

YORYM_TA1

<html><img src='.\2014-03-13\JPG\YORYM_TA1.jpg' ><a href='.\2014-03-13\JPG\YORYM_TA1.jpg'>Download file</a></html>

[[Open file|file:.\2014-03-13\JPG\YORYM_TA1.jpg]]

[[Show in folder|file:.\2014-03-13\JPG\]]

However, when the batch file moves onto the second directory - and all subsequent directories "Echo." seems to be replaced by "Echo.." and you get the following output (complete with full stops instead of blank lines):

YORYM_H2200_8
.
<html><img src='.\2014-05-22\JPG\YORYM_H2200_8.jpg' ><a href='.\2014-05-22\JPG\YORYM_H2200_8.jpg'>Download file</a></html>
.
[[Open file|file:.\2014-05-22\JPG\YORYM_H2200_8.jpg]]
.
[[Show in folder|file:.\2014-05-22\JPG\]]

Any ideas why this may be occurring? I am running windows 7.

回答1:

Using echo. can be problematic, as it searches each time on the disk for a file named echo without an extension. If there is such a file echo. will simply fail.

echo( is faster and safer.

For all the other combinations off echo you could read
Dostips: ECHO. FAILS to give text or blank line - Instead use ECHO/



回答2:

I've got a solution, but no explanation:

use echo( instead of echo.