【问题标题】:Processing file(s) and folders via drag and drop, filtering file types通过拖放处​​理文件和文件夹,过滤文件类型
【发布时间】:2018-05-07 20:33:41
【问题描述】:

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

-拖放文件夹
-拖放单个文件
- 拖放多个文件的选择

并对所有显示的与文件类型列表匹配的文件执行 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

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

【问题讨论】:

  • 这永远不会是真的IF [%~1] == "" 使用if "%~1"==""
  • 谢谢!对我的主要问题有任何见解吗?
  • 线索:如果 somefile.ext 不是目录,`if exists somefile.ext\` 总是会失败。换句话说,您只需处理传递给脚本的所有参数字符串,以确定它是否是目录字符串,方法是在其上附加一个反斜杠并检查它是否存在。
  • @jwdonahue @if exist "C:\Program Files\VideoLAN\VLC\vlc.exe" echo it exists! 似乎工作正常。我只使用存在来检查是否安装了 VLC。我的实际问题是 a)如何在处理单个文件时对文件类型进行二次检查,b)如何处理一堆参数以便拖动多个文件,以及 c)如何处理文件夹,过滤文件类型
  • 按文件类型,你是指它的扩展名吗?

标签: batch-file


【解决方案1】:

将其保存为 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

删除前导 @ 符号以查看该行的实际效果。

【讨论】:

  • CD /D "%~1"&gt;nul 2&gt;&amp;1 &amp;&amp; Goto :Folder || Goto :File 用于区分文件和目录,但该代码有助于理解字符串、变量和路径名如何在批处理脚本中交互。
  • 换目录的问题是,当字符串包含有效路径时,换目录会成功。
猜你喜欢
  • 1970-01-01
  • 2017-05-26
  • 1970-01-01
  • 1970-01-01
  • 2019-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
相关资源
最近更新 更多