?
Solved

Batch File Check For Numeric Digits

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

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 56

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

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