0

-WHAT I NEED

How can I create a batch file that interprets external instructions (not batch code) and puts them in a variable?


-WHAT I GOT

A sourceforge project called "Easy-Series" (For easier debunking, here is the download link for every dependency as well as main file; Ignore crashing when you put external commands, will fix that later, but if you can fix it, Please mention it in the comments.)


-SOLUTIONS THAT I HAVE RULED OUT

set doesnt seem to work because I think it only grabs the first line of a file encoded in plain text format... Am I right? If not then you can theorize a solution...

-EXAMPLES EXPLAINED THOROUGHLY

Say I have a text file named commands.txt. I have a few EXTERNAL commands there to be INTERPRETED by an interpreter, of which I will code. I only need the "reading each line and put each line in separate variable" part. ONCE AGAIN, these commands are NOT batch commands, THESE ARE EXTERNAL COMMANDS which I need to be interpreted by an interpreter;

newfolder test
newfile test.txt
ping google.com

since I've already told you that this "Command Interpreter" called easycommandengine (Which aims for easier use of windows command line) interprets external instructions, I want each line to be put in a seperate variable

line 1 = ecl_ecmdengine_line%linecount%
line 2 = ecl_ecmdengine_line%linecount%
line 3 = ecl_ecmdengine_line%linecount%
line 4 = ecl_ecmdengine_line%linecount%
and so on and so forth

WHAT I MEAN BY THIS is THAT, say, LINE 1 of commands.txt which is newfolder test to be saved in variable "ecl_ecmdengine_line1"... 1 would be the %linecount% variable.

If you STILL don't understand (tl;dr?): I technically mean that I want a batch file to read .txt files with instructions the same way cmd reads batch files...

I'd also like the code to make %linecount% variable (which I suppose is already self-explanatory) work... I'm too newby to make for commands which just makes my mind blow up...


-NOTES

Once again, HERE are the files if you want to test my code out on your computer yourself...

If you think I have given too much or too few information, PM or Comment on this post, and I will try and fix it as soon as I can.

Voltaire
  • 93
  • 2
  • 9

3 Answers3

0

Test this - the last command will list the variables:

@echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%a in ("commands.txt") do (
set /a c+=1
set "command[!c!]=%%a"
)
set command[
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Works... so.. my mind technically thinking too hard that I forgot on how to pass these variables and execute them.. god damn it stupid me.... I need more help D: – Voltaire Feb 13 '14 at 11:30
0
@echo off

    setlocal enableextensions disabledelayedexpansion

    set "numLines=0"
    for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" "commands.txt"') do (
        set "ecl_ecmdengine_line%%a=%%b"
        set /a "numLines+=1"
    )

    for /l %%a in (1 1 %numLines%) do (
        call echo line %%a is :%%ecl_ecmdengine_line%%a%%
    )

    setlocal enabledelayedexpansion
    for /l %%a in (1 1 %numLines%) do (
        echo line %%a is :!ecl_ecmdengine_line%%a!
    )
    endlocal   

How to read lines from file (findstr is used to numerate the lines), and how to access its contents with or without delayed expansion enabled.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Works very nicely, can't decide which code to pick, foxidrive's or yours. – Voltaire Feb 13 '14 at 11:34
  • @EpicTonic, The two works. Select the code that you better understand or that better fits with the rest of your project. You will have to maintain it. – MC ND Feb 13 '14 at 12:14
  • i feel yours work fits with the code, since it works very nicely. – Voltaire Feb 13 '14 at 12:26
0

Previous answers from foxidrive and MC ND would solve your immediate problem, that is load lines from commands.txt into an array named ecl_ecmdengine_line. However, after that you need to process each one of the lines stored in the array, that is:

@echo off
setlocal EnableDelayedExpansion

rem Load lines from commands.txt into ecl_ecmdengine_line array:
set numLines=0
for /F "delims=" %%a in (commands.txt) do (
   set /A numLines+=1
   set "ecl_ecmdengine_line!numLines!=%%a"
)

rem Process the lines in ecl_ecmdengine_line array:
for /L %%i in (1,1,%numLines%) do echo process line %%i: !ecl_ecmdengine_line%%i!

However, previous method seems a waste to me. You may read the lines from commands.txt file and directly process them with no necessity to store they in any array. The Batch code below is an example of this method that also show how the commands could be processed:

@echo off

for /F "tokens=1*" %%a in (commands.txt) do call %%a %%b
goto :EOF

:newfolder
echo Creating newfolder for %*
exit /B

:newfile
echo Creating newfile for %*
exit /B

:ping
echo Executing ping with %*
exit /B
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108