ZTree.com  | ZEN  | About...  

 Index   Back

[Tip] Admin vs NonAdmin   [Tip]

By: Tim Rude       
Date: Jun 26,2020 at 17:22

Sometimes I want to run ZTree as Administrator, sometimes not. I thought it would be cool to be able to decide easily on the fly which one I wanted.

To do this I could either (1) have 2 shortcuts, one for Admin and one without; (2) right-click a single shortcut and select 'Run as Administrator' when I wanted to do that; or (3) have a single shortcut that asks me which way I want to run it.

Since I usually start ZTree with a Ctrl-Shift-Z hotkey, I like method (3), and I use the UAC prompt as my way of asking. If I say Yes to the UAC prompt, it runs as Admin. If I say No to the UAC prompt, it runs as non-Admin.

After some research and trial and error, here's how I accomplish method (3) in a batch file:

---- batch file code snippet ----

:: First try opening as Admin

Powershell Start -FilePath {C:\ZTREEWIN\ZTW.EXE} -ArgumentList {%* /APIT /Y /TADMIN} -WorkingDirectory {C:\} -Verb Runas > nul

if "%errorlevel%"=="0" goto :EOF

:: Try opening without Admin if user declines UAC prompt

start "ZTreeWin" /B /D"C:\" /b "C:\ZTREEWIN\ZTW.EXE" %* /APIT /Y

goto :EOF

---- end batch file code snippet ----

Some notes:

The /APIT and /Y command line parameters for ZTW are just my preferences. Not required for this to work. Also, the /TADMIN parameter is just to put ADMIN in the ZTree title bar as a reminder when it's in that mode. Also not required.

On my system, I generally like the ZTreeWin folder directly off the root rather than buried under Program Files. But it can be adjusted to work from there too.

This runs the 32-bit version of ZTree but it can also work with the 64-bit version.

In fact, my actual batch file to start ZTree is more complex than this snippet. It determines the OS flavor and runs 32 or 64 bit accordingly. It also searches for ZTree in Program Files\ZTreeWin, Program Files(x86)\ZTreeWin, and \ZTreeWin (of any drive) and runs whichever one it finds. That way I can use this batch file on another system that may be configured differently (or on a flash drive) and it still works.

In case anyone's interested, here's the full batch file (which I call StartZTW.cmd):

--- full batch file code ----

@echo off

setlocal

:: Set preferred ZTree command-line options

set ZTWCL= /APIT /Y

:: Determine whether 32-bit or 64-bit OS, and set ZTW accordingly

reg.exe query HKLM\Hardware\Description\System\CentralProcessor\0 | find /i "x86 Family" > nul

if errorlevel 1 goto 64BIT

:32BIT

set ZTW=ZTW.EXE
goto FINDZTREE

:64BIT

set ZTW=ZTW64.EXE
goto FINDZTREE

:FINDZTREE

:: Try to find and run ZTree, passing command parameters

:LOC1

:: Look for Program Files\ZTreeWin folder

if "%ProgramFiles%"=="" goto LOC2
if not exist "%ProgramFiles%\ZTREEWIN\%ZTW%" goto LOC2

:: First try opening as Admin
Powershell Start -FilePath {%ProgramFiles%\ZTREEWIN\%ZTW%} -ArgumentList {%* %ZTWCL% /TADMIN} -WorkingDirectory "%SystemDrive%\" -Verb Runas > nul
if "%errorlevel%"=="0" goto DONE

:: Try opening without Admin if user declines UAC prompt
start "ZTreeWin" /B /D"%SystemDrive%\" "%ProgramFiles%\ZTREEWIN\%ZTW%" %* %ZTWCL%
goto DONE

:LOC2

:: Look for Program Files(x86)\ZTreeWin folder

if "%ProgramFiles(x86)%"=="" goto LOC3
if not exist "%ProgramFiles(x86)%\ZTREEWIN\%ZTW%" goto LOC3

:: First try opening as Admin
Powershell Start -FilePath {%ProgramFiles(x86)%\ZTREEWIN\%ZTW%} -ArgumentList {%* %ZTWCL% /TADMIN} -WorkingDirectory "%SystemDrive%\" -Verb Runas > nul
if "%errorlevel%"=="0" goto DONE

:: Try opening without Admin if user declines UAC prompt
start "ZTreeWin" /B /D"%SystemDrive%\" "%ProgramFiles(x86)%\ZTREEWIN\%ZTW%" %* %ZTWCL%
goto DONE

:LOC3

:: Look for \ZTreeWin folder on any drive available

for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist %%d:\ZTreeWin\%ZTW% call :RUNIT %%d %* & goto DONE
goto LOCBAD

:RUNIT

:: First try opening as Admin
Powershell Start -FilePath {%1:\ZTREEWIN\%ZTW%} -ArgumentList {%* %ZTWCL% /TADMIN} -WorkingDirectory {%1:\} -Verb Runas > nul
if "%errorlevel%"=="0" goto :EOF

:: Try opening without Admin if user declines UAC prompt
start "ZTreeWin" /B /D"\" /b "%1:\ZTREEWIN\%ZTW%" %* %ZTWCL%
goto :EOF

:LOCBAD

echo.
echo Cannot locate ZTreeWin Folder.
echo.
pause
goto DONE

:DONE

:: Clean up and exit

endlocal

157 views      
 

Messages in this Thread

 
95,039 Postings in 11,980 Threads, 349 registered users, 41 users online (0 registered, 41 guests)
Index | Admin contact |   Forum Time: Jul 5, 2020 - 5:09 am EDT  |  Hits:31,944,583  (5,536 Today )
RSS Feed