I have multiple *Extract.txt files located in multiple subfolders that I want to upload to a common table in a SQL server. I figure the best way to begin is to combine the data into one merged txt file, and then upload that to a database. The merged file should prepend the file name to each row of data. Each file has the same headers in the first row, which could be skipped in the merged file, or I could remove duplicate rows in SQL.
I have used the below code which works for *extract.txt files co-located with the batch file, but it does not cycle through subfolders. I have tried /R and other "For" qualifiers but the output is empty.
@echo off
setlocal EnableDelayedExpansion
set "file="
(for /f "tokens=1* delims=:" %%a in ('findstr "^" *extract.txt') do (
if "%%a" neq "!file!" (
set "file=%%a"
) else (
echo %%~Na:%%b
)
)) > output.tmp
REM ren output.tmp output.txt
I want the output to be:
filename1 data data data data....
filename2 data data data data....
filename3 data data data data....
but again, the code does not cycle through text files located in the subfolders
FindStr /?
before reading the output. Hint, you should take special note of the/S
option. – Compo