How do i make a text file with text in it with CMD

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

How do i make a text file with text in it with CMD

#1 Post by Ranguna173 » 05 Aug 2011 13:21

Hi!
I want to know how to make a batch that when opened it creates a .txt file with text in it by himself..
I write what i want the text file to contain in the program I'm using to create the batch (notepad ++), and once saved and executed it creates a .txt file with a line of text in it..
I know some ways, but none execute by themselves, like:

Code: Select all

copy con

Whenever i use "copy con" command i have to type manually the text for the file..

Can someone help me?
Plz, if you want more information just ask!

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How do i make a text file with text in it with CMD

#2 Post by Ed Dyreen » 05 Aug 2011 13:40

'
like

Code: Select all

@echo off

> "redirect.CMD" (
   ::
   echo.@echo off
   echo.
   echo.echo.This works
   echo.pause
   echo.exit /b
)
call "redirect.CMD"
pause
exit /b
:?:

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: How do i make a text file with text in it with CMD

#3 Post by Ranguna173 » 05 Aug 2011 16:32

Ed Dyreen wrote:'
like

Code: Select all

@echo off

> "redirect.CMD" (
   ::
   echo.@echo off
   echo.
   echo.echo.This works
   echo.pause
   echo.exit /b
)
call "redirect.CMD"
pause
exit /b
:?:


Wow, that was a fast reply! :shock: And..

No it's kind of not like that...
I was thinking on using this on a "Sava Game" System..
I'm making a game with CMD but when i close the cmd window all variable go "0"
I put the option "save" on the menu, so once you save and you close the window then open it again you can load your game..

I want the window to open and close automatically and once closed a txt file will be created with text inside..
If the code you said was like that it would work like that (People can't see what is going on):

Code: Select all

:: %CRED% is the variable that contains the money..
:: Lets say that %CRED% equals 2000
:: --Saving the Game--
> "money.txt" (
   ::
   echo.%CRED%
)
exit
:: That means that the text in the first line of the "money.txt" file is 2000
::--------------------------
:: The window was closed and now it's opened, that means that %CRED% equals 0
:: --Loading the Game--
set /p CRED=<money.txt
goto main_menu
:: Now the %CRED% has changed from 0 to 2000

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How do i make a text file with text in it with CMD

#4 Post by Ed Dyreen » 05 Aug 2011 18:35

'
A game in DOS :shock:

Ever heard of DarkBasic 8)

I'd like to help but I have to regenerate now, we are Borg... :mrgreen:

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: How do i make a text file with text in it with CMD

#5 Post by Ranguna173 » 05 Aug 2011 19:02

Ed Dyreen wrote:'
A game in DOS :shock:

Ever heard of DarkBasic 8)

I'd like to help but I have to regenerate now, we are Borg... :mrgreen:


:lol:
Sorry mate, but I've never watched Star Trek...

Ohh well...

Thanks anyways, I don't want to learn another programming language (not for now)...
Have to find out by myself...

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: How do i make a text file with text in it with CMD

#6 Post by Ranguna173 » 05 Aug 2011 19:04

Ed Dyreen wrote:'
A game in DOS :shock:

Ever heard of DarkBasic 8)

I'd like to help but I have to regenerate now, we are Borg... :mrgreen:


:lol:
Sorry mate, but I've never watched Star Trek...

Ohh well...

Thanks anyways, I don't want to learn another programming language (not for now)...
Have to find out by myself...

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: How do i make a text file with text in it with CMD

#7 Post by Ranguna173 » 05 Aug 2011 19:45

Got it!!

Code: Select all

 echo hello > file.txt
 set CRED=<file.txt
 echo %CRED%
 


What you will see:

Code: Select all

 hello
 


That was it... It took me a reeaaaly lononononononong while to figure that out!!!!

Thx Ranguna174 you realy helped me!!
Thx Ed Dyreen you realy helped too!!!!

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: How do i make a text file with text in it with CMD

#8 Post by orange_batch » 08 Aug 2011 06:58

The correct form for processing all lines of a text file is for /f...

Code: Select all

setlocal enabledelayedexpansion

for /f "delims= usebackq" %%a in ("text file path") do (
set /a counter+=1
set variable_!counter!=%%a
)

for /l %%a in (1,1,%counter%) do echo:!variable_%%a!

Read for /?.

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: How do i make a text file with text in it with CMD

#9 Post by taripo » 09 Aug 2011 06:28

howcome the code that OP guy mentioned worked doesn't work for me, XP

C:\>echo hello > file.txt

C:\>set CRED=<file.txt

