记事本另存很有用,有没有命令行工具能做到和记事本一样的效果的请问
可以使用 PowerShell
get-content "源文件路径" | out-file "目标文件路径" -encoding utf8
最好指定源文件编码
可以试试 iconv
像上面说的使用iconv可以批量处理,但前提是必须知道源编码,iconv不会去猜测编码。
iconv -f gb2312 -t utf8 -o output.txt input.txt
如果需要批量转换(根据实际情况适当修改)
gci -path *.txt | % {$output=$_.basename+"-utf8.txt";iconv -f gb2312 -t utf8 -o $output $_}
感谢各位,那有没有检测文件编码的命令行工具呢,是不是要先测出来是啥再去转换
以下是一个简单的 Shell 脚本,可以在 cmd
中将文件转换为UTF-8编码:
#!/bin/bash
# 检查输入参数是否正确
if [ $# -ne 1 ]; then
echo "Usage: $0 <filename>"
exit 1
fi
# 文件名
filename=$1
# 检查文件是否存在
if [ ! -f "$filename" ]; then
echo "Error: File '$filename' does not exist."
exit 1
fi
# 检测文件编码
file_encoding=$(file -i "$filename" | awk -F "=" '{print $2}')
# 输出文件编码信息
echo "File '$filename' encoding is: $file_encoding"
# 转换为UTF-8编码
iconv -f "$file_encoding" -t UTF-8 "$filename" > "${filename%.txt}_utf8.txt"
echo "File '$filename' has been converted to UTF-8 encoding."
将以上内容保存为 convert_to_utf8.sh
文件。然后在 cmd
中执行以下命令:
convert_to_utf8.sh yourfile.txt
这将把 yourfile.txt
转换为UTF-8编码,生成一个新的文件 yourfile_utf8.txt
。
【注意:脚本和待转换文件要放在同一目录下】
ps:或者直接用我刚刚找到的 这个网站
这个是批量操作
#!/bin/bash
# 检查输入参数是否正确
if [ $# -ne 1 ]; then
echo "Usage: $0 <directory>"
exit 1
fi
# 文件夹名
directory=$1
# 检查文件夹是否存在
if [ ! -d "$directory" ]; then
echo "Error: Directory '$directory' does not exist."
exit 1
fi
# 获取文件夹中的所有文件
files=$(find "$directory" -type f)
# 遍历每个文件
for file in $files; do
# 检测文件编码
file_encoding=$(file -i "$file" | awk -F "=" '{print $2}')
echo "File '$file' encoding is: $file_encoding"
# 转换为UTF-8编码
iconv -f "$file_encoding" -t UTF-8 "$file" > "${file%.txt}_utf8.txt"
echo "File '$file' has been converted to UTF-8 encoding."
done
echo "Conversion complete."
保存为 batch_convert_to_utf8.sh
文件,执行
batch_convert_to_utf8.sh your_directory
转换目录中的所有文件为UTF-8编码,并在目录下生成新的UTF-8编码文件。
PowerShell 在 get-content 的时候就会检测编码,一般来说够用了
还真不是,是一个C程序输出的txt需要另一个软件处理,但不是utf-8的
谢谢 ,不过sh在windows下不能运行
查了下安装了cygwin,按ALL安装的
但是还是找不到iconv
迷惑中
少了个前置步骤:安装 wsl 或 git 吧?不然 Windows 上怎么用 bash?
emeditor 这个软件免费版就行.
转换一个文件编码
" 源文件" /cps 保存编码 /ss+ /sa " 目标文件"
参数 | 注释 |
---|---|
/cps 编码 | 设定一个用来保存的编码。一个编码可以是 编码常数 之一。 |
/ss+ | 用一个 Unicode 签名 (BOM) 保存文件在在转换编码之后。 |
/ss- | 不用一个 Unicode 签名 (BOM) 保存文件在在转换编码之后。 |
/sa " 新文件" | 在转换编码后指定一个要保存为的文件名称。 |
啊哈,你说的没错。
没有考虑到提问者的环境问题,真是失策。