Last active
June 28, 2024 12:08
-
-
Save tetratorus/7f5819fe03f100884bfe262abef91c7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal enabledelayedexpansion | |
set "sourcePath=%1" | |
set "outputFile=CombinedCSVs.csv" | |
set "logFile=CombineCSVs_log.txt" | |
set "tempFile=temp.csv" | |
rem Clear existing log file | |
echo Combine CSV Files Log > "%logFile%" | |
echo Started at %date% %time% >> "%logFile%" | |
if not exist "%sourcePath%" ( | |
echo Error: Source directory does not exist. | |
echo Error: Source directory does not exist. >> "%logFile%" | |
goto :eof | |
) | |
echo Processing CSV files... | |
echo Processing CSV files... >> "%logFile%" | |
rem Count total number of CSV files | |
set /a totalFiles=0 | |
for /r "%sourcePath%" %%F in (*.csv) do set /a totalFiles+=1 | |
echo Found %totalFiles% CSV files to process. | |
echo Found %totalFiles% CSV files to process. >> "%logFile%" | |
rem Write headers from the first CSV file | |
set /a processedFiles=0 | |
for /r "%sourcePath%" %%F in (*.csv) do ( | |
set "firstFile=%%F" | |
goto :writeHeaders | |
) | |
:writeHeaders | |
if defined firstFile ( | |
echo Writing headers from %firstFile% | |
echo Writing headers from %firstFile% >> "%logFile%" | |
copy "!firstFile!" "%outputFile%" > nul | |
set /a processedFiles+=1 | |
) else ( | |
echo No CSV files found. | |
echo No CSV files found. >> "%logFile%" | |
goto :eof | |
) | |
rem Append data from all CSV files (skipping headers) | |
for /r "%sourcePath%" %%F in (*.csv) do ( | |
if not "%%F"=="!firstFile!" ( | |
echo Processing %%F | |
echo Processing %%F >> "%logFile%" | |
rem Use findstr to skip the header and append to temp file | |
findstr /v /b /c:"" "%%F" >> "%tempFile%" | |
set /a processedFiles+=1 | |
echo Processed !processedFiles! out of %totalFiles% files. | |
) | |
) | |
rem Append temp file to output file and delete temp file | |
type "%tempFile%" >> "%outputFile%" | |
del "%tempFile%" | |
echo All CSV files processed. | |
echo All CSV files processed. >> "%logFile%" | |
echo CSV files combined into %outputFile% | |
echo CSV files combined into %outputFile% >> "%logFile%" | |
echo Finished at %date% %time% >> "%logFile%" | |
echo Process completed. Check %logFile% for details. | |
echo Press any key to exit... | |
pause > nul |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment