Dee*_*ain 65 unix windows cmd batch-file eol
是否有Windows命令来转换文件的行结尾?
我们test.bat需要运行以启动我们的服务器.我们使用Perforce,我们需要在工作区中使用unix行结尾.出于某种原因,我们不允许在工作区中将行结尾更改为Windows.但是,服务器在Windows上运行.
每次我必须运行bat文件时,我在Notepad ++中打开它并选择编辑→EOL转换→Windows.有没有办法实现自动化,这样我们每次与Perforce同步时都不需要手动更改行结尾?
提前致谢.
Tam*_*aze 56
实际上,使用moreWindows NT及更高版本中包含的命令可以非常轻松地完成此操作.要转换input_filename包含UNIX EOL(行尾)\n到output_filename其中包含的Windows EOL \r\n,只是这样做:
TYPE input_filename | MORE /P > output_filename
Run Code Online (Sandbox Code Playgroud)
该more命令具有您可能不知道的其他格式选项.运行more/?以了解还能more做些什么.
Ans*_*ers 15
您可以在VBScript中无需其他工具的情况下执行此操作:
Do Until WScript.StdIn.AtEndOfStream
WScript.StdOut.WriteLine WScript.StdIn.ReadLine
Loop
Run Code Online (Sandbox Code Playgroud)
将上面的行放在一个文件中unix2dos.vbs并像这样运行:
cscript //NoLogo unix2dos.vbs <C:\path\to\input.txt >C:\path\to\output.txt
Run Code Online (Sandbox Code Playgroud)
或者像这样:
type C:\path\to\input.txt | cscript //NoLogo unix2dos.vbs >C:\path\to\output.txt
Run Code Online (Sandbox Code Playgroud)
您也可以在PowerShell中执行此操作:
(Get-Content "C:\path\to\input.txt") -replace "`n", "`r`n" |
Set-Content "C:\path\to\output.txt"
Run Code Online (Sandbox Code Playgroud)
可以进一步简化为:
(Get-Content "C:\path\to\input.txt") | Set-Content "C:\path\to\output.txt"
Run Code Online (Sandbox Code Playgroud)
上述语句在没有明确替换的情况下工作,因为Get-Content在任何类型的换行符(CR,LF和CR-LF)中隐式拆分输入文件,并Set-Content在将输入数组写入文件之前将输入数组与Windows换行符(CR-LF)连接起来.
Jur*_*osh 11
我正在处理CRLF问题所以我决定构建非常简单的转换工具(在NodeJS中):
因此,如果您安装了npm的NodeJS,您可以尝试:
npm i -g eol-converter-cli
eolConverter crlf "**/*.{txt,js,java,etc}"
Run Code Online (Sandbox Code Playgroud)
可以使用Glob正则表达式动态配置路径(与shell中的正则表达式相同).
因此,如果您可以使用NodeJS,它非常简单,您可以集成此命令将整个工作区转换为所需的行结尾.
kxr*_*kxr 10
Windows的MORE不可靠,它不可避免地破坏了TAB并增加了线条.
unix2dos也是MinGW/MSYS,Cygutils,GnuWin32和其他unix二进制端口集合的一部分 - 可能已经安装.
当python在那里时,这个单线程将任何行结尾转换为当前平台 - 在任何平台上:
TYPE UNIXFILE.EXT | python -c "import sys; sys.stdout.write(sys.stdin.read())" > MYPLATFILE.EXT
Run Code Online (Sandbox Code Playgroud)
要么
python -c "import sys; sys.stdout.write(open(sys.argv[1]).read())" UNIXFILE.EXT > MYPLATFILE.EXT
Run Code Online (Sandbox Code Playgroud)
或者根据您的平台将单行内容放入.bat/shell脚本和PATH:
@REM This is any2here.bat
python -c "import sys; sys.stdout.write(open(sys.argv[1]).read())" %1
Run Code Online (Sandbox Code Playgroud)
并使用该工具
any2here UNIXFILE.EXT > MYPLATFILE.EXT
Run Code Online (Sandbox Code Playgroud)
基于TampaHaze和MD XF的有用答案.
这将改变所有.txt文件来代替当前目录从LF到CRLF在命令提示符
for /f %f in ('dir /b "*.txt"') do ( type %f | more /p > %f.1 & move %f.1 %f )
Run Code Online (Sandbox Code Playgroud)
如果您不想验证每一个更改
移动
至
移动/ y
要包含子目录更改
dir/b
至
dir/b/s
要在包含子目录的批处理文件中执行所有这些操作,而不在下面提示使用
@echo off
setlocal enabledelayedexpansion
for /f %%f in ('dir /s /b "*.txt"') do (
type %%f | more /p > %%f.1
move /y %%f.1 %%f > nul
@echo Changing LF-^>CRLF in File %%f
)
echo.
pause
Run Code Online (Sandbox Code Playgroud)
试试这个:
(for /f "delims=" %i in (file.unix) do @echo %i)>file.dos
Run Code Online (Sandbox Code Playgroud)
会话协议:
C:\TEST>xxd -g1 file.unix 0000000: 36 31 36 38 39 36 32 39 33 30 38 31 30 38 36 35 6168962930810865 0000010: 0a 34 38 36 38 39 37 34 36 33 32 36 31 38 31 39 .486897463261819 0000020: 37 0a 37 32 30 30 31 33 37 33 39 31 39 32 38 35 7.72001373919285 0000030: 34 37 0a 35 30 32 32 38 31 35 37 33 32 30 32 30 47.5022815732020 0000040: 35 32 34 0a 524. C:\TEST>(for /f "delims=" %i in (file.unix) do @echo %i)>file.dos C:\TEST>xxd -g1 file.dos 0000000: 36 31 36 38 39 36 32 39 33 30 38 31 30 38 36 35 6168962930810865 0000010: 0d 0a 34 38 36 38 39 37 34 36 33 32 36 31 38 31 ..48689746326181 0000020: 39 37 0d 0a 37 32 30 30 31 33 37 33 39 31 39 32 97..720013739192 0000030: 38 35 34 37 0d 0a 35 30 32 32 38 31 35 37 33 32 8547..5022815732 0000040: 30 32 30 35 32 34 0d 0a 020524..
| 归档时间: |
|
| 查看次数: |
106831 次 |
| 最近记录: |