Solved

I need to create a file if a string exists in Windows

Posted on 2011-09-15
11
342 Views
Last Modified: 2012-05-12
I have a text file example below.

I need a vbs or some other small script to read the file.  If the following string appears :

"R        ASAP"    or "R  ASAP"  

If either of those two strings appear, then i would like a file to be created in c:\STAT.  it can be a simple file called STATEXISTS.txt.  Then, i would run another process to react in a certain way if that STATEXISTS.txt file actually does exist.  

So, that is all.  Just to watch for two strings and if either of them show up in the file, a new file will be created.  I have other processes ready, just need this peice of the puzzle resolved before I can move on.  THANKS




M F   82     3/17/2009 16:27 CRW CHEST,2-VIEW;PA+LATERAL R ASAP      ER        PrD    
   M F   82     3/17/2009 18:20 CTW CHEST ANGIO     R        ASAP       ER        PrD    
M F   84     12/20/2010 13:31 CTW HEAD WO        R        ASAP Emergen ER      PrD      
F   84     12/21/2010 19:38 CTW HEAD WO        R        ASAP Emergen ER      PrD      
F   19     1/12/2011 20:51 CRW CHEST,2-VIEW;PA+LATERAL R ASAP Emergen ER     PrD    
F   49     5/18/2011 05:39 CRW CHEST PORTABLE,1-VIEW R  ASAP Inpatient ICU   ApD    
F   46     4/28/2006 09:35 US UPPER EXT VENOUS UNI/RT R
M   56     7/24/2006 16:18 CT FACIAL MAXILLOFACIAL AREA R
F   47     12/27/2006 10:40 US THRYOID         R
M F   80     1/19/2007 17:33 CRW CHEST,2-VIEW;PA+LATERAL R           ER
M F   80     5/10/2007 10:41 CAD CAD W/SCREENING R                   9D
M F   80     5/10/2007 10:41 MG SCREEN;DIGITAL BILT R                9D
M F   81     12/30/2007 13:05 CRW CHEST,2-VIEW;PA+LATERAL R          ER        PrD      
M F   82     10/27/2008 09:06 MG SCREEN, DIGITAL; BI R               9D        PrD    
M F   83     4/13/2010 12:24 CRW KNEE,3-VIEW;RGT R             OUTPATI RA      PrD      
M F   83     5/27/2010 10:37 MG SCREEN, DIGITAL; BI R          OBSERV 9E       PrD      
F   53     8/10/2010 14:45 MRI SPINE;LUMBAR    R
M F   84     1/11/2011 18:22 CRW HAND,3-VIEW MIN;LFT R         Emergen ER      PrD    
M F   84     1/11/2011 18:26 CRW CHEST,2-VIEW;PA+LATERAL R     Emergen ER      PrD    
   F   19     2/02/2011 08:12 CTI SINUS SCREENING R             Outpatie IM     PrD    
                                                                                                                         



0
Comment
Question by:weklica
  • 7
  • 2
  • 2
11 Comments
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
OK, how about this


@echo off
del c:\stat\statexists.txt 2>NUL
findstr " R*ASAP " yourfile.txt && echo It exists - found in yourfile.txt at %date% %time%> c:\stat\statexists.txt

Specifically that is looking for the string spac, then R, then any number of chars then ASAP then a space which I imagine will do what you are after.  We can be more specific if needed.

If the string is found then the command after the && is run, which in this case is creating a file called statexists.txt with the word It exists in.
The del command first makes sure that the file doesn't exist first.

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
That is batch file btw -- enter in notepad, save As, change it to show "all files", save it as whatever.cmd ... sorry if stating the obvious here!
0
 

Author Comment

by:weklica
Comment Utility
It can only be one or more spaces.  If anything but spaces between R and the ASAP, then it needs to not count.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
not sure if this will come out from here, if not will post from pc later

findstr " R[ ]*ASAP" yourfile.txt
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Hmm, lost the - there:

findstr " R[ - ]*ASAP " yourfile.txt

i.e. space, then R, then one or more spaces, then ASAP followed by space.

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 100 total points
Comment Utility
I think was where Steve was headed, you need the /R switch to invoke regular expression mode.  And you may want to tweak the filter, right now it requires a space before and after, as well as at least one space in the middle. I also suppressed the output of the matching lines since you probably don't want to see them.  If you want to ignore case (right now only caps match) then add the /I switch as well.

@echo off
del c:\stat\statexists.txt 2>NUL
findstr /R " R [ ]*ASAP " yourfile.txt && echo It exists - found in yourfile.txt at %date% %time%> c:\stat\statexists.txt

Open in new window

~bp
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 400 total points
Comment Utility
hmm, that will teach me to do it from mobile without help text to hand :-)

I have since tried the one above though and it seems to work anyway - never was much good t findstr regular expressions mind!

steve
0
 

Author Closing Comment

by:weklica
Comment Utility
Thanks to you both!
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
No problem, and thanks to Bill for pointing out correct syntax!
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
For what it's worth, I think the following is the correct and needed syntax:

findstr /R /C:" R [ ]*ASAP "

If you don't do the /C then I believe FINDSTR "or"s together the substrings in the quoted literal.  For example if I do FINDSTR /R "this that" it will find lines that have EITHER this or that in them. Not what we want.  Below is a small set of tests on the supplied input, notice the last one is the one that gives what we want.

[Fri 09/16/2011  7:33:17.53 c:\ee]findstr " R[ - ]*ASAP " EE27311064.txt
M F   82     3/17/2009 16:27 CRW CHEST,2-VIEW;PA+LATERAL R ASAP      ER        PrD
F   19     1/12/2011 20:51 CRW CHEST,2-VIEW;PA+LATERAL R ASAP Emergen ER     PrD
F   49     5/18/2011 05:39 CRW CHEST PORTABLE,1-VIEW R  ASAP Inpatient ICU   ApD
M F   80     1/19/2007 17:33 CRW CHEST,2-VIEW;PA+LATERAL R           ER
M F   81     12/30/2007 13:05 CRW CHEST,2-VIEW;PA+LATERAL R          ER        PrD
M F   83     4/13/2010 12:24 CRW KNEE,3-VIEW;RGT R             OUTPATI RA      PrD
M F   84     1/11/2011 18:22 CRW HAND,3-VIEW MIN;LFT R         Emergen ER      PrD
M F   84     1/11/2011 18:26 CRW CHEST,2-VIEW;PA+LATERAL R     Emergen ER      PrD

[Fri 09/16/2011  7:33:36.52 c:\ee]findstr /R " R [ ]*ASAP " EE27311064.txt
M F   82     3/17/2009 16:27 CRW CHEST,2-VIEW;PA+LATERAL R ASAP      ER        PrD
   M F   82     3/17/2009 18:20 CTW CHEST ANGIO     R        ASAP       ER        PrD
M F   84     12/20/2010 13:31 CTW HEAD WO        R        ASAP Emergen ER      PrD
F   84     12/21/2010 19:38 CTW HEAD WO        R        ASAP Emergen ER      PrD
F   19     1/12/2011 20:51 CRW CHEST,2-VIEW;PA+LATERAL R ASAP Emergen ER     PrD
F   49     5/18/2011 05:39 CRW CHEST PORTABLE,1-VIEW R  ASAP Inpatient ICU   ApD
F   46     4/28/2006 09:35 US UPPER EXT VENOUS UNI/RT R
M   56     7/24/2006 16:18 CT FACIAL MAXILLOFACIAL AREA R
F   47     12/27/2006 10:40 US THRYOID         R
M F   80     1/19/2007 17:33 CRW CHEST,2-VIEW;PA+LATERAL R           ER
M F   80     5/10/2007 10:41 CAD CAD W/SCREENING R                   9D
M F   80     5/10/2007 10:41 MG SCREEN;DIGITAL BILT R                9D
M F   81     12/30/2007 13:05 CRW CHEST,2-VIEW;PA+LATERAL R          ER        PrD
M F   82     10/27/2008 09:06 MG SCREEN, DIGITAL; BI R               9D        PrD
M F   83     4/13/2010 12:24 CRW KNEE,3-VIEW;RGT R             OUTPATI RA      PrD
M F   83     5/27/2010 10:37 MG SCREEN, DIGITAL; BI R          OBSERV 9E       PrD
F   53     8/10/2010 14:45 MRI SPINE;LUMBAR    R
M F   84     1/11/2011 18:22 CRW HAND,3-VIEW MIN;LFT R         Emergen ER      PrD
M F   84     1/11/2011 18:26 CRW CHEST,2-VIEW;PA+LATERAL R     Emergen ER      PrD
   F   19     2/02/2011 08:12 CTI SINUS SCREENING R             Outpatie IM     PrD

[Fri 09/16/2011  7:34:05.67 c:\ee]findstr /R /C:" R [ ]*ASAP " EE27311064.txt
M F   82     3/17/2009 16:27 CRW CHEST,2-VIEW;PA+LATERAL R ASAP      ER        PrD
   M F   82     3/17/2009 18:20 CTW CHEST ANGIO     R        ASAP       ER        PrD
M F   84     12/20/2010 13:31 CTW HEAD WO        R        ASAP Emergen ER      PrD
F   84     12/21/2010 19:38 CTW HEAD WO        R        ASAP Emergen ER      PrD
F   19     1/12/2011 20:51 CRW CHEST,2-VIEW;PA+LATERAL R ASAP Emergen ER     PrD
F   49     5/18/2011 05:39 CRW CHEST PORTABLE,1-VIEW R  ASAP Inpatient ICU   ApD

Open in new window

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Hmm, could be.  from my own testing ... including adding some other characters betwen R and ASAP to test:


(
echo ================== " R[ - ]*ASAP "
findstr " R[ - ]*ASAP " test.txt
echo.
echo ================== /R /C:" R [ ]*ASAP "
findstr /R /C:" R [ ]*ASAP " test.txt
echo.
echo ================== /R " R [ ]*ASAP "
echo.
findstr /R " R [ ]*ASAP " test.txt
echo.
echo ================== " R*ASAP "
findstr " R*ASAP " test.txt
echo.
echo ================== /R:" R[ - ]*ASAP "
findstr /R:/C:" R[ - ]*ASAP " test.txt
echo.
echo ================== 
)>asap.txt
start asap.txt

Open in new window

================== " R[ - ]*ASAP "
M F   82     3/17/2009 16:27 CRW CHEST,2-VIEW;PA+LATERAL R ASAP      ER        PrD     
F   19     1/12/2011 20:51 CRW CHEST,2-VIEW;PA+LATERAL R ASAP Emergen ER     PrD     
F   49     5/18/2011 05:39 CRW CHEST PORTABLE,1-VIEW R  ASAP Inpatient ICU   ApD     
M F   80     1/19/2007 17:33 CRW CHEST,2-VIEW;PA+LATERAL R           ER 

================== /R /C:" R [ ]*ASAP "
M F   82     3/17/2009 16:27 CRW CHEST,2-VIEW;PA+LATERAL R ASAP      ER        PrD     
F   84     12/21/2010 19:38 CTW HEAD WO        R        ASAP Emergen ER      PrD      
F   19     1/12/2011 20:51 CRW CHEST,2-VIEW;PA+LATERAL R ASAP Emergen ER     PrD     
F   49     5/18/2011 05:39 CRW CHEST PORTABLE,1-VIEW R  ASAP Inpatient ICU   ApD     

================== /R " R [ ]*ASAP "

M F   82     3/17/2009 16:27 CRW CHEST,2-VIEW;PA+LATERAL R ASAP      ER        PrD     
   M F   82     3/17/2009 18:20 CTW CHEST ANGIO     R  NOT   ASAP       ER        PrD     
M F   84     12/20/2010 13:31 CTW HEAD WO        R    XX   ASAP Emergen ER      PrD      
F   84     12/21/2010 19:38 CTW HEAD WO        R        ASAP Emergen ER      PrD      
F   19     1/12/2011 20:51 CRW CHEST,2-VIEW;PA+LATERAL R ASAP Emergen ER     PrD     
F   49     5/18/2011 05:39 CRW CHEST PORTABLE,1-VIEW R  ASAP Inpatient ICU   ApD     
F   46     4/28/2006 09:35 US UPPER EXT VENOUS UNI/RT R 
M   56     7/24/2006 16:18 CT FACIAL MAXILLOFACIAL AREA R 
F   47     12/27/2006 10:40 US THRYOID         R 
M F   80     1/19/2007 17:33 CRW CHEST,2-VIEW;PA+LATERAL R           ER 
M F   80     5/10/2007 10:41 CAD CAD W/SCREENING R                   9D 

================== " R*ASAP "
M F   82     3/17/2009 16:27 CRW CHEST,2-VIEW;PA+LATERAL R ASAP      ER        PrD     
   M F   82     3/17/2009 18:20 CTW CHEST ANGIO     R  NOT   ASAP       ER        PrD     
M F   84     12/20/2010 13:31 CTW HEAD WO        R    XX   ASAP Emergen ER      PrD      
F   84     12/21/2010 19:38 CTW HEAD WO        R        ASAP Emergen ER      PrD      
F   19     1/12/2011 20:51 CRW CHEST,2-VIEW;PA+LATERAL R ASAP Emergen ER     PrD     
F   49     5/18/2011 05:39 CRW CHEST PORTABLE,1-VIEW R  ASAP Inpatient ICU   ApD     

================== /R:" R[ - ]*ASAP "
M F   82     3/17/2009 16:27 CRW CHEST,2-VIEW;PA+LATERAL R ASAP      ER        PrD     
F   84     12/21/2010 19:38 CTW HEAD WO        R        ASAP Emergen ER      PrD      
F   19     1/12/2011 20:51 CRW CHEST,2-VIEW;PA+LATERAL R ASAP Emergen ER     PrD     
F   49     5/18/2011 05:39 CRW CHEST PORTABLE,1-VIEW R  ASAP Inpatient ICU   ApD     

==================

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now