如何将批处理文件与 Total Commander GUI 选定的文件/目录集成?

How to integrate batch file with Total Commander GUI selected files / directories?

参考:

使用引用的批处理文件,我可以为我要备份的文件夹制作一个好的打包文件。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FolderToBackup=%1"

rem Get last modification date/time of the folder to backup
rem in region dependent format which is YYYY-MM-DD hh:mm.
for %%I in ("%FolderToBackup%") do set "FolderTimeStamp=%%~tI"

rem Get from this date/time string just the year, month
rem and day of month from the date without the hyphens.
set "FolderTimeStamp=%FolderTimeStamp:~0,4%%FolderTimeStamp:~5,2%%FolderTimeStamp:~8,2%"

rem Compress the folder to backup into a RAR archive file with
rem last modification date of folder used in archive file name.
"%Programw6432%\WinRAR\WinRAR.exe" a -ac -cfg- -dh -ep1 -ibck -m4 -oh -ol -os -ow -r -ts -y -- %FolderTimeStamp%_%FolderToBackup%.rar "%FolderToBackup%"

rem Restore the environment as set before usage of command SETLOCAL at top.
endlocal

我习惯用 Total Commander 管理我的文件。

我把批处理文件配置成TC按钮栏上的一个按钮,按钮设置参数:%s。当活动项目是 TC 中的 d:\doc\aatemp 文件夹时,我按下按钮,TC 调用批处理文件并将正确的文件夹名称传递给打包文件夹的批处理文件。

对于不止一个文件夹,我想按上面的方法做。

所以我用按钮设置 Parameters: %L 制作了另一个批处理文件。 TC 使用 %L 在临时文件的文件夹中创建一个列表文件,并将所选文件/文件夹的完整限定名称写入此列表文件,并使用此临时列表文件的全名调用批处理文件。

rem @echo off
rem Processing of %L or %WL
REM setlocal EnableExtensions DisableDelayedExpansion
setlocal enabledelayedexpansion
rem for /f "usebackq delims=" %%s in (`type %1`) do echo "%%s"
for /f "usebackq delims=" %%s in (`type %1`) do (
echo "%%s"
rem pause
rem

set FolderToBackup=%%s
echo !FolderToBackup!
REM pause

if "!FolderToBackup:~-1!"=="\" set "FolderToBackup=!FolderToBackup:~0,-1!"
echo !FolderToBackup!
pause

rem Get last modification date/time of the folder to backup
rem in region dependent format which is YYYY-MM-DD hh:mm.
for %%I in ("!FolderToBackup!") do set "FolderTimeStamp=%%~tI"
echo !FolderTimeStamp!
pause

rem Get from this date/time string just the year, month
rem and day of month from the date without the hyphens.
set "FolderTimeStamp=!FolderTimeStamp:~0,4!!FolderTimeStamp:~5,2!!FolderTimeStamp:~8,2!"
echo !FolderTimeStamp!
pause

rem Compress the folder to backup into a RAR archive file with
rem last modification date of folder used in archive file name.
rem "!Programw6432!\WinRAR\WinRAR.exe" a -ac -cfg- -dh -ep1 -ibck -m4 -oh -ol -os -ow -r -ts -y -- !FolderTimeStamp!_!FolderToBackup!.rar !FolderToBackup!
c:\Program Files\WinRAR\WinRAR.exe a -ac -cfg- -dh -ep1 -ibck -m4 -oh -ol -os -ow -r -ts -y -- !FolderTimeStamp!_!FolderToBackup!.rar !FolderToBackup!

)
rem Restore the environment as set before usage of command SETLOCAL at top.
endlocal

我可以使用命令 pause 在使用 WinRAR.exe.

的命令行之前看到我想要的输出

带有 WinRAR.exe 的命令行不能像我想要的那样处理列表文件。

