ZTree.com  | ZEN  | About...  

 Index   Back

[General] ZTreeWin CMD Script (to be Used with Shortcut).   [General]

By: Steven James       
Date: Oct 07,2016 at 05:40

All,

I'd been working on a script to invoke ZTreeWin. What is nice about this script is that a shortcut to it may be created which contains various switches to customize ZTW invocation per user preference. Since I thought others might be able to modify and use this for their own purposes, I decided to post it here. Hopefully, it will be more legible than the last one I'd posted for automated backup (using Robocopy). By selecting the 'Code' option, this will presumably appear more legible to others. (My previous attempt w/o the 'Code' option removed all backslash characters, probably interpreted as escape codes...

::===================================================================
:: $File: Z.CMD
::
:: Script used to selectively invoke ZTreeWin (either x32 or x64)
:: with various user-defined command-line switches. Refer to HELP.
::

:: Limit scope of Script variables to this instance, enable
:: delayed expansion of script variables:
::
@Echo OFF
SetLocal &:: Local Environment Variables Only
SetLocal EnableDelayedExpansion &:: Delay Var Expansion

:: Initialize the flags to accompany the allowable command-
:: line switches. Superflous switches are ignored (refer
:: to Function 'GetParms')...
::
Set "_NUSER=STEVEN JAMES"
Set "_DEBUG=FALSE"& Set "_HELP=FALSE"& Set "_ZTWEXE=ZTW.EXE"
Set "_BKUSE=FALSE"& Set "_REPAR=FALSE"& Set "_ZTFIND=FALSE"
Set "_POWER=FALSE"& Set "_Echo=OFF"& Set "_E=F"
Set "_ZTDRIVE="& Set "_BKDRIVE="& Set "_ZTPATH="
Set "_BKPATH="& Set "_TMPATH="& Set "_PARAMS="
Set /A $ErrN=-1& Set /A $MAX=75& Set "$Chr=_"& Set "$Ln="& Set /A $C=0

:: The following code establishes the main ZTreeWin AND
:: backup locations. Then, ...
:: 1) Temporarily store the Global environment (in XXX.TMP),
:: 2) Modify user arguments (e.g. /zy or -x) to the form:
:: -zy -x...
:: 3) Filter all valid switches from the command-line,
:: 4) Assign script variables according to valid switches
:: using 'Call :SetVars':
:: NOTE: This script was designed to run from any valid
:: drive specification that contains a "ZTreeWin"
:: directory. If it does not, the user is prompted for a
:: legitimate pathspec location to use.
::___________________________________________________________
::
::t "_ZTPATH=D:\ZTreeWin"
Set > XXX.TMP
Set "_ZTDRIVE=%CD:~0,2%"
Set "_BKDRIVE=H:"
Set "_ZTPATH=%_ZTDRIVE%\ZTreeWin"
Set "_BKPATH=%_BKDRIVE%\ZTreeWin"
Set "_TMPATH=%PATH%"
Set _PARAMS=%*& @Set _PARAMS=!_PARAMS:?=h!& @Set _PARAMS=!_PARAMS:/=-!
::
Call :MkSep
Call :GetParms %_PARAMS%
Call :SetVars %_PARAMS%
Call :EchoStatus

:: Make initializing calls, then verify that the ZTreeWin location
:: is valid. If not, we'll prompt the user for their preference for
:: application startup before continuing (we're either checking to
:: see whether the x32 or x64 versions exist (depends on /64 switch):
::
:Begin
::
@If "%_HELP%" EQU "TRUE" @Call :Hlp& @Goto :Exit
@If "%_BKUSE%" EQU "TRUE" @Call :BkUse
@Call :AdminOrUser
@If "%_POWER%" EQU "TRUE" @Call :Power
@If "%_REPAR%" EQU "TRUE" @Call :Repar& @Goto :Exit
@If NOT Exist %_ZTPATH%\%_ZTWEXE% @Call :Prompt
@Call :ZPaths
@Call :SetPn
@Goto :CallApp

:: If the ZTreeWin location is invalid, prompt the user:
:: 1) [Enter] uses the current pathspec {d:}\ZTreeWin", ..
:: 2) [Q] ... terminates, or
:: 3) ....... we'll attempt to use the user-designated alternative:
::
:Prompt
::
@Set /P _ZTPATH="Valid ZTreeWin Location? [Enter=!_ZTPATH!, Q=Quit]: "
@If /I "!_ZTPATH!" EQU "Q" @Goto :Exit
@If Exist !_ZTPATH!\%_ZTWEXE% @Goto :Eof
@Echo.Invalid location.& @Goto :Prompt
@Goto :Eof

