| 1. |
Solve : make batch save %var% to .txt and retrieve %var% from .txt? |
|
Answer» i have this program that can run on the command line (goes faster that way) and i have a .bat that makes it easier to do this what i want is to save where it it up to when it exits using a .txt file so that it has an input file and i have this program that can run on the command line (goes faster that way) and i have a .bat that makes it easier to do this what i want is to save where it it up to when it exits using a .txt file so that it has an input file and Ok... echo %var%>filename.txt rem The above line sends the contents of %var% to filename.txt set /p var=rem The above line recieves the contents of filename.txt and sets %var% as it. Understand?it is not writeing to the file but it is reading from it this is my batch Code: [Select]@echo off :setup0 echo do you want to start from where you left off[y\n] set/p "cho=>" if %cho%==y goto setup1 if %cho%==n goto setup2 :setup1 cls echo time set/p "time=>" echo input set /p in=<filename.txt echo %out% echo output set/p "out=>" echo are the time,input and output correct[y/n] set/p "cho=>" if %cho%==y goto evolve_b if %cho%==n goto setup %out%>filename.txt :setup2 cls echo time set/p "time=>" echo input set/p "in=>" echo output set/p "out=>" echo are the time,input and output correct[y/n] set/p "cho=>" if %cho%==y goto evolve_b if %cho%==n goto setup :evolve_b cd C:\Program Files\Evolve evolve_batch evolve_batch s %time% %in%.evolve %out%.evolve %out% is the variable i want to save and i want to make %in% have the value of %out% when i start it againThe easiest way to retrieve a value from a file is Set /P in= Note this will read only the first line of filename.ext and will put the value into %in% Grahamit now reads from the file and saves to the file but not with the value of %out% instead it deletes all the text in the .txt Code: [Select]@echo off & setlocal :setup0 if not exist save.txt goto setup2 echo do you want to start from where you left off[y\n] set/p "cho=>" if %cho%==y goto setup1 if %cho%==n goto setup2 goto:eof :setup1 for /f "tokens=*" %%a in (save.txt) do %%a set out goto:eof :setup2 set/p out=enter string: echo variable out=%out% will be saved as save.txt >save.txt echo set out=%out% sample untested code@ Reno where do you put the code in the batch every time i answer yes to the "do you want to start from where you left off[y\n]" it exits the batch Code: [Select]@echo off & setlocal :setup0 if not exist save.txt goto setup3 echo do you want to start from where you left off[y\n] set/p "cho=>" if %cho%==y goto setup1 if %cho%==n goto setup2 goto:eof :setup1 cls for /f "tokens=*" %out% in (save.txt) do %out% echo time set/p "time=>" echo input echo %out% echo %out% echo output set/p "out=>" echo are the time,input and output correct[y/n] set/p "cho=>" if %cho%==y goto evolve_b if %cho%==n goto setup :setup2 cls echo time set/p "time=>" echo input set/p "in=>" echo output set/p "out=>" echo are the time,input and output correct[y/n] set/p "cho=>" if %cho%==y goto evolve_b if %cho%==n goto setup :setup3 set/p out=enter string: echo variable out=%out% will be saved as save.txt >save.txt echo set out=%out% :evolve_b cd C:\Program Files\Evolve evolve_batch evolve_batch s %time% %out%.evolve %out%.evolve i changed it a bit it would not fit i got headache i only give example how to save a bunch of variables into a txt file and later retrieve the vars the next time batch is executed. for /f "tokens=*" %out% in (save.txt) do %out% <--- incorrect for loop syntax :setup1 <--- let's say this is sort of a "batch function" in OOP term, you have to put goto:eof at the end of each function except the last one, or the the next line of code will be executed Code: [Select]:evolve_b cd C:\Program Files\Evolve evolve_batch evolve_batch s %time% %out%.evolve %out%.evolvewhat are the value of var time,out,evolve,out on the following 2 condition: 1.do you want to start from where you left off[y\n] Y 2.do you want to start from where you left off[y\n] N it would be nice if you can gives example as i don't quite understand the problem. Quote what are the value of var time,out,evolve,out on the following 2 condition:1) time is hours(h),minutes(m),SECONDS(s) or steps(u) so when i type in the time it is 45s(seconds) 30m(minutes) 24h(hours) 4000u(steps) in is the starting conditions of the simulation (in this case it is the previous out file) .evolve is not a variable it is just the files extension out is the output file 2)in this case i set the varables to anything i want to. hmm, maybe something like below works?? Code: [Select]@echo off & setlocal :setup set/p c=do you want to start from where you left off [y\n] : echo.%c%|findstr/ix "y n yes no">nul || goto:setup if /i %c:~,1%.==y. for /f "tokens=*" %%a in (save.txt) do set %%a if /i %c:~,1%.==n. ( set/p var_time=time : set/p var_in=input : set/p var_out=output : set var_>save.txt ) :validateYesNo echo evolve_batch s %var_time% %var_in%.evolve %var_out%.evolve set/p c=are the time,input and output correct [y/n] : echo.%c%|findstr/ix "y n yes no">nul || goto:validateYesNo if /i %c:~,1%.==n. goto:setup cd C:\Program Files\Evolve evolve_batch evolve_batch s %var_time% %var_in%.evolve %var_out%.evolvewow Reno that one looked very good but i wanted it to set %var_in% to what %var_out% was last timeCode: [Select]@echo off & setlocal & set c=n if not exist save.txt goto skip :setup set c=&set/p c=do you want to start from where you left off [y\n] : echo.%c%|findstr/ix "y n yes no">nul || goto:setup :skip if /i %c:~,1%.==y. for /f "tokens=*" %%a in (save.txt) do set %%a if /i %c:~,1%.==n. ( set/p var_time=time : set/p var_in=input : ) set/p var_out=output : :validateYesNo echo evolve_batch s %var_time% %var_in%.evolve %var_out%.evolve set c=&set/p c=are the time,input and output correct [y/n] : echo.%c%|findstr/ix "y n yes no">nul || goto:validateYesNo if /i %c:~,1%.==n. goto:setup >save.txt echo var_time=%var_time% >>save.txt echo var_in=%var_out% cd C:\Program Files\Evolve evolve_batch evolve_batch s %var_time% %var_in%.evolve %var_out%.evolve CODE UPDATED. CHECK POST #11i wanted it to set %var_in% to what %var_out% was last time it was ran so //first run %var_in%=1 %var_out%=2 //second run %var_in%=2(from save.txt{so i don't need to enter this}) %var_out%=3 //third run %var_in%=3(again from save.txt) %var_out%=4 and so on perfect THANK you so much |
|