使用FOR,如果保持简单,如何在行动之前检查一个单词的文件名?

时间:2017-07-24 20:41:36

标签: batch-file

这会使用@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { if(higlightdetect==0){//condition to get cursor position without selection selectionStart = noteview.getSelectionStart(); selectionEnd=selectionStart; } public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { Toast.makeText(getthenote.this, String.valueOf(selectionStart), Toast.LENGTH_SHORT).show(); Toast.makeText(getthenote.this, String.valueOf(selectionEnd), Toast.LENGTH_SHORT).show(); higlightdetect=0 //resetting higlightdetect } 变量搜索和计算视频集,其内容类似于%string%(其中S是赛季开头#/ episode#tag)。

A?Title?of?Show?S

我的问题是我想要计算不同的剧集,而不是相同的剧集。 剧集与剧集标签等某些相似之处的命名截然不同。我需要 set count=0 for /f "delims=" %%i in ('where ".:%string%*"') do set /a count+=1 命令来查找带点作为空格的剧集:

where

此列表应为系列The.Show.S01E01.Pilot.1080p.mp4 Another Show S10E19 Untitled.mkv Another_Show_S10E19.m4v 返回%count% 1。我有一个脚本Another Show来调用,将文件名转换为变量,如episode标记到getname.cmd变量,其内容类似于%episode%

希望这并不会使复杂性不成比例。

-----编辑:我的解决方案

我认为解决方案可能就是这样......

S01E01

然而这并不起作用,因为变量不会因为某种原因从getname.cmd转移。我可能使用了delayedexpansion错误..?

2 个答案:

答案 0 :(得分:0)

我刚刚发现当前命令失败时用于执行的||,这使得这太容易了。我无法相信我现在才知道这件事!

set count=1    
for /f "delims=" %%i in ('where ".:%string%*" 2^>nul') do echo "%%~ni" | find /i "%episode%" >nul || set /a count+=1

被授予,这不会检查现有的重复项,但仅限于此新文件是重复的。这应该足够好了,我只需要通过其他方式对重复进行单次扫描。

答案 1 :(得分:0)

这不是一项微不足道的任务,因为从文件名中提取季节/剧集部分S??E??并不太简单。我可能会用这样的脚本(请参阅rem注释):

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_ROOT=%~dp0."      & rem // (directory containing the files to process)
set "_NUMS=S??E??"      & rem // (pattern that matches season/episode number)
set _SEPS=" " "_" "."   & rem // (quoted list of all permitted separators)
set "_MASK=*%_NUMS%*.*" & rem // (mask that files to be processed must match)
set "_REGEX=%_NUMS:?=[0123456789]%"  & rem // (regular expression for season/episode)
set "_TEMP=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (path to temporary file)
rem set "_TEMP=%~dpn0.tmp"  & rem // (or a temporary file located next to batch file)
set "_NAME=Another Show"& rem // (name of show to determine count of episodes)
rem set /P _NAME="Title of show: "       & rem // (or name of show entered by user)

rem // Write list of `%_NUMS%` occurrences and related names to temporary file:
> "%_TEMP%" (
    rem // Loop through all matching files:
    for /F "delims= eol=|" %%F in ('
        dir /B /A:-D "%_MASK%"
    ') do (
        rem // Store original file name in variable:
        set "FILE=%%~nF "
        setlocal EnableDelayedExpansion
        rem // Replace all possible separators by spaces:
        for %%C in (!_SEPS!) do (
            set "FILE=!FILE:%%~C= !"
        )
        rem // Pass modified file names beyond `endlocal`:
        for /F "delims= eol=|" %%E in ("!FILE!") do (
            endlocal
            rem // Store modified file name in variable:
            set "FILE=%%E"
            rem // Initialise flag for finding first `%_NUMS%` occurrence:
            set "FLAG=#"
            setlocal EnableDelayedExpansion
            rem // Split modified file name by spaces and loop through parts:
            for %%I in ("!FILE: =" "!") do (
                rem // Check flag if no `%_NUMS%` occurrence has yet been found:
                if defined FLAG (
                    endlocal
                    rem // Store current part of file name:
                    set "ITEM=%%~I"
                    setlocal EnableDelayedExpansion
                    rem // Check part of file name against regular expression:
                    echo/"!ITEM!"| > nul findstr /R /X /C:^"\"%_REGEX%\"^" && (
                        rem // Match found, so extract file name before part:
                        for /F "delims=| eol=|" %%J in ("!FILE: %%~I =|!") do (
                            endlocal
                            set "NAME=%%J"
                            set "FLAG="
                            setlocal EnableDelayedExpansion
                        )
                        rem // Output `%_NUMS%` occurrence and file name before:
                        echo/!ITEM!^|!NAME!
                    )
                )
            )
        )
        endlocal
    )
)

rem // Clean up variables beginning with `#`:
for /F "delims==" %%K in ('set "#" 2^> nul') do set "%%K="
rem // Search temporary file for certain name:
for /F "tokens=1* delims=| eol=|" %%K in ('
    findstr /L /I /E /C:"|%_NAME%" "%_TEMP%"
') do (
    rem // Assign each found name to variable beginning with `#`:
    set "#%%K=%%L"
)
rem // Count number of variables beginning with `#`:
for /F %%C in ('set "#" ^| find /C /V ""') do set "COUNT=%%C"
rem // Return count:
echo/%COUNT%

rem // Clean up temporary file:
del "%_TEMP%"

endlocal
exit /B

此方法使用临时文件,该文件根据您的示例文件接收以下数据:

S01E01|The Show
S01E19|Another Show
S01E19|Another Show

然后在此临时文件中搜索以|结尾的行加上节目的给定名称(例如Another Show)。每个返回的行都以|字符分割;左侧部分,因此季节/剧集部分用作前置#的环境变量的名称,右侧部分作为相应的值。由于没有可能的重复环境变量,重复行会产生单个变量(例如#S01E19=Another Show)。以#开头的变量数最终计算,因此结果表示给定节目的单个剧集数。

实际上不需要临时文件,它可以在第一个循环结构中立即完成,当每个派生的部分文件名与给定的节目进行比较时,如下所示:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_ROOT=%~dp0."      & rem // (directory containing the files to process)
set "_NUMS=S??E??"      & rem // (pattern that matches season/episode number)
set _SEPS=" " "_" "."   & rem // (quoted list of all permitted separators)
set "_MASK=*%_NUMS%*.*" & rem // (mask that files to be processed must match)
set "_REGEX=%_NUMS:?=[0123456789]%"  & rem // (regular expression for season/episode)
set "_TEMP=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (path to temporary file)
rem set "_TEMP=%~dpn0.tmp"  & rem // (or a temporary file located next to batch file)
set "_NAME=Another Show"& rem // (name of show to determine count of episodes)
rem set /P _NAME="Title of show: "       & rem // (or name of show entered by user)

rem // Clean up variables beginning with `#`:
for /F "delims==" %%K in ('set "#" 2^> nul') do set "%%K="
rem // Loop through all matching files:
for /F "delims= eol=|" %%F in ('
    dir /B /A:-D "%_MASK%"
') do (
    rem // Store original file name in variable:
    set "FILE=%%~nF "
    setlocal EnableDelayedExpansion
    rem // Replace all possible separators by spaces:
    for %%C in (!_SEPS!) do (
        set "FILE=!FILE:%%~C= !"
    )
    rem // Pass modified file names beyond `endlocal`:
    for /F "delims= eol=|" %%E in ("!FILE!") do (
        endlocal
        rem // Store modified file name in variable:
        set "FILE=%%E"
        rem // Initialise flag for finding first `%_NUMS%` occurrence:
        set "FLAG=#"
        setlocal EnableDelayedExpansion
        rem // Split modified file name by spaces and loop through parts:
        for %%I in ("!FILE: =" "!") do (
            rem // Check flag if no `%_NUMS%` occurrence has yet been found:
            if defined FLAG (
                endlocal
                rem // Store current part of file name:
                set "ITEM=%%~I"
                setlocal EnableDelayedExpansion
                rem // Check part of file name against regular expression:
                echo/"!ITEM!"| > nul findstr /R /X /C:^"\"%_REGEX%\"^" && (
                    rem // Match found, so extract file name before part:
                    for /F "delims=| eol=|" %%J in ("!FILE: %%~I =|!") do (
                        endlocal
                        set "NAME=%%J"
                        set "FLAG="
                        rem // Check if partial file name matches given name:
                        if /I "%%J"=="%_NAME%" (
                            rem // Create variable beginning with `#`:
                            set "#%%~I=%%J"
                        )
                        setlocal EnableDelayedExpansion
                    )
                )
            )
        )
    )
    endlocal
)

rem // Count number of variables beginning with `#`:
for /F %%C in ('set "#" ^| find /C /V ""') do set "COUNT=%%C"
rem // Return count:
echo/%COUNT%

endlocal
exit /B

然而,这种方法的缺点是(与基于临时文件的方法相反,当您稍微修改脚本时,该文件允许轻松地将其重复用于多次搜索),您需要遍历所有匹配的文件并提取季节/ episode部分每次搜索一次。