::===================================================================
:: FUNCTION MkSep
:: 'MkSep' creates a line separator '$MAX' characters long, using
:: the specified '$Chr' character as the line type.
::
:: Return: The global variable '$Ln' is set to the line type with
:: the specified '$MAX' length.
::
:MkSep
If %$C% LSS %$MAX% (Set /A $C+=1& Set $Ln=!$Ln!!$Chr!& Goto :MkSep)
Goto :Eof

::===================================================================
:: FUNCTION EchoStatus
:: 'EchoStatus' is used to record the current Echo status, whether ON
:: or OFF. The status is maintained by the status of the '_Echo'
:: variable. Echo|Findstr -ic:"ON" Sets ErrorLevel=0 (if found),
:: or Sets ErrorLevel=1 (if Not "):
::
:EchoStatus
::
Echo|Findstr -ic:"OFF" >Nul 2>&1
If %ERRORLEVEL% EQU 1 (Set "_Echo=OFF") Else (Set "_Echo=ON")
@Goto :Eof

::===================================================================
:: FUNCTION BkUse
:: 'BkUse' is invoked when the user specifies /BK on the command-
:: line. This function is used to prompt the user for an alternate
:: ZTreeWin location (the default in this script is H:\ZTreeWin).
::
:BkUse
::
@Set "_ZTPATH=%_BKPATH%"
@Set /P _ZTPATH="Valid ZTreeWin Location? [Enter=!_ZTPATH!, Q=Quit]: "
@If /I "!_ZTPATH!" EQU "Q" @Goto :Exit
@If Exist !_ZTPATH!\%_ZTWEXE% @Goto :Eof
@Echo.Invalid location. & @Goto :BkUse

::===================================================================
:: FUNCTION GetParms
:: This function is used to retrieve valid command-line switches.
:: Anything other than valid switches is ignored.
::
:: Current valid command-line switches include (this may be easily
:: enhanced):
:: /BK[BacKup ZTW location], /D[ebug], /? or /H[elp],
:: /P[ower Settings], /R[epair],/64[ZTW64.EXE]:
::
:: Returns Valid Cmd-Line Switches in '_PARAMS':
::
:GetParms
::
@Set "_Args="
@Set "_Valid=-bk -d -h -p -r -64"
@Set "_Errs="
@Set "_Dups="

@If "%_DEBUG%" EQU "TRUE" (
@Echo User data: %_PARAMS%
@Echo Valid data: %_Valid%
)
@For %%G in ( %1 ) Do (
@Echo."!_Args!"|Findstr -ic:"%%G" >NUL 2>&1
@If !ERRORLEVEL! EQU 1 (
@Echo."!_Valid!"|Findstr -ic:"%%G" >NUL 2>&1
@If !ERRORLEVEL! EQU 0 (@Set _Args=!_Args!%%G ) Else (
@Set _Errs=!_Errs!%%G )
) Else ( @Set _Dups=!_Dups!%%G )
)
@If "%_DEBUG%" EQU "TRUE" (
@If Defined _Args @Echo Valid Args: %_Args%
@If Defined _Dups @Echo Duplicates: %_Dups%
@If Defined _Errs @Echo Invalid: %_Errs%
@Pause
)
@Goto :Eof

::===================================================================
:: FUNCTION SetVars
:: 'SetVars' sets the internal script flags to their respective
:: values according to the command-line arguments provided:
::
:: /BK[BacKup ZTW location], /D[ebug], /? or /H[elp],
:: /P[wr Settings], /R[epair], /64[ZTW64.EXE]:
::
:SetVars
::
@For %%G in (!_PARAMS!) Do (
@If /I %%G?==-bk? @Set "_BKUSE=TRUE"
@If /I %%G?==-d? @Set "_DEBUG=TRUE"
@If /I %%G?==-h? @Set "_HELP=TRUE"
@If /I %%G?==-p? @Set "_POWER=TRUE"
@If /I %%G?==-r? @Set "_REPAR=TRUE"
@If /I %%G?==-64? @Set "_ZTWEXE=ZTW64.EXE"
)
::
@If "%_DEBUG%" EQU "TRUE" (
@Cls
@Echo.%$Ln%
@Echo CURRENT Flags and Switches:
@Echo.
@Set _
@Echo.%$Ln%
@Pause
)
@Goto :Eof

::===================================================================
:: FUNCTION ZPaths
:: This function is used to set the ZTreeWin Support Paths. That is,
:: it establishes the paths to be traversed that include the default
:: ZTreeWin archiver(s), viewers, file compare utilities and so forth.
:: This can be easily extended according to user preference:
::
:ZPaths
::
@Set "_Z7PATH=%_ZTPATH%\7Zip"
@Set "_PKPATH=%_ZTPATH%\PK250"
@Set "_PGPATH=%_ZTPATH%\PGV"
@Set "_RPPATH=%_ZTPATH%\rpVT"
@Set "_TCPATH=%_ZTPATH%\TFC"
@Set _EVPATH="%PATH:;=" "%"
@Set "_ZPATHS=%_ZTPATH%;%_Z7PATH%;%_PKPATH%;%_PGPATH%;%_RPPATH%;%_TCPATH%"
@If "%_DEBUG%" NEQ "TRUE" @Goto :ParsePaths

::===================================================================
:: DEBUG Paths specified:
::
@Cls
@Echo.%$Ln%
@Echo CURRENT PATHS:
@Echo.
@For %%F in ( %_EVPATH% ) Do @Echo.%%~F
@Echo.
@Echo _Z7PATH=%_Z7PATH%
@Echo _PKPATH=%_PKPATH%
@Echo _PGPATH=%_PGPATH%
@Echo _RPPATH=%_RPPATH%
@Echo _TCPATH=%_TCPATH%
@Echo _ZPATHS=%_ZPATHS%
@Echo.%$Ln%
@Pause

::===================================================================
:: Parse the PATH environment variable for an existing ZTreeWin
:: path so as not to needlessly replicate it:
::
:ParsePaths
::
@For %%F in ( %_EVPATH% ) Do (
@If /I "%%~F" EQU "%_ZTPATH%" @Set "_ZTFIND=TRUE"& @Goto :Brk1)
:Brk1

::===================================================================
:: Prefix the script variable %_ZPATHS% (established previously) to
:: the existing %PATH% Global environment variable. This will result
:: in ZTreeWin paths taking precedence over the other system paths.
::
:: NOTE: If this is not what is intended, it may be removed or
:: commented out:
::
@If %_ZTFIND%?==FALSE? @PATH %_ZPATHS%;%_TMPATH%
@If "%_DEBUG%" NEQ "TRUE" @Goto :Eof

::===================================================================
:: DEBUG ONLY: Display new paths specified above:
::
@Set _QUOTEDPATHS="%PATH:;=" "%"
@Cls
@Echo.%$Ln%
@Echo NEW PATHs (Includes ZTreeWin Dirs):
@Echo.
@For %%F in ( %_QUOTEDPATHS% ) Do @Echo.%%~F
@Echo.%$Ln%
@Pause
@Goto :Eof

::===================================================================
:: FUNCTION AdminOrUSer
:: Trivial test used to establish Administrative or User-access
:: privileges:
::
:AdminOrUser
::
@AT > NUL 2>&1
@If %ERRORLEVEL% EQU 0 (
@Set "_UserPriv=ADMINISTRATOR") ELSE (
@Set "_UserPriv=%_NUSER%" )
)
@If "%_DEBUG%" EQU "TRUE" (@Echo _UserPriv=%_UserPriv%)
@Goto :Eof

