Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批处理文件,将多行转换为单个变量_Batch File - Fatal编程技术网

Batch file 批处理文件,将多行转换为单个变量

Batch file 批处理文件,将多行转换为单个变量,batch-file,Batch File,我正在尝试制作一个批处理文件编码器,我目前的问题是我不知道如何获取一个文本文件,并将所有行放入一个变量中 如果我的文本文件是 Hello, my name is Benjamin 变量为“Hello,我的名字是Benjamin”(行间没有空格) 或者“你好,我叫本杰明”(字里行间有空格) 代码是 @echo off :a Cls echo please input a letter Set /p input= if %input%==iamdone goto done Echo>&g

我正在尝试制作一个批处理文件编码器,我目前的问题是我不知道如何获取一个文本文件,并将所有行放入一个变量中

如果我的文本文件是

Hello,

my name is

Benjamin
变量为“Hello,我的名字是Benjamin”(行间没有空格)

或者“你好,我叫本杰明”(字里行间有空格)

代码是

@echo off
:a
Cls
echo please input a letter
Set /p input=
if %input%==iamdone goto done
Echo>>temp.txt %input%
type>>temp2.txt %input%.txt
Goto a
:done

This is where i would put the commands im asking 
help for, then i would output the two variables 
(1 from temp.txt and 1 from temp2.txt) into textfiles.
另外,我使用文本文件进行加密,因为我还不够高级,所以文本文件a.txt将包含加密的字母a

@echo off
setlocal enabledelayedexpansion
set "line="
for /f "delims=" %%i in (bb.txt) do set line=!line!%%i
echo %line%

有关/,
setlocal/?
set/?

的信息,请参见
下一个脚本覆盖了任何文件内容旁边的内容,甚至包括cmd-和批处理有毒字符
%
|
^

重要提示:注意高级
set
语法中的双引号
set“varname=varvalue”

@ECHO关闭
SETLOCAL EnableExtensions DisableDelayedExpansion
rem清除“\u myVar”变量
设置“\u myVar=”
rem将文件中的所有行放入变量“%u myVar”
对于/F“usebackq令牌=*”%%G,在(“D:\bat\SO\files\33824203.txt”)中执行以下操作(
rem将非空行放入辅助变量“%u auxVar”`
设置“_auxVar=%%G”
电话:addLine
)
删除前导空格(如果有)
设置“_auxVar=”
如果定义了_myvarset“_auxVar=%_myVar:~0,1%”
如果“%\u auxVar%”设置“\u myVar=%\u myVar:~1%”
rem与'echo(%\u myVar%`'不同,下一个构造可以工作
rem,即使文件包含cmd有毒字符%、!、|等。
SETLOCAL EnableDelayedExpansion
回声(!\u myVar!
端部
端部
后藤:eof
:addLine
设置“\u myVar=%\u myVar%%\u auxVar%”
rem^设计的可选空间
后藤:eof
资源(必读,不完整):

  • (命令参考)
  • (其他特殊性)
  • %G
    %G
    等特殊页面)
  • 设置“\u auxVar=%\u myVar:~0,1%”
    等)

批处理文件在变量中有新行,这很麻烦。通常,
for/f
循环用于处理文件。您将如何处理该变量?能否发布完整的/相关的代码?您是否尝试过“您好,\n我的名字是\nBenjamin”?@kayosoufiane,为什么你认为这会在批处理文件中工作?批处理文件是一种非常原始的Windows脚本语言,不理解
\n
?@wOxxOm我尝试过这个(echo$'Hello,\n我的名字是\nBenjamin')并假设它会work@kayosoufiane,它不起作用,请参阅批处理文件标记的说明。将因许多不同的内容而失败。第一个挑战是
;hello
对于/F usebackq^tokens^=*^eol^=%%G in(“D:\bat\SO\files\33824203.txt”)执行(
处理
;hello
(@jeb的原始注释)这就是为什么我写的句子在意义上仅次于任何;既不是任何,也不是全部。我想一些简单的语法规则下的句子,例如,
Hello,Benjamin!
我讨厌21%的增值税!
在Jeb&Co购买它!
等:)我想到了一些简单的句子,比如
Jeb&Co说:“Hello Cat&Dog”
:-)但无论如何,您的解决方案很好,并且在大多数情况下都应该有效
@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
rem clear the `_myVar` variable
set "_myVar="
rem  put all the lines from file into `_myVar` variable 
for /F "usebackq tokens=*" %%G in ("D:\bat\SO\files\33824203.txt") do (
  rem put nonempty line into auxiliary variable `_auxVar`
  set "_auxVar=%%G"
  call :addLine 
)
rem remove leading space if any
set "_auxVar="
if defined _myVar set "_auxVar=%_myVar:~0,1%"
if "%_auxVar%"==" " set "_myVar=%_myVar:~1%"

rem unlike `echo(%_myVar%`, next construct would work 
rem even though file contains cmd-poisonous characters %, !, <, >, | etc. 
SETLOCAL EnableDelayedExpansion
  echo(!_myVar!
ENDLOCAL

ENDLOCAL
goto :eof

:addLine
  set "_myVar=%_myVar% %_auxVar%"
  rem                 ^ optional space by design
goto :eof