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 CR\LF 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.
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.
Related
From a dir command that displays a number of sub-folders within a main folder, I am trying to extract the total number of files only. I am trying to use the FIND command with the string "File(s)", however this lists all of the file totals for each of the sub-folders, in addition to the overall total on the last line. I would like to limit my extraction to only this overall total, and none of the sub-folder file counts.
Whilst I've provided a simple one liner already in the comment section, it does not technically provide a method of doing what you wanted. The task you've explained is really to retrieve a substring of the last but one line of output from the DIR command with its /S option. This method prevents the need to use the FIND command to search for the language dependent string File(s) too.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "MainFolder=C:\Users\CoastalGuy\Documents"
Set "CommandLine=Dir "%MainFolder%" /S /A:-D"
Set "Last="
Set "LastButOne="
For /F Delims^=^ EOL^= %%G In (
'%CommandLine% 2^>NUL'
) Do (
If Defined Last (
SetLocal EnableDelayedExpansion
For /F "Tokens=1 Delims= " %%H In ("!Last!") Do (
EndLocal
Set "LastButOne=%%H"
)
)
Set "Last=%%G"
)
If Defined LastButOne (
Echo %LastButOne%
Pause
)
For the specific task you've posted, this script is a little over the top, but I've tried to create it more like a template, so that you could more easily adapt it for similar tasks, (by changing the variable values on lines 4 and 5, and modifying the Tokens and Delims values in the inner For /F loop as required).
Found this simple result on an internet search:
After doing a dir /s of the main folder and then exporting a file (Files.txt) of each of the file results using FIND "File(s)", the following command extracts just the last line of that file, which has the grand total of Files from all sub-folders.....
For /F "UseBackQ Delims==" %%A In ("Files.txt") Do Set "lastline=%%A"
Echo - %lastline%
Works Great,
Thanks,
I have the following in a batch file
for /f "delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set %%a
net use J: https://csv/dav %p% /user:%u% /persistent:yes
I get an error:
Environment variable C:\Program Files (x86)\Wowza\creds.txt not defined
What do I need to resolve this?
Secondly, it works for all colleagues apart from one. Same laptop make, model and build. I used my details and it failed on his but worked on mine.
What fails is that it asks for the credentials to map the drive instead of taking them from the file
creds.txt
u:JoeBloggs
p:Password1234
Any idea?
Thanks
the reason for your errormessage is, your for /f loop doesn't evaluate the contents of the file. It takes a quoted string as string not as filename. Usebackq changes that behaviour.
You have another failure in your script: With your code, set %%a translates to set u:JoeBloggs, which is invalid syntax. Correct syntax requrires set u=Joebloggs. Therefore you have to split the line in a part before the colon and a part after the colon and build your set command accordingly (just set %%a would work, when the contents of the file would look like u=JoeBloggs)
Change your for loop to:
for /f "usebackq tokens=1,* delims=:" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%a=%%b"
I was going to post a similar answer to #stephan but he beat me to it. If however you have the option to change your creds.txt file to the below:
u=JoeBloggs
p=Password1234
You can shorten the for loop a bit to this:
for /f "usebackq delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%~a"
which effectively just does this:
set "u=JoeBloggs"
set "p=Password1234"
This is my first time working with batch files, so sorry if this is something simple I just missed.
I have a batch file, to which I need to pass a file path/name. This path has spaces. So as far as I know, to pass it in I need quotes around it. Once I get it in the batch file then I need to run it through a for loop, but it always gives me an error that is splitting the file path at the spaces. I am using the "delims=" in the loop. Here are some of the things I've tried, none of which have worked:
just passing in the parameter into the loop (echo %1 comes out with the file path in quotes)
for /f "usebackq skip=4 delims=" %%a in (%1) do ( stuff )
causes: 'Suite' is not recognized as an internal or external command (Suite is the word after the first space)
passing the parameter surrounded by quotes (echo "%1" produces file path surrounded by two quotes)
for /f "usebackq skip=4 delims=" %%a in ("%1") do ( stuff )
causes: The system cannot find the file "G:\Programming\App" (stuff before 1st space)
getting rid of the quotes and passing in variable in quotes (echo "%fn%" produces file path in quotes again)
set fn=%1
set fn=%fn:"=%
for /f "usebackq skip=4 delims=" %%a in ("%fn%") do ( stuff )
causes: 'Suite' is not recognized as an internal or external command
getting rid of the quotes and passing in the variable without quotes (echo %fn% produces full file path with no quotes)
set fn=%1
set fn=%fn:"=%
for /f "usebackq skip=4 delims=" %%a in (%fn%) do ( stuff )
causes: The system cannot find the file Suite.
The stuff portion works if I just type in a file path in quotes so I don't think that part is causing problems. I think the problems are occurring before it gets there.
What am I doing wrong?
EDIT
The batch file is being called by another batch file with a path embedded that loops through the files in that path:
#echo off
setlocal enableextensions enabledelayedexpansion
for %%a in ("G:\Programming\App Suite 2.0\IT Regional Files\Phone Logs\*.log") do (
call ..\BatchFile\split.bat "%%~fa"
)
endlocal
Also did try with "%~1" and it produced the same results as %1.
I have two .txt files. One contains numbers, and the other one contains filepaths. I want to combine these two files to a .csv. The combination is based on wether the number (from nrs.txt) is in the string of the filepath (nodups.txt).
Now I have the following code for this:
#setlocal enableextensions enabledelayedexpansion
for /F %a IN (Output\nrs.txt) DO (
SET "nrs=%a"
for /F %b IN (Output\nodups.txt) DO (
SET "pathstring=%b"
SET csvdelim=,
IF NOT x!pathstring:%nrs%=""!==x%pathstring% %nrs%,%pathstring%>>new2017.txt
)
)
#endlocal
However, I keep having the following issues with the code:
The pathstring never seems to get set. (when I run the code without the if statement, The nrs variable gets set but the pathstring is set to %b). I've seen a lot of possible solutions on here already but none seem to work for me (setting variables like !var! and using usebackq).
The IF statement in the second for loop gets the following error message =""!==x%pathstring% was unexpected at this time. The ="" should remove the nr. from the path (if its there). When I replace "" with something else it still does not work.
The file contents are:
File nrs.txt:
12345
12245
16532
nodubs.txt:
C:\tmp\PDF_16532_20170405.pdf
C:\tmp\PDF_1234AB_20170405.pdf
C:\tmp\PDF_12345_20170506.pdf
Desired output:
12345, C:\tmp\PDF_12345_20170506.pdf
16532, C:\tmp\PDF_16532_20170405.pdf
I really hope someone can help me out with this !
This solution use a different approach, based on arrays:
#echo off
setlocal EnableDelayedExpansion
rem Load array from nodubs.txt file
pushd "Output"
for /F "tokens=1,2* delims=_" %%a in (nodubs.txt) do set "nodubs[%%b]=%%a_%%b_%%c"
rem Process nrs.txt file and show output
(for /F %%a in (nrs.txt) do (
if defined nodubs[%%a] echo %%a, !nodubs[%%a]!
)) > new2017.txt
In a batch file for variables need two percent signs.
There is no need to put %%A into a variable, use it directly.
#setlocal enableextensions enabledelayedexpansion
for /F %%a IN (Output\nrs.txt) DO (
findstr /i "_%%a_" Output\nodups.txt >NUL 2>&1 || >>new2017.txt Echo %%a
)
#endlocal
Instead of a second for, I'd use findstr to search for the entry of nrs.txt enclosed in underscores.
if no find use condiotonal execution on failure || to write to the new file.
According to changed preliminaries another answer.
#Echo on
Pushd Output
for /F "tokens=1-3 delims=_" %%A IN (
' findstr /G:nrs.txt nodubs.txt'
) DO >>"..\new2017.txt" Echo %%B, %%A_%%B_%%C
Popd
sample output:
> type ..\new2017.txt
16532, C:\tmp\PDF_16532_20170405.pdf
12345, C:\tmp\PDF_12345_20170506.pdf
I have a log file containing a stack trace split over a number of lines. I need to read this file into a batch file and remove all of the lines breaks.
As a first step, I tried this:
if exist "%log_dir%\Log.log" (
for /F "tokens=*" %%a in ("%log_dir%\Log.log") do #echo %%a
)
My expectation was that this would echo out each line of the log file. I was then planning to concatenate these lines together and set that value in a variable.
However, this code doesn't do what I would expect. I have tried changing the value of the options for delims and tokens, but the only output I can get is the absolute path to the log file and nothing from the contents of this file.
How can I set a variable to be equal to the lines of text in a file with the line breaks removed?
If you want to use quotes for your filename in the FOR/F loop you need to add the usebackq option too, else you get a string not the content of your file.
for /F "usebackq delims=" %%a in ("%log_dir%\Log.log") do #echo %%a
Or remove the quotes
if exist "%log_dir%\Log.log" (
for /F "tokens=*" %%a in (%log_dir%\Log.log) do #echo %%a
)