Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

batch file - EnableDelayedExpansion not working as expected with ECHO

Forgive me if this has been asked before but my searches have come up with nothing similar....

I have a batch file that uses a for loop to go through a comma delimited file that is formatted like this...

Pathname,client

The batch is intended to pass the path to a logparser command which in turn sums a column of data. I am trying to get my results sent to a single file in which the client and results are listed on the same line like this...

client, sum
client, sum

However when I attempt to do so with echos and redirects (>>) the values end up on seperate lines like this...

client,
sum
client,
sum

In my search I noted that using the "SETLOCAL ENABLEDELAYEDEXPANSION" should work but I have not had any luck. SS64 I have tried variations of variables with no luck. The command is ran from a batch file, not command line.

SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
for /f "tokens=1,2 skip=1 delims=," %%f in (faxreportnames.csv) do (
echo %%g,>>parse_page_counts.txt
logparser -i:csv "select sum(NUMPAGES) from %%f where FAX_STATUS = 'SUCCESSFUL'" -q:on -rtp:-1 -headers:off>>parse_page_counts.txt
)

I have a feeling it is something silly simple but after several hours of trying, I think I am missing it...

Edit for clarity:

  • The issue is more where a CRLF is being added when using the echo statement. My searches said using the "EnableDelayedExpansion" should resolve the issue... I am aware using echo causes the CRLF to be appended.
  • The values in the .csv file being read are strictly text. First column is the path and path name of a .csv to be parsed. Second column is the client name.
  • The values returned by %%f and %%g are correct.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
for /f "tokens=1,2 skip=1 delims=," %%f in (faxreportnames.csv) do (
 logparser -i:csv "select sum(NUMPAGES) from %%f where FAX_STATUS = 'SUCCESSFUL'" -q:on -rtp:-1 -headers:off>"tempfilename.txt"
 for /f "usebackq delims=" %%c in ("tempfilename.txt") do echo %%g,%%c>>"parse_page_counts.txt"
)

(UNTESTED as I don't have logparser)

Uses and leaves tempfile.txt - a filename of your own choosing.

I've used quotes around tempfile.txt to avoid the spaces-in-names issue if the chosen tempfile.txt is in the typical %temp% directory. The usebackq is required if the filename is quoted but should be omitted if the temporary filename is not quoted.

I'm sure this could be done more cleanly, but without logparser, I'm not able to test.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...