Solved

Batch File Check For Numeric Digits

Posted on 2010-08-26
8
1,673 Views
Last Modified: 2012-05-10
Hi,

So here's the situation, I need to create a batch file that will look at a text file in the same directory - if the text file has no numbers in it - it will skip the section and move to the next step. If there are numbers than it should execute a command and then move to the next step.

The text files will always be a constant name, I will be creating multiple for different reports.

Any thoughts or programs that I can use to do this?

Thanks,
Ryan
0
Comment
Question by:Ryan Rood
8 Comments
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Not sure I understand.  Do you mean you want to check the file name for all numbers, or the actual content of a single file to see if any of the data in the file is non numeric?

~bp
0
 
LVL 1

Author Comment

by:Ryan Rood
Comment Utility
The actual content of the text file.
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
Comment Utility
You might be able to use FINDSTR for this.  To get the idea try this on one of the files that is all numeric, and then on one that is not:

FINDSTR [^0-9] filename.txr

You can see other options on the FINDSTR command via FINDSTR /?

The example I gave will display all lines containing a character other than 0 through 9.  That's regular expression syntax, if you have worked with that.

In a BAT file, you can suppress the actual output of the lines from the command, and just return a coult of the lines, or just check the return code to see if any matches were found or not.

If this feels like it might work we can help further as needed, but figured I'd see if this is on the right track first.

~bp
0
 
LVL 1

Author Comment

by:Ryan Rood
Comment Utility
Yes this will work ... if it finds any numbers than do nothing ... if it finds only normal characters ... delete the txt file. Than I can run IF EXIST on each file to complete my commands.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:Ryan Rood
Comment Utility
So testing this ... when it finds the numbers it displays them on screen ... ok ... this can be supressed. How do I tell it to delete the file if it doesn't find anything in the output.
0
 
LVL 3

Accepted Solution

by:
Fr0zT earned 250 total points
Comment Utility
@echo off
findstr /R /C:"[0-9]" test.txt > NUL
if "%ERRORLEVEL%"=="1" goto nonumbers
echo Found Numbers
goto end
:nonumbers
echo No numbers
:end
0
 
LVL 1

Author Comment

by:Ryan Rood
Comment Utility
Ok ... if I run this on both files it displays properly. Success! :) This deletes the text file appropriately. Let me do some additional testing and I will award the points to you!

@echo off
findstr /R /C:"[0-9]" BS-BM-01-Text.txt > NUL
if "%ERRORLEVEL%"=="1" goto nonumbers
ECHO EMAIL FILE
goto end1
:nonumbers
DEL BS-BM-01-Text.txt
:end1

findstr /R /C:"[0-9]" BS-BM-02-Text.txt > NUL
if "%ERRORLEVEL%"=="1" goto nonumbers
ECHO EMAIL FILE
goto end1
:nonumbers
DEL BS-BM-02-Text.txt
:end2
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
BTW you can use syntax such as

@echo off
setlocal enabledelayedexpansion

findstr /R /C:"[0-9]" BS-BM-01-Text.txt > NUL && (
  echo Found !errorlevel!
  echo Do email here
) || (
  echo NOT found !errorlevel!
  DEL /Y BS-BM-01-Text.txt
)

which uses && and || which say to run the following command if the previous command reported an error or not.  Personal preference but I find it more logical.  Either that or:

findstr /R /C:"[0-9]" BS-BM-01-Text.txt > NUL
if "%ERRORLEVEL%"=="1" (
  ECHO EMAIL FILE
) ELSE (
  DEL BS-BM-01-Text.txt
)

or

findstr /R /C:"[0-9]" BS-BM-01-Text.txt > NUL
if "%ERRORLEVEL%"=="1" (ECHO EMAIL FILE) ELSE (DEL BS-BM-01-Text.txt)

which is less likely to get caught out with line names the same (e.g. your two called nonumbers...)

Steve
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

11 Experts available now in Live!

Get 1:1 Help Now