Solved

batch file sytax question

Posted on 2013-11-19
5
637 Views
Last Modified: 2013-11-19
I'm trying to get a string returned into a variable in WIndows batch by using SET.
The command I'm running is:
for /f %i in (22NRmeta.txt) do symaccess -sid 22 list -type stor -dev %i -v | set %m = (findstr "Masking View Names") | echo %i % m >> 22PerDEV_ViewCount.txt

The issue is this: variable %m is returned to console as "%m" and not the result of findstr "Masking View Names")

Sample result returned to file below:
0BEC % m
0BF5 % m
0BFE % m
0C07 % m
0C10 % m

How do I get the result of findstr to echo to the file? Thanks folks!!
0
Comment
Question by:SaffronThePuppy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:WebDevEM
ID: 39660009
Hi,

Just a quick thought on this one... If your command above is a copy/paste of the BAT file, the answer might be as simple as removing the space between % and m in the echo command, like so:
for /f %i in (22NRmeta.txt) do symaccess -sid 22 list -type stor -dev %i -v | set %m = (findstr "Masking View Names") | echo %i %m >> 22PerDEV_ViewCount.txt

Open in new window

0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39660035
No, that doesn't work that way at all. You can't use the output of other commands directly to set a var, you'll need another FOR /F:
for /f "tokens=*" %i in (22NRmeta.txt) do ^
for /F "tokens=*" %L in ('symaccess -sid 22 list -type stor -dev %i -v ^| findstr "Masking View Names" ') do ^
echo,%i %L >> 22PerDEV_ViewCount.txt

Open in new window

Though it would be much better write to the file only once (much faster):
(for /f "tokens=*" %i in (22NRmeta.txt) do ^
for /F "tokens=*" %L in ('symaccess -sid 22 list -type stor -dev %i -v ^| findstr "Masking View Names" ') do ^
echo,%i %L ) > 22PerDEV_ViewCount.txt

Open in new window

And of course you'll need to use %% instead of % if you run that in a batch file instead of the (interactive) command prompt.
0
 
LVL 9

Expert Comment

by:WebDevEM
ID: 39660045
@Qlemo - Thanks for chiming in on this one... I'm on a Mac now so I don't have access to experiment with it.  Looks like Saffron's in good hands.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39660073
Since you have also included PowerShell in your tags:
foreach ($i in Get-Content 22NRmeta.txt) {
  symaccess -sid 22 -list -type stor -dev $i -v |
    select-string 'Masking View Names' | % {
      write-output $i $_
   }
}

Open in new window

0
 

Author Closing Comment

by:SaffronThePuppy
ID: 39660119
Awesome and thanks! I will play with the powershell also. Appreciated!
0

Featured Post

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popular third-party chat platforms like Slack, Discord, and Telegram are just a few of the many new productivity applications that are being hijacked by cybercriminals to create command-and-control (C&C) communications infrastructures for their malw…
Let’s face it: one of the reasons your organization chose a SaaS solution (whether Microsoft Dynamics 365, Netsuite or SAP) is that it is subscription-based. The upkeep is done. Or so you think.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question