1

I have hundreds of csv files in one folder, having 9 columns ,wherein i want to delete last 8 columns and 1st row from all the files.

file looks like this enter image description here

Also I am not sure if I can overwrite the the same file which is edited. For now i am considering that it is possible. And if overwriting is not possible then it should save with same file name may be in different directory

Please suggest

Any help appreciated.

Penny
  • 824
  • 1
  • 14
  • 31
  • Do you have to use a batch file? It is possible via batch (see: http://stackoverflow.com/questions/8520313/dos-batch-script-to-parse-csv-file-and-output-a-text-file) but I would recommend implementing in another language. – Kevin Nov 11 '14 at 12:19
  • Why? It's just a CSV that needs to be parsed using a comma delimiter. This is batch `for` loops 101. – SomethingDark Nov 11 '14 at 12:25
  • Yes I need in batch file. I have one entire process in batch file where this is a part of that process – Penny Nov 11 '14 at 12:34

1 Answers1

1

This code assumes you've put the batch file in the same directory as the csv files.

@echo off

for /f %%A in ('dir /b *.csv') do (
    for /f "skip=1 tokens=1,2* delims=," %%B in (%%A) do (
        echo %%B>>newdata.csv
    )
    copy /y newdata.csv %%A
    del newdata.csv
)

And this code makes no assumptions about the location of your script, source csv files, or target folder:

@echo off

set source_folder=C:\path\to\your\csv\files
set target_folder=C:\path\to\your\output\folder

if not exist %target_folder% mkdir %target_folder%

for /f %%A in ('dir /b %source_folder%\*.csv') do (
    for /f "skip=1 tokens=1,2* delims=," %%B in (%source_folder%\%%A) do (
    echo %%B>>%target_folder%\%%A
    )
)
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • It works great ,also can you suggest if my batch file is in other directory then how will the code look like – Penny Nov 11 '14 at 12:45
  • In the first for loop, you'd just change the part in parentheses to `('dir /b "C:\wherever\your\files\are\*.csv"')` - the quotes are only necessary if you have spaces anywhere in the path or filename. – SomethingDark Nov 11 '14 at 12:46
  • And if not overwrite existing files then how do u save in different dir all the modified files? What would be the code in that case? – Penny Nov 11 '14 at 13:11
  • Then you'd delete the `copy` and `del` lines and set the directory path in the `echo %%B` line. For example, if you wanted to move the new csv files to a folder in the current directory and keep the edited filenames the same as their old filenames, you can say `echo %%B>>./newdir/%%A` - make sure the directory exists first, of course. – SomethingDark Nov 11 '14 at 13:19
  • When I keep batch file in different dir it gives me a strange error saying "The system cannot find the file abc.csv. The system cannot find the file specified." I made changes told by you above ('dir /b "C:\wherever\your\files\are\*.csv"') – Penny Nov 11 '14 at 13:39
  • %%B is taking the filename without the path and that's making the code assume the csv files are in the same path as the script. I've edited my answer to provide a solution that makes no assumptions about file locations. – SomethingDark Nov 11 '14 at 13:48