Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Batch File Check For Numeric Digits

Posted on 2010-08-26
8
Medium Priority
?
1,916 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 58

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 58

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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 1000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview

610 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