我正在尝试从目录收集第一个文件,然后处理该文件。但是在第二次运行和处理批处理文件时,我无法将值存储在文件名的变量中。
下面是示例代码:
for /R C:\abcde_efghij\ab_abcabca %%i IN (*.*) DO (
set filename=%%i
set newname=%filename:~14%
set transname=%filename:~25%
goto tests
)
:tests
echo %filename%
echo %newname%
echo %transname%我确信我们必须使用名为SETLOCAL的东西,但是我无法在上面的代码中实现它。
任何帮助!
发布于 2016-06-27 08:26:12
您应该避免块内部的百分比扩展,对于块也是如此,因为在解析块时,扩展只发生一次。
for /R C:\abcde_efghij\ab_abcabca %%i IN (*.*) DO (
set filename=%%i
goto :tests # Get only the first file
)
exit /b
:tests
set newname=%filename:~14%
set transname=%filename:~25%
echo %filename%
echo %newname%
echo %transname%
exit /b正如@Stephan所指出的,您也可以在块内使用延迟扩展。
setlocal EnableDelayedExpansion
for /R C:\abcde_efghij\ab_abcabca %%i IN (*.*) DO (
set filename=%%i
set newname=!filename:~14!
set transname=!filename:~25!
goto :tests # Get only the first file
)https://stackoverflow.com/questions/38049021
复制相似问题