C:\>echo %CRED%
%CRED%

he said you'd see hello and it worked for him..
i'd probably do something like
C:\>for /f %f in ('echo hello') do set a=%f
C:\>for /f "delims=" %f in ('echo hello h') do set a=%f
But i'm curious why the one he wrote as a solution worked for him but not on my computer.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How do i make a text file with text in it with CMD

#10 Post by Ed Dyreen » 09 Aug 2011 12:41

'
howcome the code that OP guy mentioned worked doesn't work for me, XP
He made a typo :wink:

Code: Select all

set /p CRED=< "file.txt"

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: How do i make a text file with text in it with CMD

#11 Post by Ranguna173 » 09 Aug 2011 18:21

orange_batch wrote:The correct form for processing all lines of a text file is for /f...

Code: Select all

setlocal enabledelayedexpansion

for /f "delims= usebackq" %%a in ("text file path") do (
set /a counter+=1
set variable_!counter!=%%a
)

for /l %%a in (1,1,%counter%) do echo:!variable_%%a!

Read for /?.

:shock: :shock: :shock:
Ummm.....
I'll stick with mine... :wink:
Thanks...


taripo wrote:howcome the code that OP guy mentioned worked doesn't work for me, XP


Lol... yeah.. I was probably high when i was writing that... :D


Ed Dyreen wrote:'
He made a typo :wink:

Code: Select all

set /p CRED=< "file.txt"


Thx for the correction! :wink:

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: How do i make a text file with text in it with CMD

#12 Post by Ranguna173 » 09 Aug 2011 18:26

orange_batch wrote:The correct form for processing all lines of a text file is for /f...

Code: Select all

setlocal enabledelayedexpansion

for /f "delims= usebackq" %%a in ("text file path") do (
set /a counter+=1
set variable_!counter!=%%a
)

for /l %%a in (1,1,%counter%) do echo:!variable_%%a!

Read for /?.

Right now i don't need that type of code because i only need a line of the file...
I kind of didn't get the code but I'll try to understand it later(it may be useful)

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: How do i make a text file with text in it with CMD

#13 Post by Acy Forsythe » 10 Aug 2011 16:49

Ranguna173 wrote: Right now i don't need that type of code because i only need a line of the file...
I kind of didn't get the code but I'll try to understand it later(it WILL be useful)


I fixed that for you.

That code loops through every line of "text file path" and for each line in the file it sets %%a to the contents of that line. It then increases the number used for the variable and sets the numbered variable = %%a.

Essentially that code is saying:

SET Variable_1=Fileline1
SET Variable_2=Fileline2
SET Variable_3=Fileline3

The second loop just echos the values so you can see how it works using a working example, you just have to substitute "text file path" with something like "C:\mysavedata.txt".

etc... But once you understand that code, you can go even further with it.

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: How do i make a text file with text in it with CMD

#14 Post by taripo » 11 Aug 2011 04:40

Looking at orange_batch's batch file, it brings up a question I have about batch files generally

if you have this line
setlocal enabledelayedexpansion
is it an absolute necessity when you have the default /v:off and want to use !var!?

and then some variables are created..

You can't then make those variables persist after the batch file is finished or after endlocal.

or can you?
and how do you get around that problem?
surely that bat file in using that setlocal enabledelayedexpansion line, is doing so to be portable, so it can run on a /v:off prompt./ but those environment variables aren't going to persist, and what if one wants them to?

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: How do i make a text file with text in it with CMD

#15 Post by Ranguna173 » 13 Aug 2011 13:05

Acy Forsythe wrote:
Ranguna173 wrote: Right now i don't need that type of code because i only need a line of the file...
I kind of didn't get the code but I'll try to understand it later(it WILL be useful)


I fixed that for you.

That code loops through every line of "text file path" and for each line in the file it sets %%a to the contents of that line. It then increases the number used for the variable and sets the numbered variable = %%a.

Essentially that code is saying:

SET Variable_1=Fileline1
SET Variable_2=Fileline2
SET Variable_3=Fileline3

The second loop just echos the values so you can see how it works using a working example, you just have to substitute "text file path" with something like "C:\mysavedata.txt".

etc... But once you understand that code, you can go even further with it.


Thx,
That
SET Variable_1=Fileline1
SET Variable_2=Fileline2
SET Variable_3=Fileline3

I think i get it now...

I'm gonna try to use it again....
Right now I'm saving 1 variable into each text file...

With this code i can load multiple lines right?
So That means...
If i save more than one variable in a single file, i can load them with this and instead of having 4 notepads i can have 1 with every thing...

Thanks man!

Post Reply