How to read text files and execute the specified route on flutter

Viewed 33

Is there an option to read the text file where it says the route of a bat file? I need to execute that file which has a parameter that my program can give.

I know how to execute the bat file directly but I need to do it from a txt file. Because it will be on a online app and the user doesn't need that file directly.

Example:

firstRoute.txt -> C:\Users\Me\Documents\file.bat

Inside bat file

@ECHO OFF
set args1=%1
ECHO %args1%

Read&Write Files flutter

If someone asks how to execute a shell directly.

_loadBat(String args1) async {
  var shell = Shell();
  shell.run('.\\assets\\firstbat.bat $args1');
}

The txt file already associated on the assets folder and put it in the pubspec.yaml

I have the read file and I referenced. I'm using the path_provider bc the file it will be stored on documents directory

Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();

  return directory.path;
}

Future<File> get _localFile async {
  final path = await _localPath;
  
  return File('$path/counter.txt');
}

Future<int> readFile() async {
  try {
    final file = await _localFile;

    // Read the file
    final contents = await file.readAsString();

    return int.parse(contents);
  } catch (e) {
    // If encountering an error, return 0
    return 0;
  }
}
0 Answers
Related