30

I'm trying to open a file in it's default editor after the user has created the file. So far my script is:

@echo off
@echo --- Create A New File ---
@echo -
@echo Where should we put the new file?
set /p fileLocation=@ %UserProfile%\
@echo -
@echo What do you want to call your new file?
set /p fileName=@ 
@echo -
@echo Almost Done! What is the files extension?
set /p extension=@ .
@echo -
copy NUL "%UserProfile%\%fileLocation%\%fileName%.%extension%"

(ignore the extra echos and '@' those are just for fun)

After I click the file, it does the command: Choose Location > Choose File Name > Choose File extension. I'm almost done on what I want but theres one last thing. How can I get the file name that I created and then open in its default text-editor?

Mchl
  • 61,444
  • 9
  • 118
  • 120
omnix
  • 1,839
  • 7
  • 23
  • 34
  • Perhaps see also https://stackoverflow.com/questions/48051864/how-to-get-the-default-application-mapped-to-a-file-extention-in-windows-using-p – tripleee Oct 24 '22 at 11:47

4 Answers4

45

You can use start to open the file with the associated application.


Resources :

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • How can I get the created file and then use that? – omnix Sep 16 '10 at 17:39
  • You already have your file name, its `"%UserProfile%\%fileLocation%\%fileName%.%extension%"` so just start it. – Colin Hebert Sep 16 '10 at 17:42
  • in your script; `start "%UserProfile%\%fileLocation%\%fileName%.%extension%"`. – Colin Hebert Sep 16 '10 at 17:45
  • Added at the end, it opens up another cmd, not the file. – omnix Sep 16 '10 at 17:48
  • 1
    Then you should use `assoc` before to associate an extension to a file type and `ftype` to associate a file type to an application. – Colin Hebert Sep 16 '10 at 17:54
  • can you put that together for me? I really do not understand it :( thanks so much for helping – omnix Sep 16 '10 at 17:57
  • `assoc .myExt = MyNewFileType` and `FTYPE MyNewFileType=C:\NotePad.exe` will associate the ".myExt" extension to NotePad.exe, if you do `start anyFile.myExt` it will open it in NotePad.exe – Colin Hebert Sep 16 '10 at 18:06
  • How would I insert that in the script? – omnix Sep 16 '10 at 18:11
  • 1
    The thing is you can't really do that. You must know the file type association before starting your application. So you must manually execute these commands to do the association first. Then you'll be able to call the start method anytime – Colin Hebert Sep 16 '10 at 18:20
  • Hm, how about.. get the fileLocation that I entered and then use the start command? – omnix Sep 16 '10 at 18:29
  • If it didn't work, it's because your file extension wasn't associated with an application, not because of your file location – Colin Hebert Sep 16 '10 at 18:36
24

In windows you can use start (http://ss64.com/nt/start.html).

start "" "%UserProfile%\%fileLocation%\%fileName%.%extension%"
Oliver
  • 35,233
  • 12
  • 66
  • 78
10

You can also use explorer.exe/explorer to open the file (e.g. explorer file.txt). This also works nicely if you use WSL, especially with an alias like alias open="explorer.exe" so you can just call it like, e.g., open file.txt.

Nathan
  • 9,651
  • 4
  • 45
  • 65
  • I was using a lot this procedure (also from Cobol) but I saw that for every run it creates a new explorer.exe process and probably won't close when exiting the application. Check TaskManager. – Ivan Ferrer Villa May 15 '20 at 14:06
  • Hm, that's not the case for me. I tried opening several files this way, and I didn't see any explorer.exe processes in task manager. – Nathan Jun 13 '20 at 15:48
2

I achieved the correct way of FILE ASSOCIATION using these cmd commands. this is just an example:

REG ADD "HKEY_CLASSES_ROOT\Applications\notepad++.exe\shell\open\command" /v @ /t REG_SZ /d "\"C:\\Program Files\\Noteepad++\\notepad++.exe\" \"%1\"" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt" /v "Application" /t REG_SZ /d "notepad++.exe" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList" /v "g" /t REG_SZ /d "notepad++.exe" /f

assoc .txt=MyCustomType
ftype MyCustomType="C:\Program Files\Noteepad++\notepad++.exe" "%1"

(it's better to put them in .bat file)

T.Todua
  • 53,146
  • 19
  • 236
  • 237