Solved Saving a variable to a specific line and retrieve it

April 28, 2013 at 02:22:23
Specs: Windows 7, 3.301 GHz / 8147 MB
Hello :)
I wonder if/how i could do this:
First i want to save a variable like %a% on a specific line in the same program. For example:
save %a% to line 2000
and after that i want to be able to retrieve it again like
set a=whatever is on line 2000 in the file

that would help me really much because i could use it to save settings and restore them in the same program. I want my biggest batch file to be like all in one.

Thanks for all answers :)


See More: Saving a variable to a specific line and retrieve it

Report •

✔ Best Answer
May 2, 2013 at 19:12:43
Batch is not really suited for random access retrieval. You can of course store and retrieve values, but trying to build the values into your batchfile code is very inefficient since the batchfile has to replace itself with appended data every time the data is stored. (ie: batch is sequential or block-mode access, not random). Better to just use a text data-storage file:
::=== begin batch
@echo off & setlocal enabledelayedexpansion
set /p r=retrieve?
if /i "%r%" equ "y" goto :retrieve
set c=-1
:1
set /a c+=1
set x%c%=
set /p x%c%=enter data %c%
if defined x%c% goto :1
echo saving data:
set x
pause
set x>temp
goto :eof
:retrieve
for /f "tokens=*" %%a in (temp) do set %%a
echo retrieved data:
set x
goto :eof
::==== end
althought this doesn't give you "line storage", it does give you a semblance of random-access storage by using the index of x
(x0, x1, x2 etc) to pull specific items from the data-clump.


#1
April 28, 2013 at 04:31:43
Do you mean to put the value into the batch file? [Have the batch modify itself.]

If so, that's playing with fire.

=====================
M2 Golden-Triangle


Report •

#2
April 29, 2013 at 06:59:02
Yes i want the batch file to modify itself.
If its possible i would very much like to know how. I have backups, so im not afraid of playing with fire :3

Report •

#3
April 29, 2013 at 07:41:50
http://blogs.msdn.com/b/oldnewthing...
Read this. Reflect on it and how it applies to your situation. Then report back.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
May 2, 2013 at 06:06:03
Quote:
http://blogs.msdn.com/b/oldnewthing...
Read this. Reflect on it and how it applies to your situation. Then report back.

It doesn't apply to my situation i think. I know some stuff in batch programming, but i have no idea on the subject I'm asking for in this thread. If that was what you were thinking about, i don't know "half the code", and i hope I've been clear enough on what i want to achieve :)


Report •

#5
May 2, 2013 at 07:04:06
Actually, that blog post does apply to your situation. The problem with it is that the lesson to be learned was obscured with the added commentary.

You have an XY problem. Meaning that you have problem X and have decided on a faulty solution Y and have asked us how to implement your faulty solution rather than asking how to solve the root problem.


Report •

#6
May 2, 2013 at 07:30:29
Hi FM,

long time no see

=====================
M2 Golden-Triangle


Report •

#7
May 2, 2013 at 07:54:05
Hi M2,

I poke my head in occasionally, but am finding less and less Perl related questions, which is as you know my language of choice.


Report •

#8
May 2, 2013 at 19:12:43
✔ Best Answer
Batch is not really suited for random access retrieval. You can of course store and retrieve values, but trying to build the values into your batchfile code is very inefficient since the batchfile has to replace itself with appended data every time the data is stored. (ie: batch is sequential or block-mode access, not random). Better to just use a text data-storage file:
::=== begin batch
@echo off & setlocal enabledelayedexpansion
set /p r=retrieve?
if /i "%r%" equ "y" goto :retrieve
set c=-1
:1
set /a c+=1
set x%c%=
set /p x%c%=enter data %c%
if defined x%c% goto :1
echo saving data:
set x
pause
set x>temp
goto :eof
:retrieve
for /f "tokens=*" %%a in (temp) do set %%a
echo retrieved data:
set x
goto :eof
::==== end
althought this doesn't give you "line storage", it does give you a semblance of random-access storage by using the index of x
(x0, x1, x2 etc) to pull specific items from the data-clump.

Report •

#9
May 6, 2013 at 01:52:17
Quote:
Actually, that blog post does apply to your situation. The problem with it is that the lesson to be learned was obscured with the added commentary.
You have an XY problem. Meaning that you have problem X and have decided on a faulty solution Y and have asked us how to implement your faulty solution rather than asking how to solve the root problem.


Now i understood what you were asking for :)
Well the root problem is probably that i want to be able to save stuff as passwords, colour settings etc. inside the batch file instead of saving it to another txt file or something like that. I don't want more files or subfolder or stuff like that. I want everything to be able to be done inside its own file.
The settings should be saved so i don't have to register a new password or changing the color settings everytime i start the file.


Report •

#10
May 6, 2013 at 03:11:12
You may as well accept that you'll need another file.

The good news is you need just one to store the info.

=====================
M2 Golden-Triangle


Report •

#11
May 6, 2013 at 06:14:24
Depending on the execution environment, look into SETX.

How To Ask Questions The Smart Way


Report •

#12
May 6, 2013 at 13:01:57
@Razor: wow, that's (setx) a new one for me, it's impressive! too bad it's not on xp.

@op: One good thing about a separate text file: if you want to hide passwords, at least you can zip it encrypted with a "master" password. You could also append it to a random media file (jpg, mp3, bmp, etc) to hide it. But of course, if you use your batchfile to extract the passwords, then the master password, (and the target file and methodology) will be plain for anyone to examine in the code unless you use a batch-encryptor.


Report •

Ask Question