Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

FileExist() with environement variables ?


  • Please log in to reply
16 replies to this topic
majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
How can I check with this function if this file exists:

%SYSTEMROOT%\SYSTEM32\notepad.exe

thx.
Posted Image

Roland
  • Members
  • 307 posts
  • Last active: Mar 09 2014 07:55 PM
  • Joined: 08 Jun 2006
Like this:

Msgbox % FileExist(SYSTEMROOT "\SYSTEM32\notepad.exe")


majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
I didn't ask that Roland:

ppath = %SYSTEMROOT%\System32\notepad.exe


Is there any function that can handle env var here or no ?
Like (imagine this): FileExist(ppath)

or I have to split the string manualy in my own wrapper function

If no, this should be considered as an update to this function.

Or even this path
(in cmd.exe)
set sys=system32
set pad=notepad.exe

(in script started from this same cmd)
ppath = %SYSTEMROOT%\%SYS%\%PAD%
Posted Image

Roland
  • Members
  • 307 posts
  • Last active: Mar 09 2014 07:55 PM
  • Joined: 08 Jun 2006
You lost me.

ppath = %SYSTEMROOT%\System32\notepad.exe


Is there any function that can handle env var here or no ?
Like (imagine this): FileExist(ppath)


Okay, I'm imagining it. FileExist() will return an empty string if %ppath% doesn't exist or its attributes if it does. What's the problem?

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
I forgot #NoEnv

so, just add it above and think again.
Sorry.
Posted Image

Roland
  • Members
  • 307 posts
  • Last active: Mar 09 2014 07:55 PM
  • Joined: 08 Jun 2006
It's all in the docs:

When #NoEnv is in effect, the script should use EnvGet to retrieve environment variables


Like this:

#NoEnv
EnvGet, SysRoot, SYSTEMROOT
Msgbox % FileExist(SysRoot "\SYSTEM32\notepad.exe")


majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Hej Roland...

How long will this going to take :lol: :lol:

The question is simply is there any file checking function that is env-var aware .. I guess not... so its cool. You use expression above, and that doesn't count.

BTW, I don't use scripts without #NoEnv, since it will become default one day.
Posted Image

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
This is what I thought:

#NoEnv
 ppath = blabla bla asd `%TEMP`%....`%SYSTEMROOT`%.. 
 MsgBox % ExpandEnvVars(ppath)
return

ExpandEnvVars(ppath)
{
	open  = 0
	s_idx = 0
	res = %ppath%
	Loop
	{ 
		StringGetPos idx, ppath, `%, L, s_idx
		if (idx = -1) 
			break
	
		open := !open
		if (!open)
		{
			StringMid env, ppath, % s_idx + 1, % idx - s_idx
			EnvGet env_val, %env%
			if (env_val != "")
				StringReplace res, res, `%%env%`%, %env_val%
			else
			  open := !open
		}

		s_idx := idx + 1
	}
	return res
}

Posted Image

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
I might be useful that A_WinDir (which is a built-in variable) is the same as the environment variable "SystemRoot" on most systems (perhaps just about all of them).

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
2 Chris
:)
So, you actually say, that all we need is those several integrated env vars ?

Every serious computer user I know has several non-standard as well, like on my systme: %UTILS% %COMMANDER_PATH% %EDITOR%... etc

It also shouldn't take much to guess, that if you want to create lanucher that supports env variables you can't use integrated solutions.

Example:
I am parsing Total Commander menu file which contains commands, some of them to be executables for launching, and those can be specified with fensy constructions like those I mention above. So I acctually receive info about the path in a string containing unknown count of env variables. I need to check if that file exists and run it if so.
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
I replied in the Scripts & Functions section, but I repeat here for those searching a topic:
Transform, Deref, String is probably what you are looking for.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Ok, PhilLo, thx very much.

I didn't see that.
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Well, it is well hidden in a series of obsolete transforms (Asc, Chr, etc., all available as functions/operators now). But there are still some other gems (Unicode, HTML).
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
As you probably know, Transform Deref can't resolve environment variables when #NoEnv is in effect. If this affects you, you could use PhiLho's solution at Transform, DerefEnv

ParanoidX
  • Members
  • 148 posts
  • Last active: Jan 14 2007 02:00 AM
  • Joined: 16 Dec 2005
or simply go directly to the source i.e. parse out autoexec.bat or setvar.bat (or wherever the batch file is which sets the env).
Posted Image
546F206C69766520
6973204368726973742C0D746F2064696520
6973206761696E2E0D285068696C20313A323129