Solved

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

Posted on 2011-09-15
11
353 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
[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
  • 7
  • 2
  • 2
11 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36546325
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
ID: 36546331
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
ID: 36546491
It can only be one or more spaces.  If anything but spaces between R and the ASAP, then it needs to not count.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:Steve Knight
ID: 36546517
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
ID: 36546668
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
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 100 total points
ID: 36546691
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
ID: 36547393
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
ID: 36547503
Thanks to you both!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36547753
No problem, and thanks to Bill for pointing out correct syntax!
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36548537
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
ID: 36548584
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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