通过拖放处​​理文件和文件夹,过滤文件类型

时间:2018-05-07 20:33:41

标签: batch-file

我正在尝试制作一个可以采用以下三种输入中的任何一种的批处理脚本:

- 拖放文件夹
-drag并删除单个文件
-drag并删除多个文件的选择

并在所有呈现的文件类型列表中执行cmd命令(使用VLC转码某些视频)。我已经尝试过多种不同的方式,但每种方式似乎都有一种显示出来的缺点。

我可以让脚本检查是否安装了VLC,确定是否已经为其提供了文件,文件或文件夹,并且在没有过滤扩展的情况下处理单个文件就好了;它正在处理多个文件,处理文件夹,并通过扩展程序进行过滤,这些文件正在绊倒我:

@echo off
Title PPT Transcoder
Mode con cols=120 lines=16
IF [%~1] == "" GoTo :Error_ranDirectly
IF exist "C:\Program Files\VideoLAN\VLC\vlc.exe" (
    CD /D "%~1">nul 2>&1 && Goto :Explorer_Folder || Goto :Files
) else (
    Goto :Error_VLCNotInstalled
)
Exit /b
::**********************************************************
:Files <File>
Color 0E
ECHO    Transcoding %1 to %1_PPT.mp4... 

REM I really want to filter for six filetypes here (.mp4, .avi, .mov., .wmv,
REM .flv, .gif) and not affect any others. Forfiles looks like it could 
REM work, but I can't figure out how to get it to work on my arguments 
REM rather than the entire folder of files that match the filter.

"C:\Program Files\VideoLAN\VLC\vlc.exe" [a bunch of flags] %1 [a bunch more flags]:file{dst=%1_PPT.mp4,no-overwrite} [even more flags] vlc://quit
IF [%~2] == "" Goto :FilesFinished
shift 
GoTo :Files
:FilesFinished
Color 0A
Echo Finished processing.
Timeout /T 10 /NoBreak >nul
Exit /b

::**********************************************************
:Explorer_Folder <Folder>
REM This block of code almost works, but i think it malforms the file names 
REM fed to vlc.  It runs and thinks it's working, but fails to actually 
REM accomplish anything.

setlocal
Color 0C
SET /P AREYOUSURE=Transcode all files in folder %1 (Y/[N])?
IF /I "%AREYOUSURE%" NEQ "Y" GOTO :Abort
Color 0F
for %%x in (%1\*.mp4 %1\*.mov %1\*.avi %1\*.wmv %1\*.flv) do (
echo Transcoding %%x to %%x_PPT.mp4... 
""C:\Program Files\VideoLAN\VLC\vlc.exe" [a bunch of flags] %%x [a bunch more flags]:file{dst=%%x_PPT.mp4,no-overwrite} [even more flags] vlc://quit
echo Done.)
Timeout /T 10 /NoBreak >nul
endlocal
Exit /b
::**********************************************************
:Error_RanDirectly
Color 0C & echo(
ECHO    Drag and drop video files on the icon to transcode them. 
Timeout /T 5 /NoBreak >nul
Exit /b
::**********************************************************
:Error_VLCNotInstalled
Color 0C & echo(
ECHO    VLC 64-bit (free download from videolan.org) must be installed.
Timeout /T 10 /NoBreak >nul
Exit /b
::**********************************************************
:Abort
Exit /b

我想这是一个普遍的问题:是否有一个普遍接受的框架,使批处理脚本能够智能地拖放执行?

1 个答案:

答案 0 :(得分:0)

将其另存为cmd文件并在其上删除目录和文件。看看它是如何工作的。

@setlocal ENABLEEXTENSIONS
@rem @set prompt=$G

@call :ShowTypes %*
@pause
@exit /b

:ShowTypes
@echo Processing:
:_showTypes
@rem If it exists with a slash appended to the end, 
@rem it's directory, otherwise a file type.
@if exist "%1\\" (@set _type=directory) else (@call :SetFileType "%1")
@if "%1" neq "" (echo %1 as %_type%) else (exit /b 0)
@shift
@goto :_showTypes

:SetFileType
@rem %~x1 is the extension of the first parameter passed by caller.
@set _type=%~x1
@rem Remove the dot from the extension string.
@set _type=%_type:.=%
@rem Now _type is whatever the extension was.
@exit /b 0

删除前导@符号以查看该行。