::===================================================================
:: FUNCTION Power
:: This block of code is used to make temporary Power Scheme
:: adjustments (*=Active). This may be necessary when ZTreeWin
:: is working on time-consuming tasks and it's undesirable for
:: the computer to be place in either sleep, hibernate or shutdown
:: modes. Function 'Power' preserves the original power scheme
:: and resets it back to its original state at the end of the
:: script.
:: NOTE: The Power Scheme GUIDs here are symbolic; each machine
:: will contain its own settings derived from:
:: 'PowerCfg -List' ...
::
:Power
::
@Set "_PwrSchemeBalan=12345678-fedc-1234-9976-fegbbzzzzzza"
@Set "_PwrScheme8_Hrs=67890123-3210-4567-9012-f0011aa22bbb"
@Set "_PwrSchemeHiPrf=abc12345-e8bf-4680-9abc-abcdef321012"
@Set "_PwrSchemeSaver=ace22222-3456-4321-bc01-fghij7654321"
::
@For /F "tokens=2,3 delims=:()" %%A in ('PowerCfg -GetActiveScheme') Do @If "%%B" NEQ "" (
@Set "_PwrSchemeOrig=%%A"
@Set "_PwrSchemeOrigID=%%B"
)
@If "%_DEBUG%" EQU "TRUE" @Echo "_PwrSchemeOrig=%_PwrSchemeOrig% (%_PwrSchemeOrigID%)"
@PowerCfg -SetActive %_PwrScheme8_Hrs%
@If "%_DEBUG%" EQU "TRUE" @Pause
@Goto :Eof

