首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过拖放和过滤文件类型处理文件和文件夹

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

Stack Overflow用户
提问于 2018-05-07 20:33:41
回答 1查看 264关注 0票数 0

我正在尝试创建一个批处理脚本,它可以接受以下三种输入中的任何一种:

-drag并删除文件夹

-drag并删除单个文件

-drag并删除多个文件的选择

并对所有与文件类型列表匹配的文件执行cmd命令(用VLC对一些视频进行转码)。我尝试过很多不同的方法,但每一种方法似乎都有这样或那样的缺点。

我可以让脚本检查是否安装了VLC,确定它是否获得了一个文件、文件或文件夹,并在没有过滤扩展名的情况下处理一个单独的文件;它正在处理多个文件,处理一个文件夹,并按扩展名进行筛选,这使我感到震惊:

代码语言:javascript
运行
复制
@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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-10 03:24:52

将其保存为cmd文件,并将目录和文件拖放到其中。看看它是怎么工作的。

代码语言:javascript
运行
复制
@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

移除前面的@符号,以查看该行的运行情况。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50222020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档