0

I need to write a script in batch to do the following:

  1. For each csv in the folder check the first character (first character of the first line)
  2. If it is "*" then add a first row from another txt file ("header.txt")

I have written the following code in batch:

@echo off
pushd %~dp0
for %%F  in (*.csv) do (
    for %%a in ("%%F") do set "var=%%a"&goto :stop
    stop:
    set var=!var:~0,1!
    if %var% == "*" (
        type "header.txt" >> "Temp.csv"
        type "%%F" >> "Temp.csv"
        del "%%F"
        ren "Temp.csv" "%%F")
)

This code without "if condition" works:

@echo off
pushd %~dp0
for %%F  in (*.csv) do (
        type "header.txt" >> "Temp.csv"
        type "%%F" >> "Temp.csv"
        del "%%F"
        ren "Temp.csv" "%%F")
)
olo
  • 13
  • 3
  • 1
    An answer with multiple lines cannot be posted anymore. So here is a single command line solution for usage in a batch file: `@setlocal EnableExtensions EnableDelayedExpansion & pushd "%~dp0" && (for %%I in (*.csv) do @(set /P FirstLine=nul && move /Y "Temp.csv" "%%I" >nul) & popd) & endlocal`. This code works only on no `.csv` file containing `!` in its file name. See [single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564) to split it up to multiple lines. – Mofi Sep 15 '21 at 17:17
  • Awesome! That works! Thank you! – olo Sep 16 '21 at 08:51

0 Answers0