0

I have a file1.txt with some file names. I want to read from the file and if the file name contains "xyz" string i need to run some commands and if not some other commands Not able to do this and need your help,tried a lot of methods but not able to figure out Here's the sample code i tried:

 @echo off
 setlocal EnableDelayedExpansion

 for /F "tokens=1,2,3 delims=" %%i in (file1.txt) do (
     set f=%%i
     set z=XYZ
     echo !f! >>test.txt
     if /i "!f!" == "!z!" (
         echo matched >> test.txt
     ) else ( 
         echo nomatch >> test.txt
     )
 )
Renats Stozkovs
  • 2,549
  • 10
  • 22
  • 26
Soorajh
  • 3
  • 1
  • 2
  • To compare whether a string contains another one, you can do it like this: `if "!STRING!"=="!STRING:%SUB%=!" echo Sub-string "%SUB%" found within "!STRING!".` or you use the [`find` command](http://ss64.com/nt/find.html): `find "%SUB%" "file1.txt"` (which returns only those lines within `file1.txt` that contain the sub-string in `%SUB%`; add the `/I` option to do a case-sensitive search) – aschipfl Apr 18 '17 at 12:55
  • Quite interesting that there appear two questions about the [same topic](http://stackoverflow.com/q/43471346) within less than one hour... – aschipfl Apr 18 '17 at 13:00
  • Possible duplicate of [How to pass command line parameters to a batch file?](http://stackoverflow.com/questions/26551/how-to-pass-command-line-parameters-to-a-batch-file) – Nathaniel Flick Apr 18 '17 at 13:14
  • @aschipfl I tried your code like this : `@echo off setlocal EnableDelayedExpansion for /F "tokens=1,2,3 delims=" %%i in (file1.txt) do ( set f=%%i set z=XYZ echo !f! >>test.txt if "!f!"=="!f:%z%=!" ( echo matched >> test.txt ) else ( echo nomatch >> test.txt ) )` But i am getting only "no match" written in test.txt even if XYZ string is present in file1.txt – Soorajh Apr 18 '17 at 13:56
  • 1
    @aschipfl's suggestion have an error. The right way is: if the comparison is _different_, then the other substring is contained in the first one; that is: `if "!f!" neq "!f:%z%=!" ( echo matched >> test.txt ) else ( echo nomatch >> test.txt )`. Also, note that in order for this to work you must `set z=XYZ` _before_ the `for` loop... – Aacini Apr 18 '17 at 16:17
  • You are totally right, @Aacini, thanks for clarification! – aschipfl Apr 18 '17 at 17:06
  • @Aacini Thanks a lot . . :) Its working perfectly now. . . Thanks a million times. . . – Soorajh Apr 19 '17 at 06:07
  • @aschipfl Thanks a lot for your help . . . :) was stuck on this problem for a day. . . – Soorajh Apr 19 '17 at 06:09

1 Answers1

3

To compare whether a string contains another one, you can do it like this (note that this is case-insensitive, because the underlying sub-string expansion syntax is case-insensitive on its own):

if "!STRING!"=="!STRING:%SUB%=!" echo Sub-string "%SUB%" NOT found within "!STRING!"

Here the above approach is implemented into your script:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_INFILE=file1.txt"
set "_OUTFILE=test.txt"
set "_SUBSTR=XYZ"

>> "%_OUTFILE%" (
    for /F "usebackq tokens=1-3 delims=" %%L in ("%_INFILE%") do (
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        echo(!LINE!
        if not "!LINE!"=="!LINE:%_SUBSTR%=!" (
            echo matched
        ) else ( 
            echo nomatch
        )
        endlocal
    )
)

endlocal
exit /B

In addition, I improved the following things:

  • constant-like variables are predefined at the beginning;
  • the returned text is redirected once only; replace >> by > to overwrite an already existing file rather than appending to it;
  • delayed expansion is toggled within the loop in order not to lose exclamation marks in the text; note that the string in _SUBSTR must not contain such;
  • all file paths used in the script are enclosed within quotation marks;
  • the quoted set syntax is used throughout the script;
  • code indention is used for improved readability;

Alternatively, you could use the find command, which returns only those lines within file1.txt that contain the sub-string in %SUB%; add the /I option to do a case-insensitive search:

find "%SUB%" "file1.txt"
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Thank a ton @aschipfl . . :) I have created the batch file and its working perfectly . . – Soorajh Apr 19 '17 at 13:56
  • Just a quick question . . . This batch file i use only to run in the working directory. . If i create an .exe out of this batch file using iexpress. . . and copy the exe to folder where i want the bat to be executed it doesnt happen because iexpress extracts it to some temp folder. . Is there any work around for this.i.e whichever folder i run the .exe the .bat file should also get executed in the same folder . . .Please do let me know if i need to post this as a different question. Thanks a lot once again – Soorajh Apr 19 '17 at 14:04
  • You could include the path to the files in the related variables, like: `set "_INFILE=D:\Data\file1.txt"`; or you change that to this: `set "_INFILE=%~1"`, so you can pass the (full) file path as the first command line argument to the batch file... – aschipfl Apr 19 '17 at 15:47