Solved Append filename to end of each row in .csv file

May 17, 2012 at 07:30:39
Specs: Server 2008
I have the batch code as below. This only insert filename to each line(%rundir%tmpfile.txt
). I'd like to append the filename to end of each row (original .csv file). Doing this would help to insert filename into database table using sqlldr. But can't get it to work. HELP is really appreciated.

FOR /F %%B in ('dir /b %rundir%*TEST*.csv') do (
set FILENAME=%%B
::echo FILENAME > %rundir%\tmpfile.txt
::copy %rundir%\tmpfile.txt %rundir%*TEST*.csv
echo %%B %FILENAME% >> %rundir%tmpfile.txt
)


See More: Append filename to end of each row in .csv file

Report •

#1
May 17, 2012 at 13:45:48
✔ Best Answer
Untested
@echo off
for /f "tokens=*" %%i in (
'dir /b *test*.csv'
) do (
for /f "tokens=*" %%z in (
'type %%i'
) do (
>>tmpfile.txt echo %%z %%i
)
2>nul move tmpfile.txt %%i
)

Tony


Report •

#2
May 17, 2012 at 15:13:50
Thanks Tony!
But it's still not working. Even didn't create the tmpfile.txt.
forgot about the reading directory:
set rundir=\\server\running\(.csv)

Thanks.


Report •

#3
May 17, 2012 at 20:35:50
It seems to work under local directory (with .txt file tests), creates and appends filename to the same file, but i'm still having problems with different directory; it can't read and append (write) the filename.

local dir: set dir=e:\test1\test2\test3\
running dir: set rundir=e:\test1\test2a\test2b\test2c\test2d\

@echo off
::SETLOCAL enableDELAYdedeXpansion
for /f "tokens=* delims= " %%i in ('dir /b %rundir%*test*.txt') Do (
::gawk -f awk_script_file.awk %%i
for /f "tokens=* delims= " %%z in ('type %%i') Do (
echo %%z %%i >>tmpfile.txt)
move tmpfile.txt %rundir%%%i 2>nul
)

Thanks.


Report •

Related Solutions

#4
May 18, 2012 at 06:58:37
@echo off
pushd c:\your\dir
for /f "tokens=*" %%i in (
'dir /b *test*.csv'
) do (
for /f "tokens=*" %%z in (
'type %%i'
) do (
>>tmpfile.txt echo %%z %%i
)
2>nul move tmpfile.txt %%i
)
popd

Tony


Report •

#5
May 18, 2012 at 07:37:27
Thanks Tony!
yours works even faster than below:

@echo off
::SETLOCAL enableDELAYdedeXpansion
move %rundir%*test*.csv %dir%
for /f "tokens=* delims=. " %%i in ('dir /b *test*.csv') Do (
gawk -f awk_script_file.awk %%i
::set /i N+=1
for /f "tokens=* delims=. " %%z in ('type %%i') Do (
echo %%z %%i >> tmpfile.txt)
::move %dir%*test*.csv %rundir%
move tmpfile.txt %rundir%%%i 2>nul
)

Now, the problem is the filename is resided in the last cells of each row (concatenated with some text in this cell). Can we add this filename to new another column (after last)?

Thanks.


Report •

#6
May 18, 2012 at 13:19:24
You probably just need to add your delimeter:

echo ,%%z %%i >> tmpfile.txt)

If it's not a comma, change it to what you're using.

Tony


Report •

#7
May 18, 2012 at 14:36:07
Thanks Tony!
It works as below.

echo %%z,%%i >> tmpfile.txt
or
echo %%z,"%%i" >> tmpfile.txt (as other cells)

Again, thank you very much.


Report •

#8
June 19, 2012 at 09:06:42
Hi,
Is there a way that we can add Filename into any random column (such as column 5 or 6 over 10 columns) each row instead of the end of each row.

Thanks.


Report •

Ask Question