首页 > 解决方案 > CMD环境下PowerShell中的语法问题

问题描述

在 Windows 10 Enterprise LTSC 下,我有一个简单的 CMD ff.bat,其中包含:

powershell -Command (Measure-Command {ffmpeg.exe -hide_banner -i %* E:\%~n1.mkv}).ToString

在 CMD> 中ff test.mp4有效ff "test.mp4"但不是

ff "E:\Serie.(2009).8x04.episode.FR.LD.WEBRip.x264-LiBERTY.[server.org.ru].mkv"

鉴于 PowerShell 错误是:

Au caractère Ligne:1 : 60 + (Measure-Command {ffmpeg.exe -hide_banner -i Serie.(2009).8x04.episode.FR.LD.WEBRip.x264-LiBERTY.[server.org.ru].mkv . .. +
~ Nom de propriété manquant après l'opérateur de référence.(参考运算符后缺少属性)Au caractère Ligne:1 : 144 + ... .FR.LD.WEBRip.x264-LiBERTY.[server.org.ru ].mp4 E:\Serie ... + ~ Nom de propriété manquant après l'opérateur de référence. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingPropertyName

我猜这个问题来自.-[]():\文件名中的和括号和括号字符。请给我正确的语法以使其在 CMD 中工作。作为奖励,如果可以的话,你可以给我语法让它在 PS 中工作。请注意,我是 PowerShell 的新手,需要的示例多于建议。在此先感谢,马克。

标签: windowspowershellbatch-filecmdsyntax

解决方案


你的假设在这里...

我猜问题来自文件名中的 .-:\ 和括号和括号字符

...是正确的。

每个语言用例中都有特殊字符。它们只能按照语言规范中的定义使用。如果您使用的任何字符串具有这些类型的字符,您需要删除它们或正确终止它们。

<#
LONG DESCRIPTION
    Windows PowerShell supports a set of special character sequences that 
    are used to represent characters that are not part of the standard 
    character set. 

    The special characters in Windows PowerShell begin with the backtick 
    character, also known as the grave accent (ASCII 96). 

    The following special characters are recognized by Windows PowerShell: 

        `0    Null 
        `a    Alert 
        `b    Backspace 
        `f    Form feed 
        `n    New line 
        `r    Carriage return 
        `t    Horizontal tab 
        `v    Vertical tab 
        --%   Stop parsing
#>
Get-help -Name about_Special_Characters 

即使是圆括号、大括号、方括号,也有特殊含义,不能用在文件名中。

PowerShell - 特殊字符和标记

因此,根据您在该失败文件名中显示的内容,您需要重命名它们。


推荐阅读