如何在FOR/F(批处理文件)的分隔符中包含空格 [英] How do I include a space in the delimiter of a FOR /F (batch file)

查看:53
本文介绍了如何在FOR/F(批处理文件)的分隔符中包含空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下命令根据分隔符(在本例中为破折号或-)前面的文件名部分创建文件夹:

The following command creates folders based on the part of a filename before a delimiter (in this case, a dash, or -):

setlocal EnableExtensions DisableDelayedExpansion
set "SourceDir=C:\Users\T\Source"
set "DestDir=C:\Users\T\Dest"

for /F "eol=| delims=" %%A in ('dir /B /A-D-H "%SourceDir%\*-*.jpg" 2^>nul') do (
    for /F "eol=| tokens=1 delims=-" %%B in ("%%~nA") do (
        md "%DestDir%\%%B" 2>nul
        REM move /Y "%SourceDir%\%%A" "%DestDir%\%%B\"
    )
)

endlocal

具体地说,定界符命令在这里:

Specifically, the delimiter command is here:

delims =-

但是我需要在定界符中在破折号之前和之后都包含一个空格.我如何在 delims 中包含一个空格?

But I need to include a space in the delimiter, both before and after the dash. How would I include a space in delims?

推荐答案

如果将文件名分配给环境变量,则可以使用字符串替换来操纵该变量.在第一次替换中,您可以使用带字符串定界符的通配符来删除文件名的第一部分.这将为您提供文件名的结尾部分.现在您已经有了文件名的结尾部分,您可以转过来使用字符串定界符和文件名的结尾部分从原始文件名中删除.

If you assign the file name to an environmental variable you can then use string substitution to manipulate that variable. In the first substitution you can remove the first part of the file name by using a wildcard with the string delimiter. This will give you the ending portion of the file name. Now that you have the ending portion of the file name you can turn around and use the string delimiter and the ending portion of the file name to be removed from the original file name.

setlocal EnableExtensions DisableDelayedExpansion
set "SourceDir=C:\Users\T\Source"
set "DestDir=C:\Users\T\Dest"

for /F "eol=| delims=" %%A in ('dir /B /A-D-H "%SourceDir%\*-*.jpg" 2^>nul') do (
    set "string=%%~A"
    setlocal enabledelayedexpansion
    SET "end=!string:* - =!"
    FOR /F "delims=" %%G IN ("!end!") do set "begin=!string: - %%~G=!"
    md "%DestDir%\!begin!" 2>nul
    REM move /Y "%SourceDir%\%%A" "%DestDir%\!begin!\"
    endlocal
)

endlocal

这篇关于如何在FOR/F(批处理文件)的分隔符中包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