?
Solved

DOS batch script to read a input file and only listing the numbers from the 4000 range

Posted on 2012-09-16
13
Medium Priority
?
450 Views
Last Modified: 2013-08-23
I am trying to write a DOS batch script that will read a input file, which has an example of the following lines of text:-

\\srch35\homes\windows\port=3000
\\srch35\homes\windows\port=3000
\\srch35\homes\windows\port=4000
\\srch35\homes\windows\port=4001
\\srch35\homes\windows\port=4245

But only want the numbers listed from the 4000 range into another text file

Any help would be most appreciated
Thanks
0
Comment
Question by:rakkad
  • 8
  • 3
  • 2
13 Comments
 
LVL 11

Expert Comment

by:paultomasi
ID: 38403980
Try this:

@echo off

(for /f "tokens=1,2 usebackq delims==" %%a in ("file.txt") do (
    if %%b gtr 4000 echo %%a=%%b
))>newfile.txt

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38403985
Or, you might prefer this style:

@echo off

for /f "tokens=1,2 usebackq delims==" %%a in ("file.txt") do (
    if %%b geq 4000 echo %%a=%%b>>newfile.txt
)
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38403999
Just incase some decides to mention it first, how about this command:

    findstr /e "4..." file.txt >newfile.txt

Can also be made into a single line batch file ie:

@echo off
findstr /e "4..." file.txt >newfile.txt

Open in new window

Where 'file.txt' is the input file and 'newfile.txt' is the output file. Use '>>' instead of just '>' to append further outputs to the output file.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 59

Expert Comment

by:Bill Prew
ID: 38404115
I'd do it this way:

findstr /R /C:"port=4[0-9][0-9][0-9]" in.txt>out.txt

Open in new window

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38404142
strictly speaking, yes, bill.

btw, i realise i made an error in using 'gtr' rather than 'geq' in line 3 of the follwing code above:

@echo off
(for /f "tokens=1,2 usebackq delims==" %%a in ("file.txt") do (
  if %%b geq 4000 echo %%a=%%b
))>newfile.txt

Open in new window

incidentally, i think 'findstr /e "4..." file.txt' is sufficient here as we can be certain of the format of his data.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38404160
As long as you are okay with picking up a line that ended in "4ABC" or "port=14000" for some reason...

That being said, I guess I'd add /i and /e to my proposed solution to be safe.

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38404193
bill

Going by the sample data, I don't think we're going to see '4ABC' at the end of a line so 'findstr /e "4..." file.txt' is just fine.

BTW, '4ABC' is a valid hexadecimal number.

And again, going by the sample data, I don't think we're going to see '14000' either otherwise we both fall short on that one and in which case, my previous comments (http:#a38403980 and http:#a38403985) would work just fine (providing he chages the 'gtr' to geq' in http:#a38403980).
0
 

Author Comment

by:rakkad
ID: 38483977
The code works to find the range after 4000, but I only want the numbers to be displayed so the text before the number is stripped out e.g.
4000
4001
4023
etc....

Thanks
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38498865
Okay, give this a try:

@echo off
setlocal EnableDelayedExpansion

set FileIn=in.txt
set FileOut=out.txt
set MinPort=4000

(
  for /f "usebackq tokens=2 delims==" %%A in ("%FileIn%") do (
    set Port=%%~A
    if  '!Port:~-1!' EQU '^"' set Port=!Port:~0,-1!
    if !Port! GEQ %MinPort% echo !Port!
  )
)>"%FileOut%"

Open in new window

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38499479
Any shorter than this and it starts to get cryptic:

@echo off
for /f "tokens=2 usebackq delims==" %%a in ("file.txt") do if %%a geq 4000 echo %%a>>output.txt

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38562931
Got this (commandline version) down to 78 characters with input and output files i and o.

(for /f "tokens=2 usebackq delims==" %a in ("i") do @if %a geq 4000 echo %a)>o
0
 
LVL 11

Accepted Solution

by:
paultomasi earned 1500 total points
ID: 38581810
Finally, got this down to only 65 characters with input and output files i and o.

for /f "tokens=2 delims==" %a in (i) do if %a geq 4000 echo %a>>o
Surely this must be it!
0
 

Author Closing Comment

by:rakkad
ID: 39433862
This answer my question correctly
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
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
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

840 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