Computing.Net > Forums > Programming > Find string inside a variable,batch

Find string inside a variable,batch

Reply to Message Icon

Original Message
Name: shr00m
Date: January 17, 2005 at 17:16:10 Pacific
Subject: Find string inside a variable,batch
OS: Win Xp Pro
CPU/Ram: Amd 3200,1Gb
Comment:

Hi,
here's what i could need some help with..
1. I want to read a whole txt document into a variable. Also it would be nice to check if a certain word inside the variable exist so i could compare two variables with the if command.

2. I would like to compare 2 files, and use a if command to do some action if they are alike/not alike

3. I would like this to work, but it doesnt:
echo echo File %wheretoo0%\%wheretoo1%%choise1% already exist, skipping file >> ErrorLog.txt >>%filename%.txt

I have a main batch that generates a new file with its own script.
I want to echo >>errorlog.txt into filename.txt, but it doesnt seems to work when using >>errorlog.txt>>filename.txt
The filename.txt is supposed to check if a filename exist and then echo "File doesnt exist" into a errorlog if file doesnt exist.

Ok.. Its kinda late, so i dont know if any of this is understandable..

Hope anyone can help me! :)


Report Offensive Message For Removal


Response Number 1
Name: Mechanix2Go
Date: January 17, 2005 at 19:35:15 Pacific
Reply: (edit)

Hi Schroom,

Sounds like it was kinda late.

First, You will not be able to set a variable as a whole text file unless the file is quite small; you'll soon be out of environment.

You can do compares with FC

if exist filename.ext echo msg1 >> my.log

HTH

M2


Report Offensive Follow Up For Removal

Response Number 2
Name: shr00m
Date: January 18, 2005 at 06:54:21 Pacific
Reply: (edit)

Hehe.. It was late ye..

Well, i dont want a big txt file to be read.
max 3lines would do just fine.
I would then like to search for a string inside the variable containing the text.
For example an ip adress

I'll try to show what i want:
If "string" found in %variable% goto next

When it comes to file comparing, i'll illustrate what i was thinking aswell:

If file1=file2 goto next

I dont know if this is possible tho, or if i have to use fc file1 file2>>result.txt
,then read result into a variable and then look for a string inside the variable.

Hope that made more sense..

And finally, hope anyone can help me with the last one (nr.3).

Problem is that i cant echo this into the file called filename.txt
"echo File %wheretoo0%\%wheretoo1%%choise1% already exist, skipping file >> ErrorLog.txt "



Report Offensive Follow Up For Removal

Response Number 3
Name: Mechanix2Go
Date: January 18, 2005 at 07:51:38 Pacific
Reply: (edit)

That makes more sense.

One piece at a time.

For a file compare:

::==========
fc file1 file2
if errorlevel 1 goto :different

::Here's where we do what we do when they are the same

goto :end

:different
They are fifferent; so we're here

:end


M2


Report Offensive Follow Up For Removal

Response Number 4
Name: shr00m
Date: January 18, 2005 at 10:18:18 Pacific
Reply: (edit)

Ahh.. Works perfectly! Thnx:)

Is it by any chance you would know how i can solve the problem with searching for a string inside a variable? (or any website you could refer too)

If "string" in variable found goto :found


Report Offensive Follow Up For Removal

Response Number 5
Name: Mechanix2Go
Date: January 18, 2005 at 10:31:17 Pacific
Reply: (edit)

@echo off
echo %var% | find "string"

if errorlevel 1 goto :not-found

:found
::Do found stuff
goto :end

:not-found
::Do not found stuff

:end

M2


Report Offensive Follow Up For Removal


Response Number 6
Name: shr00m
Date: January 18, 2005 at 10:50:22 Pacific
Reply: (edit)

Thnx once again:) You have been a big help to me. I'll have abit more look into the use of errorlevels..


Report Offensive Follow Up For Removal

Response Number 7
Name: Mechanix2Go
Date: January 18, 2005 at 10:59:30 Pacific
Reply: (edit)

SchrOOm,

One thing to whatch out for is that the:

if errorlevel x

test evaluates true if the errorlevel is equal to **OR MORE THAN** x

HTH

M2


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Find string inside a variable,batch

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge