Solved

Display variable results form a batch file?

Posted on 2007-03-27
4
490 Views
Last Modified: 2013-12-29
Hi there

Wonder if you can help me?

I have the following batch code (windowsXP)

set Files=attrib.exe /s c:\data\*.* |find /c /v ""
echo the value of Files is :%Files%

nFiles should display 11, but on the echo the value of nFiles is :%Files% line I'm getting 0

Can someone please tell me why this isnt working

Also

I have a filename called 001_004_test.xml

How do I get the value 004 and store that in a variable

Thanks
mosuemat24
0
Comment
Question by:mousemat24
[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
  • 3
4 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 18799514
You can't do it the way you have there but you can do similar with the for command:

@echo off
for /f "tokens=1" %%a in ('dir /s /b /a-d *.xml ^| find /c /v ""') do echo Result is %%a

for the second half

@echo off
for /f "tokens=2 delims=_" %%a in ("001_004_test.xml") do echo %%a & set myresult=%%a

To run these from the command line use single % instead of double

hth

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 18799541
You can of course change the "echo result is %%a" to "set files=%%a" in the first command to add it to a variable.  

The dir command returns all XML files in bare format (filename only) through all subdirs and excludes showing directory names (/a-d).  This is parsed using for by using the ' ' characters around it.  The for command splits the line at each space or comma by default and runs the command after the do for each line found in the output -- in this case the output is one line with the number on which is pulled out as the first token on the line.

Same sort of thing for the second question.  Sets the delimiter to _ and pulls the second token off the line.
0
 

Author Comment

by:mousemat24
ID: 18800044
Thank you for all your help!! that worked
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 18800203
No problem!

Steve
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

636 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