Log in

View Full Version : Batch File


Never_Forget
September 20th, 2008, 01:48 PM
I've written a fairly simple batch file that, when put into a directory and opened, will allow me to open files or, if it's a .txt file, view it in the command prompt. However, I'm having problems. :(
I can't open directories (folders) or any file with a space in the name.
The code for it is:
@echo off
title File Reader
:start
:a
cls
echo Type filename and extension
set /p file=
echo %file%? (y/n)
set /p var=
if %var%==y goto b
if %var%==n goto start
:b
echo Do you wish to Display or Open? (D/O)
set /p do=
if %do%==d type %file%
if %do%==o call %file%
echo:
pause
goto start

Could anybody edit this and post it back here or tell me how to open folders and files with a space in the name? And yes, I have tried underscores.

Thanks ^^
~Samm

Falk 'Ace' Flyer
September 20th, 2008, 02:18 PM
Did you try putting it it quotes? Otherwise I don't think command prompt allows files with spaces...

Never_Forget
September 20th, 2008, 02:24 PM
Did you try putting it it quotes? Otherwise I don't think command prompt allows files with spaces...
Nope, I didn't try. I'll try now before I post this ^^ Thanks
It works for files with spaces ^^ Thanks! Although I still have my folder problem. Thanks! I wouldn't have thought of using quotes.

Kiros
September 20th, 2008, 02:38 PM
I'm not sure if I understand the directory problem completely. If you're wanting to open a an explorer window of the folder, then you're going to have to use the explorer.exe application and pass in the path argument - which should be the first parameter.

For instance, explorer.exe "C:\Documents and Settings\" would open a window with that path.

However, if you want to change your directory within the prompt, use the cd command.

For example, cd \ would change the directory to the root of the current drive, and cd ..\tacos\rule would take you back one directory (..) and then go into \tacos\ and finally end up at \rule\.

Never_Forget
September 20th, 2008, 02:55 PM
I'm not sure if I understand the directory problem completely. If you're wanting to open a an explorer window of the folder, then you're going to have to use the explorer.exe application and pass in the path argument - which should be the first parameter.

For instance, explorer.exe "C:\Documents and Settings\" would open a window with that path.

However, if you want to change your directory within the prompt, use the cd command.

For example, cd \ would change the directory to the root of the current drive, and cd ..\tacos\rule would take you back one directory (..) and then go into \tacos\ and finally end up at \rule\.

so,
cd ..\desktop\%folder%
call explorer.exe
would take me to a folder in my desktop,
depending on what %folder% was set to, such as, movies, or something?

Kiros
September 21st, 2008, 09:37 AM
Mmm... Not exactly what I meant. The command cd changes the directory that the prompt is working with. If you start explorer, it's just going to open to a default folder, not the folder that the prompt is currently at. Here, place the following code in an empty batch file and run it (it's an example).

@echo off
title CD and Explorer

echo * =======================================
echo * The current directory is:
cd

echo *
echo * Let's open explorer.exe
echo * Press any key ...
pause >nul

start explorer.exe
echo * Press any key to resume demonstration ...
pause >nul

echo *
echo * Now let's go up one directory!
cd ..
echo * Now the current directory is:
cd

echo *
echo * Let's open explorer.exe again
pause >nul

start explorer.exe
echo * Press any key to resume demonstration ...
pause >nul

echo *
echo * And now let's go to the root!
cd \
echo * Now the current directory is:
cd

echo *
echo * Let's open explorer.exe one last time
echo * Press any key ...
pause >nul

start explorer.exe
echo * Press any key to resume demonstration ...
pause >nul

echo *
echo * Okay, so now we know that explorer.exe doesn't
echo * open to the directory that the prompt is currently
echo * using... So we're going to open explorer.exe
echo * with a directory/file path passed in:
echo * "C:\WINDOWS"
echo * Press any key ...
pause >nul

start explorer.exe "C:\WINDOWS"
echo *
echo * There we go! That settles that!
echo * Press any key to close ...
pause >nul

exit

Hope that demonstration helps. :)

Never_Forget
September 22nd, 2008, 10:05 AM
I LOVE YOU!
Now I can finally finish my reader ^^
Thank you so so so much ^^
Now all I need to know is how to open shortcuts without having to put in the original files location....

Kiros
September 22nd, 2008, 11:38 AM
No problem. Also, shortcuts have *.lnk extensions. You can open them simply like an executable.

