操纵FOR命令变量?

时间:2018-11-24 02:33:50

标签: batch-file for-loop variables

有效。

:: %%a is output of FOR loop in batch:  
set _string=%%a  
:: First two chars:  
set "_two=!_string:~0,2!"  

我想通过直接对%% a进行操作来获得两个字符,
即不创建中间变量_string。
可能?

1 个答案:

答案 0 :(得分:0)

可能没有创建中间变量,但没有向您的请求添加实际任务,解决方案可能不像下面的示例那样EZ,而使用了PowerShell

批处理文件示例:

@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2)"
@Pause

…并在命令提示符处:

For %A In ("ezrine") Do @PowerShell "('%A').SubString(0,2)"

您甚至还可以执行其他操作,例如强制输出大写字母:

@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2).ToUpper()"
@Pause
相关问题