2

Im trying to make a batch-file Encoder, my current problem is that i dont know how to take a text file, and put all the lines into 1 variable

so if my text file is

Hello,

my name is

Benjamin

The variable will be either "Hello,my name isBenjamin" (without Spaces between the lines)

or "Hello, my name is Benjamin" (With Spaces between the lines)

The code is

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

Also im using textfiles to do the encryption, because im not advanced enough yet, so the text file a.txt would contain the encrypted letter a

Benjamin
  • 21
  • 3
  • Batch files are awkward with new lines inside variables, usually `for /f` loops are used to process the files. What exactly are you going to do with that variable? Can you post the complete/relevant code? – wOxxOm Nov 20 '15 at 10:25
  • Have you tried "Hello,\nmy name is\nBenjamin" ? – kayosoufiane Nov 20 '15 at 10:29
  • @kayosoufiane, why do you think this will work in a batch file which is a very primitive Windows scripting language that doesn't understand `\n`? – wOxxOm Nov 20 '15 at 10:39
  • @wOxxOm i tried this (echo $'Hello,\nmy name is\nBenjamin') and assumed it will work – kayosoufiane Nov 20 '15 at 10:50
  • 1
    @kayosoufiane, it doesn't work, see the description of the `batch-file` tag. – wOxxOm Nov 20 '15 at 10:54

2 Answers2

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

see for /, setlocal /?, set /? and delayed Expansion

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
2

Next script covers next to any file content - even cmd- and batch-poisonous characters %, !, <, >, |, ^, ...

Important: note advanced set syntax with double quotes 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

Resources (required reading, incomplete):

JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Will fail with many different contents. First challenge `;hello` – jeb Nov 23 '15 at 21:00
  • `for /F usebackq^ tokens^=*^ eol^= %%G in ("D:\bat\SO\files\33824203.txt") do (` to treat `;hello` (@jeb's pristine comment). That's why I wrote _next to any_ in sense [**next to** = almost; nearly](http://dictionary.reference.com/browse/next--to?s=t); neither _any_, neither _all_. I suppose sentences under some simple grammar rules, e.g. `Hello, Benjamin!` or `I hate 21 % VAT!` or `Buy it at Jeb&Co!` etc. :) – JosefZ Nov 23 '15 at 21:27
  • And I thought about simple sentences like `Jeb&Co said: "Hello Cat&Dog"` :-) But at all, your solution is nice and should work in the most cases – jeb Nov 23 '15 at 22:26