批处理文件,多行成单变量

时间:2015-11-20 10:20:43

标签: batch-file

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

所以如果我的文本文件是

Hello,

my name is

Benjamin

变量将是"您好,我的名字是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.

我也使用textfiles进行加密,因为我还没有足够高级,所以文本文件a.txt会包含加密的字母a

2 个答案:

答案 0 :(得分:3)

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

for /setlocal /?set /?delayed Expansion

答案 1 :(得分:2)

下一个脚本涵盖任何文件内容旁边 - 甚至是cmd-和批处理有毒字符%!<>|^,...

重要提示:请注意使用双引号set的高级set "varname=varvalue"语法。

@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

资源(必读,不完整):