3

Best title of which I could think. Here's the issue. I have one batch file that I use to give me a directory of user log files. I pass the beginnings of a user name to the file. DIR lists all the files in the specified directory that match; the batch file then terminates. I have a similar batch file to call machine log files. I pass a computer name to that batch file, it opens the corresponding text file in Notepad...and it sits there waiting for me to close the text file before it terminates. Doesn't seem to me that such should be the proper behavior. Here's the wonky batch file:

@echo off
o:
cd \[location]
%1.txt
exit /b

Is there some way to get this batch to terminate after it opens the text file; or why does it wait for the text file to close before it ends?

2
  • start %1.txt should do it – jez Apr 15 at 17:49
  • Thank you. I went with @DavidPostil's response as my answer because of the caveat on leaving out the title. – kgm4714 Apr 15 at 18:04
2

Is there some way to get this batch to terminate after it opens the text file?

Yes. Change your batch file to the following:

@echo off
o:
cd \[location]
start "" %1.txt
exit /b
  • start - Start a program, command or batch script, opens in a new window.

  • "" is to ensure an empty title

    Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes "" According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.

why does it wait for the text file to close before it ends?

It waits because the program called by %1.txt (usually notepad) is running in the context of the current command shell. The current shell won't continue until the called program exits.


Further Reading

2
  • Oh, wait a minute. That was weird. Initially, upon responding to the notification, I saw the same text as before. Now that I sent my response, I see a change. Hang on. – kgm4714 Apr 15 at 17:57
  • OK, @DavidPostil, that worked. Thank you. – kgm4714 Apr 15 at 18:02
2

You can use:

@start "..\%~1" /B Notepad "%~DPN1.txt"

The "%~dpn1.txt means:

~D ==> Drive:           ==> O:
~P ==> \path\Location\  ==> [\Location\Folder\To\File]
~N ==> Name             ==> FileName

:: The eXtension ::
~X  ==> .txt            ==> .eXtension
~NX ==> ~NX1            ==> FileName.txt  

Start "..\%~nx1" /b Notepad "%~DPNX1.txt"

%~DPNX1 is the same %~F1:
Full/Path/To/File.eXtension ==> O:\Folder\To\FileName.txt  

You can replace Notepad with your favorite editor ...

Start "..\%~1" /b "C:\Program Files (x86)\Sublime Text\Sublime.exe" "%~DPN1.txt"

For use with the Default editor:

@start "[location]\%~1.txt" /b "%~dpn1.txt"

Ref: start /?

3
  • Thank you. Eventually, all the responses came down to a variation of this one; as the answer to my query, I picked what seemed to me the simpler one. – kgm4714 Apr 15 at 18:03
  • @kgm4714 You did it well, which makes it easier to maintain/edit. That is a choice, personal, and I would (and do) the same in other technologies ... – It Wasn't Me Apr 15 at 18:08
  • Thank you, also, for the link to the page on the start command. Very informative. – kgm4714 Apr 15 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.