Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

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

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
weklica
Asked:
weklica
  • 7
  • 2
  • 2
2 Solutions
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
weklicaAuthor Commented:
It can only be one or more spaces.  If anything but spaces between R and the ASAP, then it needs to not count.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Steve KnightIT ConsultancyCommented:
not sure if this will come out from here, if not will post from pc later

findstr " R[ ]*ASAP" yourfile.txt
0
 
Steve KnightIT ConsultancyCommented:
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
 
Bill PrewCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
weklicaAuthor Commented:
Thanks to you both!
0
 
Steve KnightIT ConsultancyCommented:
No problem, and thanks to Bill for pointing out correct syntax!
0
 
Bill PrewCommented:
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
 
Steve KnightIT ConsultancyCommented:
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

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.

  • 7
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now