::===================================================================
:: FUNCTION Repar
:: This function is designed to delete the two files:
:: 1) Zbar.DAT and
:: 2) Temp.ZLOG ..... if ZTreeWin became unresponsive or
:: ............... crashed during a previous session. This
:: function is used as "maintenance only" and terminates after file
:: deletion (there may be a better way to handle this!):
::
:Repar
::
@If "%_DEBUG%" EQU "TRUE" (
@Echo.
@If NOT Exist %_ZTPATH%\zbar.dat @Echo MISSING: %_ZTPATH%\zbar.dat
@If NOT Exist %_ZTPATH%\TEMP.ZLOG @Echo MISSING: %_ZTPATH%\TEMP.ZLOG
@Echo.
)
@If Exist %_ZTPATH%\zbar.dat @Del %_ZTPATH%\zbar.dat > NUL 2>&1
@If Exist %_ZTPATH%\TEMP.ZLOG @Del %_ZTPATH%\TEMP.ZLOG > NUL 2>&1
@If "%_DEBUG%" EQU "TRUE" @Pause
@Goto :Eof

::===================================================================
:: FUNCTION SetPn
:: 'SetPn' is used to establish the command-line executable (ZTW.EXE,
:: ZTW64.EXE) based on the /64 switch. Other, optional switches are
:: also assigned here. This is obviously based upon user preference.
::
:: NOTE: The switch options here have been place on their own
:: respective lines for clarity (someone with MANY switches
:: would obviously string them together):
::
:SetPn
::
@Set _P0="%_ZTPATH%\%_ZTWEXE%"
@Set "_P1=/ZB"
@Set "_P2=/ZD"
@Set "_P3=/ZF"
@Set "_P4=/ZM"
@Set _P5=/T"%_UserPriv%: %_ZTWEXE%"
@Set "_P6=/COLS:MAX"
@Set "_P7=/ROWS:MAX"
@Set _P8=/APIT
@Set _P9=
::
@If "%_DEBUG%" EQU "FALSE" @Goto :Eof
@Echo.%$Ln%
@Echo SCRIPT Parameters:
@Echo.
@For /F %%G in ('Set _') Do (
@Echo %%G|Findstr -irc:"_P[0-9]"
@If %ERRORLEVEL% EQU 1 (Echo %%G)
)
@Echo.%$Ln%
@Pause

