Hello. I have files, they are all in cp1251 encoding, I need to re-save them in utf-8 encoding. Manually through the editor is not an option, since the files are more than 1000 pcs. Please tell me some optimal way.
- oneYou would at least OS indicated. And when you specify - try it yourself to look for a recoder for it. Surely there is not one, and not even one dozen. - user6550 5:46 pm
- windows 7. I found a couple of programs, but they did not help me - iKey
- Without specifying in which language the solution should be, it turns out that the question is a recommendation request, which tool to use, and this is outside the format of this site. - Nakilon 5:06
- If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky β¦
5 answers
I do not work with Windows, but I can offer a solution for any system if you have the Python 3 interpreter installed:
import os from glob import glob for filename in glob('*.txt'): # Π·Π°Π΄Π°ΡΡΠ΅ ΡΠ°Π±Π»ΠΎΠ½ Π΄Π»Ρ ΡΠ°ΠΉΠ»ΠΎΠ² with open(filename, 'r', encoding='cp1251') as in_file: raw_filename, ext = os.path.splitext(filename) # Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΡΡΡΡΠΈΠΊΡ ΠΈΠΌΠ΅Π½ΠΈ ΡΠ°ΠΉΠ»Π°, ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΏΠ°ΠΏΠΊΡ new_filename = raw_filename + '-unicode' + ext with open(new_filename, 'w') as out_file: # Π·Π°ΠΏΠΈΡΡΠ²Π°Π΅ΠΌ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ out_file.write(in_file.read()) For files in the current directory:
for /r %%a in (*.php) do "ΠΏΠ΅ΡΠ΅ΠΊΠΎΠ΄ΠΈΡΠΎΠ²ΡΠΈΠΊ %%a" where "recoder" is the recoder you chose with the necessary command line switches, in %%a will contain the file name.
since the author did not indicate his operating system in the question text, or with the help of tags, and the answer to the question may be of interest to users of the gnu/linux operating system, I will provide the answer for her:
$ for f in *.txt; do mv "$f" tmpfile; cat tmpfile | iconv -f cp1251 -t utf8 > "$f"; done; rm tmpfile for all files with the .txt suffix in the current directory, their contents will be encoded from cp1251 to utf8 .
- Basically, the OS was specified in the comment - Timofei Bondarev
- @TimofeyBondarev, I know. - aleksandr barakin
- oneI will add:
cygwinwill allow you to run this command on windows. - Pavel Mayorov
Get-Childitem d:\*.txt -File | foreach {$a=Get-Item $_ ; $b="d:\utf\"+$a.Name ; Get-Content $a | Out-File -Encoding UTF8 -FilePath $b} Powershell, for files inside a single directory. There is in windows 7 by default. You can add -recurse for all subfolders. You can read the paths from the file: Get-Content d: \ files.txt