English 中文(简体)
通过Powerhell或Batch从Windows到UNIX
原标题:Convert file from Windows to UNIX through Powershell or Batch

我有一份打字稿,促使用户提供一些投入,然后利用AIX环境将一米卷宗投入使用。 这些档案必须采用UNIX格式(我认为是UTF8),但我期待着在SPRILEST这样做的方法方面给予某种指导。

我无需下载额外的软件包;Cygwin或GnuWin32。 如果可能的话,我就不考虑这样做,我的编码选择是Batch、Powerhell和VBS。 是否有任何人知道这样做的方法?

或者,我能否与Batch建立档案,并叫一个权力书来改革这些档案?

这里的想法是要求用户提供一些信息,然后我拿出一份标准文件,基本上在AIX中迅速找到工作。 我先使用Batch,因为我不知道我会遇到这一问题,但我喜欢在强权中这样做。 由于我在另一个论坛找到了一些能够转换的法规(下文)。

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

探讨这方面的一些方向或一些投入。

最佳回答

在批量档案中,如果没有外部工具,你就可以这样做。

如果大家需要的是文件编码,那么你提供的刀子应当发挥作用。 如果你想要将档案转换成线(而不是将其书写到另一个地方),你可以这样做。

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).

接下来的问题是,视窗和Uniix之间的界限不同。 Unix只使用U+000A(LF),Windows则使用两种特性:U+000DU+000A(CR+LF)。 理想的情况是,你也转折。 但是,这更加复杂:

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)
}
问题回答

如果你使用ANSI特性,而不是仅仅使用ASCII特性,则使用超负荷版本的RepAllText(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.text.encoding(v=vs.110).aspx
ASCII - Gets an encoding for the ASCII (7-bit) character set.
Default - Gets an encoding for the operating system s current ANSI code page.





相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签