-1

I have a file:

myfile.txt

And a batchfile

mybat.bat

And I want to open myfile.txt with mybat.bat.
In mybat.bat, I would have something like this:

start "filepath"

But, how do I get the file path of myfile.txt (The file I opened with the batch file)?

Edit: What I mean in this question is that when you have .exe files, for example notepad.exe, You open files with that program. What I would like to do is open a file with my batch file, and in the batch file have some code that does something with the file opened with it. So, to do something with the file opened with the batch file, I neeed the file path of the file opened with my batch file.

kirtan-shah
  • 405
  • 7
  • 21
  • You copy and paste in the file path from Windows Explorer...like `start "C:/path/to/file/myfile.txt"` – Mingle Li Sep 19 '15 at 15:51
  • That won't work if I have a file called myfile2.txt, I want it so that ANY file opened with my bat file will be opened. (I don't want a hard-coded value) – kirtan-shah Sep 19 '15 at 16:05
  • 3
    possible duplicate of [How to pass command line parameters to a batch file?](http://stackoverflow.com/questions/26551/how-to-pass-command-line-parameters-to-a-batch-file) – wOxxOm Sep 19 '15 at 17:50
  • Open a command prompt window, execute `call /?` and read help of this command explaining how to evaluate parameters of a batch file or a subroutine which both can be called with command __call__. – Mofi Sep 19 '15 at 18:34
  • 1
    Am I the only one struggling to understand what OP wants? @htmlapps, is the text file in the same directory as the batch script? – rojo Sep 19 '15 at 20:40
  • 1
    @TheJuniorProgrammer: Note that start takes a title as the first parameter, so it is `start "title" myfile.txt` or `start "title" %1` what OP wants. – Thomas Weller Sep 19 '15 at 23:42
  • I _think_ you're looking for `for /f "delims=" %%A in (myfile.txt)` – SomethingDark Sep 20 '15 at 00:02

2 Answers2

0

When you open with a batch file, the file path and name is usually passed as the first argument, so you would use %1 to get the path and name to execute it:

@call %1
loadingnow
  • 597
  • 3
  • 20
-1

you don't "open" Textfiles with Batch, you read them.

setlocal enabledelayedexpansion
rem get filename (given as parameter):
set filename=%1
rem read file line by line:
for /f "delims=" %%a in (%filename%) do (
  set line=%%a
  rem do something with the line:
  set line=!line:e=a!
  rem write (changed) line:
  echo(!line!
)

Call your batchfile with the name of the textfile as parameter:

mybatch.bat myfile.txt
Stephan
  • 53,940
  • 10
  • 58
  • 91