BookmarkSubscribeRSS Feed
desertsp2
Calcite | Level 5

As part of a larger process, I need to scan a text file for a string. To do this, I'm using a macro I found online (sorry, can't remember where). I need assistance modifying it so that the two parameters can contain special characters.

 

Here is the macro:

%macro CheckFileForString(FilenameAndPath=,
                          SearchString=);
  %local SearchResult filrf rc fid c;
  %let SearchResult = 0; /* default unless the string is found */
  %let filrf=srcf;
  %let rc=%sysfunc(filename(filrf, &FilenameAndPath));
  %let fid=%sysfunc(fopen(&filrf));
  %if &fid > 0 %then %do;  /* if the file reference exists (can be opened) */
    %do %while(%sysfunc(fread(&fid))=0);  /* while file is still returning contents (until end) */
	                                      /* read portion of the file into the file data buffer */
	                                      /* the position of the file pointer is updated after the read */
	                                      /* operation so successive fread functions read successive file records */
      %let rc=%sysfunc(fget(&fid, charvar)); /* copy characters from file data buffer into a variable */
	                                         /* blanks are are implicit delimiters */

      %if %index(%superq(charvar), &SearchString) %then %do;/* test the character variable */  
	     %let SearchResult=1; /* set the return code */ 
		 %GOTO exit;
		                                                %end;
    %end;

	%exit:
    %let rc=%sysfunc(fclose(&fid));
  %end;
  %let rc=%sysfunc(filename(filrf));
  &SearchResult
%mend CheckFileForString;

...and here is an example which fails. In this case, I'm scanning a config file for a specific parameter. It happens to be in XML format but that's not something I need to concern myself with....I just need to see if it has the string somewhere.

%LET FILE_LOCATION = %STR(C:\Program Files (x86)\Program Name\file.with.multiple.periods);


%IF %CheckFileForString(FilenameAndPath = &FILE_LOCATION ,
                          SearchString = '<parameter>value</parameter>') = 1 %THEN  

do something

 

 

Any assistance would be appreciated!

1 REPLY 1
Astounding
PROC Star

The first suspicious piece is the quotes:

 

%IF %CheckFileForString(FilenameAndPath = &FILE_LOCATION ,
                          SearchString = '<parameter>value</parameter>') = 1 %THEN 

 

Would this work with no need to change the macro?

 

%IF %CheckFileForString(FilenameAndPath = &FILE_LOCATION ,
                          SearchString = <parameter>value</parameter>) = 1 %THEN