Solved

Batch File Check For Numeric Digits

Posted on 2010-08-26
8
1,791 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
[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
8 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33536409
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
ID: 33536415
The actual content of the text file.
0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 33536480
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 1

Author Comment

by:Ryan Rood
ID: 33536498
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
 
LVL 1

Author Comment

by:Ryan Rood
ID: 33536570
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
ID: 33536585
@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
ID: 33536617
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
ID: 33537151
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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