batch:第二个for循环,dir名称中的空格不起作用

时间:2016-11-07 11:28:10

标签: batch-file for-loop spaces

我尝试使用两个for循环来完成一项简单的工作: 第一个for-loop给我带来了一个文件列表, 然后第二个for-loop应该把我所有文件的内容连接在一个临时文件中(后来ftp在某个地方,但那不是问题)。 所以这是我目前的代码:

setlocal enableDelayedExpansion

REM FILE-MERGER
if exist "%temp%\ZS_aus_Files.csv" del /f /q "%temp%\ZS_aus_Files.csv"
for /f "delims=" %%x in ('dir /s /b /a-d C:\Documents\accounting\') do (
        echo %%x
        for /f tokens^=*^ delims^=^ eol^= %%f IN (%%x) DO echo %%f >> %temp%\ZS_aus_Files.csv
)

%% x到目前为止还有空格的完整路径。 在第二个for循环中(%% x)因空格而抛出错误。

我的代码输出类似于:

C:\Documents\accounting\file with spaces.csv
The file "C:\Documents\accounting\file" cannot be found.

将(%% x)放入qoutes(" %% x")会使输出成为文件列表而不是合并的内容文件。

我真的被困在这里,希望有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

尝试

if exist "%temp%\ZS_aus_Files.csv" del /f /q "%temp%\ZS_aus_Files.csv"
for /f "delims=" %%x in ('dir /s /b /a-d C:\Documents\accounting\') do (
        echo %%x
        for /f usebackq^ tokens^=*^ delims^=^ eol^= %%f IN ("%%x") DO echo %%f >> %temp%\ZS_aus_Files.csv
)

MS根本没有提供帮助,但是当usebackq与双引号一起使用时,它可以用于文件名并处理带空格的文件名。