【问题标题】:Extract a section from the filename of a Batch variable从批处理变量的文件名中提取一个部分
【发布时间】:2018-06-16 12:19:09
【问题描述】:

文件:Test 123 - Test 456 - Test 789.txt

我需要从批处理文件中传递的参数中提取第一部分。在这种情况下,它将是“Test 123”,但文件始终具有不同的名称。 “-”需要作为分隔符(空格+连字符)。

%~n1%1 扩展为仅文件名,但如何仅指定文件名的一部分?

编辑:感谢所有帮助,但只有 LotPings 的 PowerShell 解决方案可以按预期工作!其他人回应了一个空文件名。我不知道为什么,但我确定这与我的设置有关。

【问题讨论】:

  • 将其设置为变量并通过替换扩展它,或者根据需要通过分隔的循环运行它。如果您需要更具体的帮助,请提供minimal reproducible example 代码 sn-p,以便我们将响应放入您实际问题的上下文中。
  • 如果您遵循了我上次评论中的建议,并提供了您需要帮助的代码,那么答案可能会以适合您环境的格式提供。这完全是你的错,你不公平地因为这个原因而忽视那些正确的答案。在发布任何其他问题之前,请确保您使用tour 并理解How to Ask;谢谢。

标签: windows batch-file command-line parameter-passing


【解决方案1】:

另一种使用 PowerShell 的解决方案

@Echo off
For /f "delims=" %%A in ('
Powershell -NoP -C "('%~1' -Split ' - ')[0]"
') Do Set "NewName=%%A%~x1"
Set NewNAme

>  SO_50887843_2.cmd " -;Test 123 - Test 456 =! Test 789.txt"
NewName= -;Test 123.txt

通过字符串替换,您可以进行一些洗牌(使用不带引号的参数)

:: SO_50887843.cmd
@Echo off
Set "_Args=%*"
:: remove content up to first delimiter " - "
Set "_Rest=%_Args:* - =%"
:: remove " - " and Rest from Args
Call Set "_First=%%_Args: - %_Rest%=%%"
Set _

> SO_50887843.cmd Test 123 - Test 456 - Test 789.txt
_Args=Test 123 - Test 456 - Test 789.txt
_First=Test 123
_Rest=Test 456 - Test 789.txt

使用带引号的参数将第二行更改为:

Set "_Args=%~1"

【讨论】:

  • 谢谢,PowerShell 解决方案有效!只需要将%~1 替换为%~n1 并将%%A%~x1 替换为%%A
  • 嗯,我认为所有方法都有效 - 感谢您的反馈。
【解决方案2】:

……还有另一个:

@Echo Off
Set "filename=%~n1"
Set "newname=%filename: -="&:"%"
Echo "%newname%%~x1"
Pause
GoTo :EOF

【讨论】:

    【解决方案3】:

    此注释代码可用于此任务:

    @echo off
    if "%~1" == "" goto :EOF
    setlocal EnableExtensions DisableDelayedExpansion
    
    rem Get file name without extension and path assigned to an environment variable.
    set "FileName=%~n1"
    
    rem For file names starting with a dot and not having one more dot like .htaccess.
    if not defined FileName set "FileName=%~x1"
    
    rem Exit the batch file if passed argument is a folder path ending with a backslash.
    if not defined FileName goto EndBatch
    
    rem Replace each occurrence of space+hyphen+space and next also of just
    rem space+hyphen by a vertical bar in file name. A vertical bar is used
    rem because a file name cannot contain this character.
    set "FileName=%FileName: - =|%"
    set "FileName=%FileName: -=|%"
    
    rem Get first vertical bar delimited string assigned to the environment variable.
    for /F "eol=< delims=|" %%I in ("%FileName%") do set "FileName=%%I"
    
    echo First part of "%~nx1" is "%FileName%".
    
    rem Add here more commands using the environment variable FileName.
    
    :EndBatch
    endlocal
    

    此批处理文件必须用双引号括起来的文件名调用,因为文件名包含步数,例如:

    GetFirstFileNamePart.bat "Test 123 - Test 456 - Test 789.txt"
    

    这个批处理文件甚至可以用以下非常奇怪的文件名调用它:

    GetFirstFileNamePart.bat " - Test 123 -Test 456 != Test 789 & More.txt"
    

    这种情况下的输出是:

    First part of " - Test 123 -Test 456 != Test 789 & More.txt" is "Test 123".
    

    要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

    • echo /?
    • endlocal /?
    • for /?
    • goto /?
    • if /?
    • rem /?
    • set /?
    • setlocal /?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多