简体   繁体   English

通过Powershell或Batch将文件从Windows转换为UNIX

[英]Convert file from Windows to UNIX through Powershell or Batch

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. 我有一个批处理脚本,提示用户输入一些输入,然后输出我在AIX环境中使用的几个文件。 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. 这些文件需要采用UNIX格式(我认为是UTF8),但我正在寻找SIMPLEST方法的一些方向。

I don't like to have to download extra software packages; 我不喜欢下载额外的软件包; Cygwin or GnuWin32. Cygwin或GnuWin32。 I don't mind coding this if it is possible, my coding options are Batch, Powershell and VBS. 如果有可能,我不介意编码,我的编码选项是Batch,Powershell和VBS。 Does anyone know of a way to do this? 有谁知道这样做的方法?

Alternatively could I create the files with Batch and call a Powershell script to reform these? 或者,我可以用Batch创建文件并调用Powershell脚本来改造这些吗?

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. 这里的想法是用户会被提示输入一些信息,然后我输出一个标准文件,这些文件基本上是AIX中为作业提示的快速答案。 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. 我最初使用Batch,因为我不知道我会遇到这个问题,但我有点倾向于在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 $_ }

(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). Get-Content周围的括号很重要)但是,这将在UTF-8中写入带有签名的文件(U + FEFF),一些Unix工具不接受(尽管它在技术上是合法的,但不鼓励使用)。

Then there is the problem that line breaks are different between Windows and Unix. 然后存在Windows和Unix之间的换行不同的问题。 Unix uses only U+000A (LF) while Windows uses two characters for that: U+000D U+000A (CR+LF). Unix仅使用U + 000A(LF),而Windows使用两个字符: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)
}

Try the overloaded version ReadAllText(String, Encoding) if you are using ANSI characters and not only ASCII ones. 如果您使用的是ANSI字符而不仅仅是ASCII字符,请尝试重载版本ReadAllText(String,Encoding)。

$contents = [IO.File]::ReadAllText($_, [Text.Encoding]::Default) -replace "`r`n", "`n"

https://msdn.microsoft.com/en-us/library/system.io.file.readalltext(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.io.file.readalltext(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.110).aspx
ASCII - Gets an encoding for the ASCII (7-bit) character set. ASCII - 获取ASCII(7位)字符集的编码。
Default - Gets an encoding for the operating system's current ANSI code page. 默认值 - 获取操作系统当前ANSI代码页的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM