Here's my code, the description of what it does is below it.
Code: Select all
:: Batch script: convert plaintext coordinates into arduino-usable .ino file
@ECHO off
SETLOCAL enabledelayedexpansion
(
:: Create a line counter
SET /a line=0
:: Opening of the .ino file
ECHO.float waypoints[][3] = {
:: Load file line-by-line, commas as delimiters
FOR /f "tokens=1,2,3 delims=," %%a IN (path.txt) DO (
:: Increment line count (unused)
SET /a line+=1
SET x=%%a
:: Remove any spaces
SET x=!x: =!
SET y=%%b
:: Remove any spaces
SET y=!y: =!
SET z=%%c
:: Remove any spaces
SET z=!z: =!
ECHO.{!x!, !y!, !z!},
)
:: Close the .ino file
ECHO.};
)>path.ino
* * * * *
I'm very new to batch scripting. Right now I have a .bat that takes a file "path.txt," formats each line, and writes the output to "path.ino" in a format that Arduino can compile. Each line of "path.txt" is supposed to have three comma-separated numbers (extra spaces are stripped, the comma is the delimiter). So, for example, if "path.txt" looked like
Code: Select all
0.0, 0.0, 0.0
1.0, -1.3, -0.6
2 , 41, -3.14
then "path.ino" would look like
Code: Select all
float waypoints[][3] = {
{0.0, 0.0, 0.0},
{1.0, -1.3, -0.6},
{2, 41, -3.14},
};
The script I pasted above works fine for this purpose. HOWEVER, I'm trying to add some type of error handling--that is, if the script encounters a line with fewer than three numbers, or if there are non-numeric characters present, I'd like to be able to tell the user "Problem occurred on line #[whatever]", and abort the script (without creating path.ino). How can this be done?
I'm sorry I don't have any attempts to show you right now. I am still struggling to plan out the next step, I thought I'd go ahead and ask before I make my own attempt.
Let me know what else I can contribute!
Thanks,
Sam