Making Batch for /f code work propely - batch-file

Here is my file that is trying to read hosts file. For some reason, it's not workin properly. Try the code yourself to see the error messages.
#echo off
cd %windir%/System32/drivers/etc
for /f "eol=# tokens=* delims=," %%a in (%windir%/System32/drivers/etc/hosts) do type %%a
pause>nul

type expects a file name. you probably meant echo.
as another issue, the hosts file is not comma, but whitespace separated. so you'll need to change your delims to include space and tab, assuming you want to have only the hostname.

Related

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 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.

Batch File: "Environment Variable Not Defined" on a path

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"

batch file to get file name up to specified character

I have filenames in a directory in the format of Mumbai Short Call Agentwise-MUMBAI SHORT CALL-3-01092016. I would like to strip off everything after the second hyphen and keep the first portion of the filename.
Is there a good website that could direct me in how to accomplish this? Or, maybe one of you dos batch experts can lead me in how to do this?
for /f "tokens=1,2,* delims=-" %%a in ('dir /b *-*-*') do #ECHO ren "%%a-%%b-%%c" "%%a-%%b%%~xc"
for every file with the given mask *-*.*: get first (%%a) and second part (%%b) plus extension of the rest (rest: %%c; Extension of the rest:%%~xc)
Notes:
- if you shorten filenames, be aware of possible duplicates!
#ECHO just lists the rename commands. Remove #ECHO, if the output satisfies you
See for /? or for /f for more information

Script skips reading text file

I have the path to a text file stored in a variable like so:
set readfile="%USERPROFILE%\Utilities\list_2.txt"
I use a for loop to read it like this:
FOR /F "tokens=1" %%i IN (%readfile%) DO
The problem is, it doesn't read the file, it skips the for loop altogether. The only way it can read the file is if I plug in the path directly, no variable in it's place. I've tried !readfile! as well, but it doesn't work, same result.
FOR /F "usebackqtokens=1" %%i IN (%readfile%) DO
or
FOR /F "tokens=1" %%i IN ('type %readfile%') DO
should fix your problem.
See for /?|more from the prompt for documentation.

This Supybot for windows batch install script needs to create another batch file

This Supybot for windows batch install script needs to create another batch file...
The Problem:
(1) I have a directory that has a file that ends with .conf
(2) There is only one file in this directory that ends with .conf
(3) But I don't know what this file starts with.. all I know is ????????.conf
(4) How do I set the filename.conf and remove the .conf part of the file name?
(5) As it is just the beginning of the filename that I need.
Example:
C:\runbot>find "supybot.ident: " | type *.conf > temp.txt
robotbot.conf
Outputs : robotbot.conf
The quest, is how do I set a variable=robotbot
=========================================================================
The Input was this file named "RootCode.conf" among many others
within the directory searched:
RootCode.conf
The Solution is:
FOR /F "tokens=1,2 delims=." %%a in ('FINDSTR /M "supybot.ident:" *.conf') DO SET USER=%%a&set dontneed=%%b
echo %USER%
pause
The Output is:
C:\runbot>FOR /F "tokens=1,2 delims=." %a in ('FINDSTR /M "supybot.ident:" *.con
f') DO SET USER=%a & set dontneed=%b
C:\runbot>SET USER=RootCode & set dontneed=conf
C:\runbot>echo RootCode
RootCode
C:\runbot>pause
Press any key to continue . . .
Winner... Special thanks Everyone
Your example of piping the output to typecommand is either wrong or useless. So I am assuming you mistyped and the real line was piping the other way around, and thus I am assuming that you are trying to find the filename of the file that contains the string "supybot.ident: ". In that case I would suggest to use findstr command instead.
FOR /F "tokens=*" %%a in ('FINDSTR /M "supybot.ident:" *.conf') DO SET USER=%%a
See HELP FINDSTR, HELP SET and HELP FOR for more information.
It's a bit unclear (to me, at least) what exactly you ask here. But if you need the output of a command, then use for /f:
for /f "delims=" %%x in ('some command ^| line') do set somevar=%%x
Note that you need to escape shell metacharacters in the command line (as they need to survive one parsing pass). Also note that you cannot set a variable to contain more than one line of text.

Resources