Solved

Batch File Check For Numeric Digits

Posted on 2010-08-26
8
1,768 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to find then copy to anotehr file 3 62
How to batch remove spreadsheet password 19 286
Windows batch files 3 65
Windows updates batch files 7 103
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

762 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