For example, I have a shortcut on my desktop to my "BenOpen" folder on my F: drive (F:\BenOpen\). For simplicity, I have the shortcut named to BenOpen as well (but it's really BenOpen.lnk). To open this in a batch file (or command prompt), I would simply do the following:

@echo off
title Opening BenOpen
cd "C:\Documents and Settings\Ben Arnett\Desktop"
start BenOpen.lnk

You don't have to use the "start" command to open executables or shortcuts, but it's good practice, as you can pass in certain parameters into the start command to do different things, like make the prompt/script run in a higher priority.

Good luck :)

Never_Forget
September 22nd, 2008, 11:46 AM
I tried but it didn't work. Does call work for shortcuts?
heres the code:


@echo off
title File Reader
:start
:a
cls
echo ---Instructions for use---
echo *Type a filename AND extension to open
echo eg. iexplorer.bat
echo *If the filename has a space, wrap in quote
echo marks.
echo *When viewing folders, the directory it is
echo contained in must be listed too. eg.
echo desktop\batch
echo *Shortcuts have the extension .lnk
pause
cls
echo Do you wish to open a FOLDER? (y/n)
set /p do2=
if %do2%==y goto z
if %do2%==n goto x
pause
:z
echo Type folder name
set /p folder=
echo %folder%? (y/n)
:y
set /p do3=
if %do3%==y goto n
if %do3%==n goto z
:n
call explorer.exe "C:\Users\sam's\%folder%"
pause
goto start
:x
echo Type filename and extension
set /p file=
echo %file%? (y/n)
set /p var=
if %var%==y goto b
if %var%==n goto start
:b
echo Do you wish to Display or Open? (D/O) (only .txt can be displayed)
set /p do=
if %do%==d type %file%
if %do%==o call %file%
echo:
pause
goto start

It works fine apart from trying to open shortcuts.

Kiros
September 22nd, 2008, 02:10 PM
I wouldn't use call if I were you. Call should be used for calling another batch script. In fact, I have never used call. Its only use, like I said before, for it is calling another batch. Instead, use start.

Nonetheless, the code for opening shortcuts should work (even with call) as long as you're opening it as a file.

But I do have some suggestions.

If you plan to distribute this, then you should set up some defaults, because not everyone is going to have a profile called "sam's." Also, not everyone will follow your standard for the input commands (y, n, etc.), so you should use if, else, and else if commands as a backup.

You might want to look into using some of these to set the default directory: http://www.technipages.com/list-of-windows-environment-variables.html

Personally, I would use something like the following:
cd /d %userprofile%

Also, allowing the user to change directories would be a great feature as well. And to spice things up a bit more, you could use some enhancements to check the file extensions (to see if it's a shortcut or some document that can be typed).

Documents that can be typed are really any plain-text files, like *.txt, *.log, *.xml, *.htm, *.php, etc..

Anyway, just some thoughts. But the shortcut thing should work fine - I tested it.

Never_Forget
September 22nd, 2008, 02:23 PM
I just tried it again and it worked... Weird.
Anyway, I don't really wanna distribute this until i get it like way better. Also, I don't know how to use else or what it does...
lol...
EDIT: Isn't else like, if the command directly preceeding it fails (eg. cant find file) it will do the else command?
i mean like,
start internet.exe
else goto a
goto b

if it couldnt find internet.exe would it goto a bu if it did find internet.exe it would goto b and ignore the else command?

iJack
September 22nd, 2008, 02:52 PM
Aah batch files, dont'cha love them? I have many-a funny shutdown batch files, one, right after executing, it stops the shutdown, and freaks out the user, :D.

Kiros
September 22nd, 2008, 02:55 PM
Not exactly. The commands if and else go together.

Like this:
if "%var%" == "Lol" (
echo The variable is equal to "Lol"
echo Yay!
) else (
echo The variable is not equal to "Lol"
echo Boo!
)

Because the way that batch scripts work, you have to include the opening parenthesis on the same line as the if statement, as well as have the next opening parenthesis on the same line as else. That's the way it was 5 years ago when I was into batch scripting. I'm not sure if it's changed, but I would still use the same convention anyway. The main point of that is to show how you can include more than one line within an if statement and also use else.

To learn more, open up a command prompt and type "if /?" (without quotes). Also, you can type "set /?" to learn some interesting things about variables (including the enhancements I talked about in my previous post).

Again, good luck :)

syd
November 25th, 2008, 11:02 PM
why are you programmign batch files this isn't 1992

Zephyr
November 25th, 2008, 11:23 PM
Please don't bump old threads.

~Locked~