open a .txt with a .bat then close cmd window?


Recommended Posts

I have created a batch file that creates a listing of files in a folder and saves it to a text file. I then added this batch file to the context menu for folders, so that I can right click on a folder and get there is an option to save a list of all the files in that folder. Here is the .bat file

cd %1

dir /a /b /-p /o:GEN >"C:\Documents and Settings\<username>\Local Settings\Temp\filelist.txt"

copy "C:\Documents and Settings\<username>\Local Settings\Temp\filelist.txt" %1

del "C:\Documents and Settings\<username>\Local Settings\Temp\filelist.txt"

%1\filelist.txt

My only problem is that after that last line opens the txt file, the cmd window stays open. How do I get the batch file to close the cmd window? I have tried "stop", "exit", "end" but none of them does it.

you might need to put quotes around it like:

start notepad "%1\filelist.txt"

It's working for me with my limited testing. My test was simply this tho...

dir

start notepad "c:\test.txt"

exit

You may have an issue that im not sure how to get around without something tricky, and that is the fact that %1 already has some quotes in it. you may need to do away with the quotes and use this instead:

dir /a /b /-p /o:GEN > C:\Docume~1\<username>\Locals~1\Temp\filelist.txt

then attach the quotes again here:

start notepad "%1\filelist.txt"

Good luck!, i hope this works for you.

Ok. That makes sense. I'm sure I'll get it working now.

One more question, though. Is there a way to use the %USERPROFILE% variable? When I simply replace <username> with it, all the batch file does is run the first line.

Edit:

Actually using the "Temp" variable would make more sense. How would I do that?

I tried this but I still get "access is denied" from notepad

cd %1

dir /a /b /-p /o:GEN > %temp%\filelist.txt"

copy %temp%\filelist.txt" %1

del %temp%\filelist.txt"

start notepad %1\filelist.txt

exit

Edited by unknownsoldierX

I believe that's happening because %USERPROFILE% actually returns "C:\documents and settings\username"

so replace:

dir /a /b /-p /o:GEN > C:\Docume~1\<username>\Locals~1\Temp\filelist.txt

with:

dir /a /b /-p /o:GEN > C:\Docume~1\%USERNAME%\Locals~1\Temp\filelist.txt

i think %USERNAME% is the key

Ok. Now notepad pops up saying "can not find the .txt file. Do you want to create a new one?" The filelist.txt is created in the folder, but the start notepad "%1\filelist.txt" line is not working.

Here is my current .bat file

cd %1

dir /a /b /-p /o:GEN > C:\Docume~1\%USERNAME%\Locals~1\Temp\filelist.txt

copy C:\Docume~1\%USERNAME%\Locals~1\Temp\filelist.txt %1

del C:\Docume~1\%USERNAME%\Locals~1\Temp\filelist.txt

start notepad "%1\filelist.txt"

exit

Yup the quotes within the %1 are the problem and I don't know of an easy way to remove them. Here is my proposed solution:

cd %1

dir /a /b /-p /o:GEN > C:\Docume~1\%USERNAME%\Locals~1\Temp\filelist.txt

copy C:\Docume~1\%USERNAME%\Locals~1\Temp\filelist.txt %1

copy C:\Docume~1\%USERNAME%\Locals~1\Temp\filelist.txt c:\ /Y

del C:\Docume~1\%USERNAME%\Locals~1\Temp\filelist.txt

start notepad "c:\filelist.txt"

exit

This way you'll get a copy of the file in the actual folder and one that you get to view right away from the C drive where we don't have to deal with the quote issue. /Y is suspressing the overwrite request.

I have decided to simplify things by just saving filelist.txt to the desktop. I wish there was a way to name the text file using a variable, naming it "music_filelist.txt" for example. Using %1_filelist.txt doesn't work.

cd %1

dir /a /b /-p /o:GEN > C:\Docume~1\%USERNAME%\Desktop\filelist.txt

start notepad "C:\Docume~1\%USERNAME%\Desktop\filelist.txt"

exit

This batch file is good enough for me, as I normally just copy and paste lines from that text file and then delete it. An ideal solution would be if I could have a batch file, or something else that could be ran from the context menu, that took the output and pasted it into notepad without creating a txt file.

  Nightburn said:

Well if batch files continue to fail you, you could try a program such as AutoHotkey

http://www.autohotkey.com/

I've had some success with it, it really seems quite powerful.

I will definitely check that out. I don't yet see how it will work for me in this situation, but it does look like a useful program.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.