Search code examples
batch-filefilenamesspaces

Modify multiple files with spaces in the filenames in batch


I have a directory c:\test that is populated automatically with files named PM1 2016-10-06 1.AL1, PM1 2016-10-06 2.AL1, PM1 2016-10-07 1.AL1 etc. I have written (thanks to Aacini for the core code!) a batch script that works exactly the way I need it to, but my problem is that it only works on one file, and the file cannot have spaces in the filename. The code:

@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  

If you're curious about the TYPE line, it changes all the CRs in the file to CRLFs.


Solution

  • @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