这个扩展版本的批处理文件可以被 Total Commander 调用,例如,第一个参数是一个绝对的或者甚至是一个不以反斜杠结尾或以反斜杠结尾的相对文件夹路径,以及一个现有列表文件的名称与绝对或相对文件路径。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FolderToBackup="
if "%~1" == "" goto ErrorArg
set "FolderToBackup=%~f1"

rem Does the argument string not end with a backslash for a folder path?
if not "%FolderToBackup:~-1%" == "\" goto IsFolder

rem The argument string is a folder path.
rem Does the specified folder not exist?
if not exist "%FolderToBackup%" goto ErrorArg

rem The specified folder exists and is archived now on not being a root folder.
call :BackupFolder
goto EndBatch

:IsFolder
rem Does the argument without trailing backslash not specify a folder?
if not exist "%FolderToBackup%\" goto ExistsFile

rem The argument is a folder path.
rem This folder is archived now on not being a root folder.
call :BackupFolder
goto EndBatch

:ExistsFile
rem The argument string is definitely not a folder path.
rem Does the argument string not specify an existing file?
if not exist "%FolderToBackup%" goto ErrorArg

rem The argument string specifies a file which is hopefully a list file.
set "ListFile=%FolderToBackup%"
for /F "usebackq eol=| delims=" %%I in ("%ListFile%") do (
    set "FolderToBackup=%%~I"
    call :BackupFolder
)
goto EndBatch

:BackupFolder
rem Remove the backslash at end of folder path if there is one at all.
if "%FolderToBackup:~-1%" == "\" set "FolderToBackup=%FolderToBackup:~0,-1%"
rem Exit the subroutine if the folder path was root folder of a drive.
if "%FolderToBackup:~2%" == "" goto :EOF
rem Does the folder to backup not exist? This skips also files!
if not exist "%FolderToBackup%\" goto :EOF

for %%J in ("%FolderToBackup%") do set "FolderName=%%~nxJ" & set "FolderPath=%%~dpJ"

rem Get last modification date/time of the folder to backup
rem in region dependent format which is YYYY-MM-DD hh:mm.
for %%J in ("%FolderToBackup%") do set "FolderTimeStamp=%%~tJ"

rem Get from this date/time string just the year, month
rem and day of month from the date without the hyphens.
set "FolderTimeStamp=%FolderTimeStamp:~0,4%%FolderTimeStamp:~5,2%%FolderTimeStamp:~8,2%"

rem Compress the folder to backup into a RAR archive file with
rem last modification date of folder used in archive file name.
"C:\Program Files\WinRAR\WinRAR.exe" a -ac -cfg- -dh -ep1 -ibck -m4 -oh -ol -os -ow -r -ts -y -- "%FolderPath%%FolderTimeStamp%_%FolderName%.rar" "%FolderToBackup%"
goto :EOF

:ErrorArg
if defined FolderToBackup (echo %~f0 "%FolderToBackup%") else echo %~f0
echo/
echo Error: This batch file must be started with path of an existing folder
echo        or the name of a list file with one or more folder paths. It is
echo        not possible to use this batch file with root folder of a drive.
echo/
pause

:EndBatch
rem Restore the environment as set before usage of command SETLOCAL at top.
endlocal

请阅读 remark 以了解此批处理文件的说明。

在 Total Commander(简称:TC)中,只需使用工具栏中的一项,即可使用一个或多个文件夹路径调用此批处理文件。将具有完整路径的批处理文件指定为 Command,并将 %L 指定为 Parameters.

TC 执行批处理文件,并将文件夹路径作为第一个参数传递给工具栏中的此项拖放单个文件夹。

如果在活动窗格中有一个文件夹处于活动状态,或者在活动窗格中选择了一个或多个文件夹,则单击工具栏中的项目会导致执行批处理文件,TC 会临时创建具有任一活动文件夹的完整限定文件夹名称的列表文件文件夹或选定的文件夹,除了在活动窗格中没有选择任何内容并且活动是 ...