通过PowerShell的或批量转换从Windows到UNIX文件 [英] Convert file from Windows to UNIX through Powershell or Batch

查看:643
本文介绍了通过PowerShell的或批量转换从Windows到UNIX文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理脚本提示用户某些输入然后输出一对夫妇我使用AIX环境中的文件。这些文件需要在UNIX格式(我认为这是UTF8),但我在寻找做这个最简单的方法了一些方向。

I have a batch script that prompts a user for some input then outputs a couple of files I'm using in an AIX environment. These files need to be in UNIX format (which I believe is UTF8), but I'm looking for some direction on the SIMPLEST way of doing this.

我不喜欢有下载额外的软件包; Cygwin的或的GnuWin32。我不介意这个编码,如果有可能,我的编码选项批次,PowerShell和VBS。有谁知道的一种方式做到这一点?

I don't like to have to download extra software packages; Cygwin or GnuWin32. I don't mind coding this if it is possible, my coding options are Batch, Powershell and VBS. Does anyone know of a way to do this?

我也可以创建一个批处理文件并调用PowerShell脚本改革这些?

Alternatively could I create the files with Batch and call a Powershell script to reform these?

这里的想法是,用户将被提示输入一些信息,然后我这是基本的AIX提示答案的作业输出的标准文件。我使用批处理最初,因为我不知道我会遇到这个问题,但我对在PowerShell中重做这种倾斜。因为我发现了另一个论坛上一些code,可以做转换(如下图)。

The idea here is a user would be prompted for some information, then I output a standard file which are basically prompt answers in AIX for a job. I'm using Batch initially, because I didn't know that I would run into this problem, but I'm kind of leaning towards redoing this in Powershell. because I had found some code on another forum that can do the conversion (below).

% foreach($i in ls -name DIR/*.txt) { \
       get-content DIR/$i | \
       out-file -encoding utf8 -filepath DIR2/$i \
  }

寻找这方面的一些指示或某些输入。

Looking for some direction or some input on this.

推荐答案

您不能在批处理文件做没有外部工具。

You can't do this without external tools in batch files.

如果你需要的是文件编码,然后你给的片段应该工作。如果你要转换的文件内联(而不是将其写入到另一个地方),你可以做

If all you need is the file encoding, then the snippet you gave should work. If you want to convert the files inline (instead of writing them to another place) you can do

Get-ChildItem *.txt | ForEach-Object { (Get-Content $_) | Out-File -Encoding UTF8 $_ }

(约获取内容括号是重要的)然而,这将在UTF-8将文件写入与在启动(U + FEFF)的签名一些Unix工具不接受(即使它在技术上是合法的,但劝阻使用)。

(the parentheses around Get-Content are important) However, this will write the files in UTF-8 with a signature at the start (U+FEFF) which some Unix tools don't accept (even though it's technically legal, though discouraged to use).

再有就是换行符Windows和Unix之间不同的问题。 UNIX只使用U + 000A(LF),而Windows使用为两个字符:U + 000D U + 000A(CR + LF)。所以,理想情况下,你会转换换行了。但是,这变得有点复杂得多:

Then there is the problem that line breaks are different between Windows and Unix. Unix uses only U+000A (LF) while Windows uses two characters for that: U+000D U+000A (CR+LF). So ideally you'd convert the line breaks, too. But that gets a little more complex:

Get-ChildItem *.txt | ForEach-Object {
  # get the contents and replace line breaks by U+000A
  $contents = [IO.File]::ReadAllText($_) -replace "`r`n?", "`n"
  # create UTF-8 encoding without signature
  $utf8 = New-Object System.Text.UTF8Encoding $false
  # write the text back
  [IO.File]::WriteAllText($_, $contents, $utf8)
}

这篇关于通过PowerShell的或批量转换从Windows到UNIX文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