Page 1 of 1
How do i make a text file with text in it with CMD
Posted: 05 Aug 2011 13:21
by Ranguna173
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:
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!
Re: How do i make a text file with text in it with CMD
Posted: 05 Aug 2011 13:40
by Ed Dyreen
'
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
Re: How do i make a text file with text in it with CMD
Posted: 05 Aug 2011 16:32
by Ranguna173
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!
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
Re: How do i make a text file with text in it with CMD
Posted: 05 Aug 2011 18:35
by Ed Dyreen
'
A game in DOS
Ever heard of DarkBasic
I'd like to help but I have to regenerate now, we are Borg...
Re: How do i make a text file with text in it with CMD
Posted: 05 Aug 2011 19:02
by Ranguna173
Ed Dyreen wrote:'
A game in DOS
Ever heard of DarkBasic
I'd like to help but I have to regenerate now, we are Borg...
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...
Re: How do i make a text file with text in it with CMD
Posted: 05 Aug 2011 19:04
by Ranguna173
Ed Dyreen wrote:'
A game in DOS
Ever heard of DarkBasic
I'd like to help but I have to regenerate now, we are Borg...
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...
Re: How do i make a text file with text in it with CMD
Posted: 05 Aug 2011 19:45
by Ranguna173
Got it!!
Code: Select all
echo hello > file.txt
set CRED=<file.txt
echo %CRED%
What you will see:
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!!!!
Re: How do i make a text file with text in it with CMD
Posted: 08 Aug 2011 06:58
by orange_batch
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 /?.
Re: How do i make a text file with text in it with CMD
Posted: 09 Aug 2011 06:28
by taripo
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.
Re: How do i make a text file with text in it with CMD
Posted: 09 Aug 2011 12:41
by Ed Dyreen
'
howcome the code that OP guy mentioned worked doesn't work for me, XP
He made a typo
Re: How do i make a text file with text in it with CMD
Posted: 09 Aug 2011 18:21
by Ranguna173
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 /?.
Ummm.....
I'll stick with mine...
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...
Ed Dyreen wrote:'
He made a typo
Thx for the correction!
Re: How do i make a text file with text in it with CMD
Posted: 09 Aug 2011 18:26
by Ranguna173
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)
Re: How do i make a text file with text in it with CMD
Posted: 10 Aug 2011 16:49
by Acy Forsythe
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.
Re: How do i make a text file with text in it with CMD
Posted: 11 Aug 2011 04:40
by taripo
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?
Re: How do i make a text file with text in it with CMD
Posted: 13 Aug 2011 13:05
by Ranguna173
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!