Search code examples
batch-filecmdquote

Batch remove Remove quote in text file and first line of data


I am writing a code to accomplish a few things:

  1. Add date to the end of a text file
  2. Remove " from the text file
  3. Remove first line from text file (Header)

The test file looks like this:

Header
"Data Line 1
Data Line 2"

So far I have this which works for the first item:

```

@echo off


for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%

set stamp=%MM%%DD%%YYYY%
move "C:\test\*.csv" "C:\test\test_%stamp%.txt"

```

Solution

  • @echo off
    setlocal enabledelayedexpansion
    for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
    set YYYY=%dt:~0,4%
    set MM=%dt:~4,2%
    set DD=%dt:~6,2%
    set stamp=%MM%%DD%%YYYY%
    for %%a in ("c:\test\*.csv") do (
      (for /f "usebackq skip=1 delims=" %%b in ("%%a") do (
        set "line=%%b"
        echo !line:"=!
      ))>"C:\test\test_%%~na_%stamp%.txt"
    )
    

    Completely untested and may produce unwanted results depending on the exact file contents (which you didn't show us).