Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Display variable results form a batch file?

Posted on 2007-03-27
4
Medium Priority
?
491 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
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.
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

670 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