Solved create csv based on existing file name batch file

July 8, 2013 at 12:17:56
Specs: Windows 7
I need a batch file that will go through a folder of csv files, create a new csv file (to add a header) naming the new file by a designated portion of the original files name and then add the contents of the original file to the new file. I just started working with batch files and I have no idea how to do this.

See More: create csv based on existing file name batch file

Report •

July 8, 2013 at 19:38:11
First, you need to clarify how this header is composed. Second, you need to specify what is intended by "a designated portion" of the file's name. The former has infinite possibilities, the latter effectively so. Some examples would be really really good.

(For #2, we could just append a "1" to the filename but keep the CSV extension, for example.)

Basically, create the new-file-name, echo the header to the new-file-name, then copy new-file-name + original-file-name.

Report •

July 9, 2013 at 01:02:01
✔ Best Answer
::====== script starts here ===============
:: csvheadr.bat 2013-07-09 14:51:06.00
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

set /p H=<header

for /f "tokens=* delims= " %%a in ('dir/b *.csv') do (
call :sub1 %%a
goto :eof

for /f "tokens=* delims= " %%i in ("%1") do (
set F=%%i
set N=!F:~0,4!
type %1
move/y NEWFILE !N!.csv
goto :eof
::====== script ends here =================

M2 Golden-Triangle

Report •

July 9, 2013 at 07:19:59
Thank you so much. That script works great. I was getting stuck on of all things the retaining the filename.

Report •

Related Solutions

Ask Question