Solved

Display variable results form a batch file?

Posted on 2007-03-27
4
485 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now