0

如何在不对段落进行任何操作的情况下将段落回显到文件中。我的意思是换行符、空格、特殊字符等,所有的缩进应该是一样的。

我的 .bat 文件(包含 .ppk 文件的内容)

    echo (PuTTY-User-Key-File-2: ssh-rsa
Encryption: none
Comment: rsa-key-20130127
Public-Lines: 6
AAAAB3NzaC1yc2EAAAABJQAAAQBVYEVYbGluh1Ne6psUMsK4TRiqwN8GG+ImbsfC
qIYje3S7n3owtEUSDMEc5VvTZTEXk/CKHK6tXhGSNrExXKDhKE2HejY7TFtbc3vU
KM4OogxYeZs/0yBX/kVEu5+kIeZ0ZEsq/ve9/hnEVLZ3DFotUoDzzwdd4jAHUZv2
08xk5tTxodh+iO26RVPSaklZrFjbZkqCwPOnVZhK6JqE/7kZyIM+p5W8CH8XPG3r
fgU/R4BpwMNz+pYo2iiV3eZApI25lY+IcjclA5Amx1JdCM1zIvA6C8ABUbTMXsyG
RHkzdEYjw5+pBF8qLU8s8+M9sw0l5z58dP9t+vuYWz+JrsmL
Private-Lines: 14
AAABAECb4XNY9dcaTO3h+NED0aA6V9sqLDv5bN7QX9GUSdWpiMVWF0dzqeQlU96E
DiNvLBHXvPLlRer7FDdL+7am9kmGSIIy+JuTusG/LUaba4Cx+4E5bpEqJlBtZNoR
ceP9+oGYAYhSPvCkneCuz0VVdKytI1C4WJoS8+nc5LrJyxvsFGgVEIo5nadkABRV
eOLwotq2Mj/lLXHMbE5lB+9m9VNsWrBErNVCwNQdhQOyG1E39YcwBV+hB+Pyu41z
6EHxIRXQvbYNE2HjLvowrnX/9fFfx1kf51+WE+VbFQxrrZqE1p3y2S2kJkAopRio
KZBlNwrR6mwSLtRn4a6ZecKwVE0AAACBAJVV6O5leR0gl27DVqnXu6mhf6Xb8Ije
2JfvLCa73bgpFSYQ2EpxKTiSpZpM3kPpnC7Y0SvigstGHwze/OuCXYnzT/KDkBrs
NDPiuTSmzW3JxtGv1axpfRRt/PaRIHq6pCtyGxIc5A0RrTuSrPzZkHxFggkAYSMK
9YOi1sZhcVtlAAAAgQCSW1Pvq4zSjsUuGMc/w8T8mOOZG4Yllq7e9CSpTMeqeVHl
Vytm5+ujIyas/k/UGA8WQt3ZnD5uLF7tBRoaHf88oE00nXVnLlDeME3Jdbts84tE
1HB9RzwGE+BVknEiNcUjqhVcRqv6+pOClR+K5VOstqs/tmmMOA57c1481K/aLwAA
AIAQn9Fhg4Ih71uvber4RELcZCQrvRFsuASYHrgYBkzw17PTPQ9APv0B1nIPZ/u6
+jhou7qzR78yrYw0Po4ZJmmDt0CP/cZPWL4jQ6sM6on98D6TdKk7rE1c0WPn1Bta
ZtaxQqOku7eAfw745L9EjokekZjohjTFI9rSFdsCfa6dXw==
Private-MAC: 2e75877ab827b492b2a0a16c5019cd45f96e4990) > myfile.txt

在我将其保存为 .bat 并执行后,上面的行不起作用

我想将该段落放在一个具有唯一名称的文件中,这样它就不会覆盖任何现有文件。我是否需要在每一行回显并继续附加。

4

1 回答 1

2

在最后的 EXIT /B 之后放置一个独特的文本标记,然后是您的段落。使用 FOR /F 和 FINDSTR 来定位唯一标记,然后使用 FOR /F 循环来处理标记之后的所有行。

有很多变化,每一种都有不同的限制。

这是最简单的形式,但也有最大的局限性。文本中描述了这些限制。

@echo off
setlocal disableDelayedExpansion
set "skip="
for /f "delims=:" %%N in (
  'findstr /x /n ":::BeginText" "%~f0"'
) do if not defined skip set skip=%%N
>test.txt (
  for /f "usebackq skip=%skip% delims=" %%A in ("%~f0") do echo(%%A
)
type test.txt
exit /b

:::BeginText
This text will be exactly preserved with the following limitations:

  1) Each line will be terminated by CR LF even if original has only LF.

  2) Lines are limited in length to approximately 8191 bytes.

  3) Empty lines will be stripped.

; 4) Lines beginning with ; will be stripped.

  5) The text will be truncated at the first occurance of hex code 0x1A (Ctrl-Z).

Special characters like ^ & < > | etc. do not cause a problem


一些奇怪的 FOR /F 选项语法消除了以开头的行的限制;

@echo off
setlocal disableDelayedExpansion
set "skip="
for /f "delims=:" %%N in (
  'findstr /x /n ":::BeginText" "%~f0"'
) do if not defined skip set skip=%%N
>test.txt (
  for /f ^usebackq^ skip^=%skip%^ delims^=^ eol^= %%A in ("%~f0") do echo(%%A
)
type test.txt
exit /b

:::BeginText
This text will be exactly preserved with the following limitations:

  1) Each line will be terminated by CR LF even if original has only LF.

  2) Lines are limited in length to approximately 8191 bytes.

  3) Empty lines will be stripped.

  4) The text will be truncated at the first occurance of hex code 0x1A (Ctrl-Z).

Special characters like ^ & < > | etc. do not cause a problem
;Lines beginning with ; are preserved


读取 FINDSTR /N 输出而不是直接读取文件会保留空行。但是引入了领先的新限制:

@echo off
setlocal disableDelayedExpansion
set "skip="
for /f "delims=:" %%N in (
  'findstr /x /n ":::BeginText" "%~f0"'
) do if not defined skip set skip=%%N
>test.txt (
  for /f "skip=%skip% tokens=1* delims=:" %%A in (
   'findstr /n "^" "%~f0"'
  ) do echo(%%B
)
type test.txt
exit /b

:::BeginText
This text will be exactly preserved with the following limitations:

  1) Each line will be terminated by CR LF even if original has only LF.

  2) Lines are limited in length to approximately 8191 bytes.

:::  3) Leading : will be stripped from each line.

Special characters like ^ & < > | etc. do not cause a problem
Empty lines are preserved!
;Lines beginning with ; are preserved.


这个最终版本几乎和它一样好。延迟扩展必须打开和关闭以保留!可能出现在文本中的内容。

@echo off
setlocal disableDelayedExpansion
set "skip="
for /f "delims=:" %%N in (
  'findstr /x /n ":::BeginText" "%~f0"'
) do if not defined skip set skip=%%N
>test.txt (
  for /f "skip=%skip% tokens=*" %%A in (
   'findstr /n "^" "%~f0"'
  ) do (
    set "line=%%A"
    setlocal enableDelayedExpansion
    echo(!line:*:=!
    endlocal
  )
)
type test.txt
exit /b

:::BeginText
This text will be exactly preserved with the following limitations:

  1) Each line will be terminated by CR LF even if original has only LF.

  2) Lines are limited in length to approximately 8191 bytes.

Special characters like ^ & < > | etc. do not cause a problem.
Empty lines are preserved!
;Lines beginning with ; are preserved.
:::Leading : are preserved.
于 2013-01-27T17:25:42.887 回答