1

I am trying to run a batch file to concatenate all csv files with the pattern ContractEligibility_* in the working folder.They are all of the same format and i need to concatenate without the repeating header.

The code is as follows:

@echo off>Combined.csv
cls
setlocal enabledelayedexpansion

if exist C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv del C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv

dir /a-d /b C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\ContractEligibility_*.csv>C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt

cd C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\

for /f "tokens=*" %%A in (C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt) do (
    set /p header=<%%A
    echo !header!>Combined.csv
)

for /f "tokens=*" %%A in (C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt) do (
        more +1 %%A>>Combined.csv
   )

del dirfiles.txt

it concatenates fine but I am getting an "Echo is off " at the start of the file as the first line. I know that the first for loop (The one in bold) is responsible as it is passing null value in the variable header. All the files are in the set path. Can someone help me solve the problem. Placing a drop after the echo is not acceptable as the batch file is followed by an automated load which utilizes the created file combined.csv.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
kartikeya_aj
  • 25
  • 1
  • 6
  • In loop where you are doing this `set /p header=<%%A` you want to get only the first line of the first file in the list ? – npocmaka Mar 17 '16 at 11:04
  • yes, I have repating headers in all of the file lists. So first line of any file works – kartikeya_aj Mar 17 '16 at 11:24
  • Just a tip or word of advice. In debugging and testing your script until you get it finalized, I would remove any `@echo off` and related statements. Once you get the finalized code, add in the `@echo off`. – Leptonator Mar 17 '16 at 15:11

4 Answers4

1

The equivalent code below solve the problem, run faster, and looks cleaner...

@echo off
setlocal enabledelayedexpansion

cls

cd C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\

set "header="
(for %%A in (ContractEligibility_*.csv) do (

   if not defined header (
      set /p header=<%%A
      echo !header!
   )

   more +1 %%A

)) > Combined.csv
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • this code is still adding header of all files where the first file ends and the second begins i.e 00032116,21238,39947PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14 [ Row : Header ],,,,,,,,, ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy – kartikeya_aj Mar 28 '16 at 07:19
  • This code do _the same task_ as your posted code... Perhaps if you post a small section of your files (5 lines from 3 files, for example) we both could test it with _the same data_ and have a common base to identify any error. Otherwise, describing what you get in your computer when I can't see it is pointless... Please, _edit the question_, do NOT post additional data in comments! – Aacini Mar 28 '16 at 15:19
0

Quick and dirty, just remove that line. Add commands like this at the end of your script.

findstr /v "Echo is off" Combined.csv > Combined2.csv

del Combined.csv

Community
  • 1
  • 1
lessthanideal
  • 1,084
  • 13
  • 14
  • that did cross my mind thank you for that.But was trying to understand why the Header variable is not getting assigned.But your method is effective. – kartikeya_aj Mar 17 '16 at 10:52
0
@echo off
break>Combined.csv
cls
setlocal enabledelayedexpansion

if exist C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv del C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv

dir /a-d /b C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\ContractEligibility_*.csv>C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt

cd C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\

for /f "tokens=*" %%A in (C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt) do (
    set /p header=<%%A
    if "!header!" neq "" (
        (echo(!header!)>Combined.csv
        goto :break_for
    )

)
:break_for

for /f "tokens=*" %%A in (C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt) do (
        more +1 %%A>>Combined.csv
   )

del dirfiles.txt

this will happen when the !header! is an empty line and there are no arguments passed to the echo command.You can workaround this with (echo(!header!)>Combined.csv . Don't worry about unbalanced parenthesis - it will not break the script.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • It does remove but it it adds the parenthesis to the first line which is what was mentioned with the drop('.'). As a automated code runs to consume the file afterwards the load fails as it cannot process the parenthesis.I was trying to find why the Set /p header=<%%A assignment was not working. leading to empty header variable. – kartikeya_aj Mar 17 '16 at 10:54
  • @npocmaka-it seems there is another issue the code is unable to remove the header from the second file so the created file Combined.csv is header recordsfile1 header recordsfile2. {[ Row : Header ],,,,,,,,, ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,Created‌​By,DateUpdated,UpdatedBy 00032116,21238,39947PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,201‌​6-02-29 10:46:14,2016-02-29 10:46:14 [ Row : Header ],,,,,,,,, ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,Created‌​By,DateUpdated,UpdatedBy} – kartikeya_aj Mar 28 '16 at 10:25
0

change

echo !header!>Combined.csv

to

echo(!header!>Combined.csv

if you can handle a newline at the start-of-file

OR to

if defined header echo !header!>Combined.csv
Magoo
  • 77,302
  • 8
  • 62
  • 84