批量修改文件名中包含空格的多个文件

时间:2016-10-06 11:40:48

标签: batch-file filenames spaces

我有一个目录 c:\ test ,会自动填充名为 PM1的文件2016-10-06 1.AL1,PM1 2016-10-06 2.AL1,PM1 2016 -10-07 1.AL1 等我写过(感谢 Aacini 核心代码!)一个批处理脚本,它完全按照我需要的方式工作,但我的问题是它只适用于一个文件,文件中的文件名不能包含空格。代码:

@echo off

cd c:\test

setlocal EnableDelayedExpansion

set LF=^

%do not remove%
%these lines%

set "EOL=!LF!" & set "EOL2=!LF!"

for /f "tokens=*" %%a in (c:\test\PM1.AL1) do (
     if %%a equ PROP-SUMMARY set "EOL=!LF!"
     set /P "=%%a!EOL!" < NUL
     set "EOL0=!EOL!" & set "EOL=!EOL2!" & set "EOL2=!EOL0!"
     if %%a equ PROP-VALUES set "EOL=,"
  ) >>c:\test\test.tmp
TYPE c:\test\test.tmp | FIND "" /V > c:\test\PM1_new.AL1"

DEL c:\test\test.tmp  

如果您对TYPE行感到好奇,它会将文件中的所有CR更改为CRLF。

1 个答案:

答案 0 :(得分:1)

@echo off
setlocal EnableDelayedExpansion

cd c:\test

set LF=^
%do not remove%
%these lines%

for %%f in (*.*) do (

   set "EOL=!LF!" & set "EOL2=!LF!"

   (for /f "usebackq tokens=*" %%a in ("%%f") do (
      if %%a equ PROP-SUMMARY set "EOL=!LF!"
      set /P "=%%a!EOL!"
      set "EOL0=!EOL!" & set "EOL=!EOL2!" & set "EOL2=!EOL0!"
      if %%a equ PROP-VALUES set "EOL=,"
   )) < NUL > test.tmp

   TYPE test.tmp | FIND "" /V > "%%f"

)

DEL test.tmp