2

I need to open a .csv file and instantly save it with the same name (overwrite it) using a .bat.

This may sound stupid but it is the easiest way that I've found to change that .csv file codification (from ANSI to UTF-8).

If I type notepad file-name.csv it opens the .csv file, but how do I save it now?

user2864740
  • 60,010
  • 15
  • 145
  • 220
maeq
  • 1,073
  • 1
  • 12
  • 23
  • Why do you need to open and save it ? –  Sep 08 '14 at 10:00
  • @Jidder The .csv file is downloaded from Google Drive and It has ANSI codification. If I open that file with Notepad and save it, it codification changes to UTF-8. That's why I need a script to do this process automatically. – maeq Sep 08 '14 at 10:04
  • You say `.bash` (which is a Linux console environment) but at the same time you mention "notepad" which is a Windows tool. Which operating are you using? In Linux you can use `iconv` to convert the encoding of a text file. –  Sep 08 '14 at 10:05
  • @user2864740 I need to open the file with Notepad because the codification of the file changes from ANSI to UTF-8 when I open it with Notepad and save it. – maeq Sep 08 '14 at 10:05
  • Since you have `bash` installed, I take it mingw/cygwin is available? [I would probably see this then](http://stackoverflow.com/questions/64860/best-way-to-convert-text-files-between-character-sets) (note that even a PowerShell solution is provided). You're using the wrong tool for the job. – user2864740 Sep 08 '14 at 10:06
  • @a_horse_with_no_name I meant .bat file. Sorry. I'm working on Windows 8.1 – maeq Sep 08 '14 at 10:06
  • You can use the Windows port of the iconv tool: http://gnuwin32.sourceforge.net/packages/libiconv.htm –  Sep 08 '14 at 10:23
  • @a_horse_with_no_name THANKS.. that worked.. Can you type it as a answer so I can vote it up? – maeq Sep 08 '14 at 10:47

2 Answers2

1

You can use the Windows port of the Linux tool iconv which can convert text files from one encoding to another.

The gnuwin32 project has ports of all major GNU utilities, including iconv which can be downloaded as a single package from here:

1

For a pure script solution, this is a hybrid batch/javascript solution (adapted from a previous vbs answer). Adapt as needed and save as .cmd file.

@if (@this==@isBatch) @then
@echo off

    cscript //nologo //e:jscript "%~f0" ^
            /input:"input_file.csv" ^
            /output:"output_file.csv" ^
            /from:"x-ansi" ^
            /to:"utf-8"

    exit /b

@end
var adTypeText = 2;
var adSaveCreateOverWrite = 2;

var inputFile = WScript.Arguments.Named.Item('input');
var outputFile = WScript.Arguments.Named.Item('output');

var from = WScript.Arguments.Named.Item('from');
var to = WScript.Arguments.Named.Item('to');

var inputStream  = WScript.CreateObject('adodb.stream');
    with (inputStream){
        Type = adTypeText;
        Charset = from;
        Open();
        LoadFromFile( inputFile );
    }

var outputStream = WScript.CreateObject('adodb.stream')
    with (outputStream){
        Type = adTypeText;
        Charset = to;
        Open();
        WriteText( inputStream.ReadText );
        SaveToFile( outputFile, adSaveCreateOverWrite );
    }

    inputStream.Close()
    outputStream.Close()
MC ND
  • 69,615
  • 8
  • 84
  • 126