ZTree.com  | ZEN  | About...  

 Index   Back

[General] ZTreeWin CMD Script (repaired).   [General]

By: Steven James       
Date: Oct 07,2016 at 21:43
In Response to: [General] ZTreeWin CMD Script (to be Used with Shortcut). (Steven James)

All,

This script I posted has been modified below with minor repairs. I doubt anyone who's interested in using this would expect to enter the ZTreeWin location manually more than once.

::===================================================================
:: $File: ZX.CMD ......... Original Post to ZTreeWin Forum
:: ....................... Oct 6,2016, 10:40 PST -S. James
::
:: 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 "_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 "_TMPATH="& Set "_PARAMS="& Set "_QUIT=FALSE"
Set /A $ErrN=-1& Set /A $MAX=75& Set "$Chr=_"
Set "$Ln="& Set /A $C=0

::___________________________________________________________
:: Set the user name (if any) and establish the name of the
:: text file to contain ZTreeWin's desired location. This
:: location may be modified using the /BK switch, which will
:: prompt for an alternate ZTreeWin location, then store it.
:: Temporarily store the Global environment (in the file
:: designated (XXX.TMP)...
::___________________________________________________________
::
Set "_ZUSER=STEVEN JAMES"
Set "_ZTSAV=_ZTSAV.TXT"
Set "_TMPATH=%PATH%"
Set> XXX.TMP
::___________________________________________________________
:: Make a separator using 'Call :MkSep'.
:: Gather the user arguments (e.g. /64 /? -D -BK and modify
:: to form: -64 -H -D -BK ...).
:: Filter all valid switches from the command-line and
:: assign script variables according to valid switches
:: using 'Call :SetVars'.
:: We actually need both the '_DEBUG' and '_HELP' flags early,
:: so they're treated a bit differently:
::
Set _PARAMS=%*& @Set _PARAMS=!_PARAMS:?=h!& @Set _PARAMS=!_PARAMS:/=-!
For %%G in ( %_PARAMS% ) Do (
If /I "%%G" EQU "-D" @Set "_DEBUG=TRUE"
If /I "%%G" EQU "-H" @Set "_HELP=TRUE" )
::
Call :MkSep
Call :GetParms "%_PARAMS%"
Call :SetVars "%_PARAMS%"
Call :EchoStatus

::___________________________________________________________
:: Process HELP alone.
:: Execute based on command-line switches...
:: (Functions 'SetPn' and 'ZPaths' assign script defaults.
:: They should be modified according to prference):
::
:Begin
::
@If "%_HELP%" EQU "TRUE" @Call :Hlp& @Goto :Exit
@Call :Prompt
@If "%_QUIT%" EQU "TRUE" @Goto :Exit
@Call :AdminOrUser
@If "%_POWER%" EQU "TRUE" @Call :Power
@If "%_REPAR%" EQU "TRUE" @Call :Repar& @Goto :Exit
@Call :ZPaths
@Call :SetPn
@Goto :CallApp

::===================================================================
:: FUNCTION Prompt
:: If the ZTreeWin location is invalid - or an alternative
:: location is preferred, prompt the user for it (this function is
:: invoked by the /BK command-line switch):
::
:Prompt
::
@If "%_BKUSE%" EQU "TRUE" @Goto :GetLoc
@If NOT EXIST %_ZTSAV% @Goto :GetLoc
@For /f "Tokens=* Delims=" %%G in ('Type %CD%\%_ZTSAV%') Do (
@If NOT Defined _ZTPATH Set "_ZTPATH=%%G" )
::
@If Exist !_ZTPATH!\%_ZTWEXE% @Goto :Eof

:GetLoc
@Set /P _ZTPATH="Valid ZTreeWin Location? [Enter=!_ZTPATH!, Q=Quit]: "
@If /I "!_ZTPATH!" EQU "Q" @Set "_QUIT=TRUE"& @Goto :Eof
If Exist !_ZTPATH!\%_ZTWEXE% @Echo !_ZTPATH!>%_ZTSAV%& @Goto :Eof
Echo.Invalid location.& @Goto :GetLoc
@Goto :Eof &:: Fail safe.

::===================================================================
:: 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 (originally intended as an echo toggle)
:: 'EchoStatus' is used to record the current Echo status, whether ON
:: or OFF.
::
:EchoStatus
::
Echo|Findstr -ic:"OFF" >Nul 2>&1
If %ERRORLEVEL% EQU 1 (Set "_ECHO=OFF") Else (Set "_ECHO=ON")
@Goto :Eof

::===================================================================
:: 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[New ZTW location], /D[ebug], /? or /H[elp],
:: /P[ower Settings], /R[epair], /64[ZTW64.EXE]:
::
:: Returns Valid Cmd-Line Switches in '_PARAMS':
::
:GetParms
::
@Cls
Set "_Args="
Set "_Valid=-BK -D -H -P -R -64"
Set "_Errs="
Set "_Dups="
Set "_User=%~1"
::
If "%_DEBUG%" EQU "TRUE" (
Echo %$Ln%
Echo User data: %_User%
Echo Valid data: %_Valid%
)
::
@For %%G in ( %_User% ) 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%
@Echo %$Ln%
@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
::
Set "_User=%~1"

:: '_DEBUG', '_HELP' processed at the outset:
:: @If /I %%G?==-d? @Set "_DEBUG=TRUE"
:: @If /I %%G?==-h? @Set "_HELP=TRUE"

@For %%G in ( %_User% ) Do (
@If /I %%G?==-bk? @Set "_BKUSE=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 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%" EQU "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=%_ZUSER%" )
)
@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=33333333-ffff-4444-9999-ffbbaabbaacc"
@Set "_PwrScheme8_Hrs=88888888-3333-4444-9999-ffbbaabbaacc"
@Set "_PwrSchemeHiPrf=88888888-eeee-4444-9999-ffbbaabbaacc"
@Set "_PwrSchemeSaver=aaaaaaaa-3333-4444-bbbb-ffbbaabbaacc"
::
@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 &:: ALL local script variables disappear.

:__END__


Steven James

1,371 views      
Thread locked
 

Messages in this Thread

 
96,664 Postings in 12,235 Threads, 350 registered users, 196 users online (0 registered, 196 guests)
Index | Admin contact |   Forum Time: Jun 16, 2024 - 3:47 pm UTC  |  Hits:64,854,160  (64,220 Today )
RSS Feed