::===================================================================
:: DEBUG ONLY: Miscellaneous output for inspection...
:: (variables beginning with '_'):
@Cls
@Echo."Set _"|Findstr -ic:"not found" >NUL 2>&1
@If %ERRORLEVEL% EQU 1 @Goto :Skip1
@Echo.%$Ln%
@Set _
@Echo.%$Ln%
@Pause

::===================================================================
:: DEBUG ONLY: ZTreeWin-Specific Environment Variables (if any):
::
:Skip1
::
@Cls
@Echo."Set #"|Findstr -ic:"not found" >NUL 2>&1
@If %ERRORLEVEL% EQU 1 @Goto :Eof
@Echo.%$Ln%
@Set #
@Echo.%$Ln%
@Pause
@Goto :Eof

::===================================================================
:: EXECUTION:
::
:CallApp
::
@If "%_DEBUG%" EQU "FALSE" @Goto :CallZ

::===================================================================
:: Confirm Application Execution:
::
@Echo.%$Ln%
@Echo Application Execution Will Proceed As:
@Echo %_P0% %_P1% %_P2% %_P3% %_P4% %_P5% %_P6% %_P7% %_P8% %_P9%
@Echo.%$Ln%
::
@Choice /C YNQ /N /M "Is This Correct [Y/N/Q]? "
@If %ErrorLevel%?==3? @Goto :Exit &:: 'Q' Selected.
@If %ErrorLevel%?==2? @Goto :Rstor &:: 'N' Selected.
@If %ErrorLevel%?==1? @Goto :CallZ &:: 'Y' Selected (Can Just Fall Through).

:CallZ
@%_P0% %_P1% %_P2% %_P3% %_P4% %_P5% %_P6% %_P7% %_P8% %_P9%

::===================================================================
:: Restore Original Power Settings,
::
:Rstor
::
@If "%_POWER%" EQU "FALSE" @Goto :Exit
@PowerCfg -SETACTIVE %_PwrSchemeOrig%
@Goto :Exit

::===================================================================
:: HELP MENU:
:: Display Help Menu and Exit (this is the only option with
:: command-line switches: /?, /h and none):
::
:Hlp
@Cls
@Echo Off
@Echo.%$Ln%
@Echo."%~dpnx0" Cmd-line Options:
@Echo.
@Echo./?,/H ........... Help
@Echo./BK ............. Specify Alternate/Backup ZTW[64].EXE PATH
@Echo./D .............. Turn Debugging ON
@Echo./P .............. Configure POWER Settings (Set to 8-Hrs)
@Echo./R .............. Repair (Deletes Temp.ZLOG, Zbar.DAT Files)
@Echo./64 ............. Launch ZTW64.EXE (vs. ZTW[32].EXE)
@Echo.%$Ln%
@Echo.
@Goto :Eof

:Exit
::
@If Exist XXX.TMP @Del XXX.TMP > NUL 2>&1
@EndLocal &:: Clear ALL Local Script Variables for Next Time.

:__END__


For those who might complain that all this is easily accomplished using a shortcut alone, I'd point out that power options, paths and other features may not be easily accomplished by a .LNK file. Modify all as you see fit.

Steven James

1,540 views      
Thread locked
 

Messages in this Thread

 
96,637 Postings in 12,231 Threads, 350 registered users, 80 users online (0 registered, 80 guests)
Index | Admin contact |   Forum Time: Mar 29, 2024 - 1:45 pm UTC  |  Hits:62,407,927  (22,713 Today )
RSS Feed