Need help with batch program please

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dossier90
Posts: 3
Joined: 12 Sep 2013 18:42

Need help with batch program please

#1 Post by Dossier90 » 12 Sep 2013 19:05

Hey all, I need some help with my batch file that i'm creating for myself. So a bit of background for this;

I'm making a .bat "Archiver" that stores CD Keys that belong to software/games. I have it setup pretty great and figured stuff out, but this one thing I do not get.

Basically I have three options in the menu

#1 add a cd key
#2 look up a cd key
#3 edit a cd key


so #1 I have the input like this:

set /p %name%= Please enter name:
set /p %key%= Please enter key:
echo %name%, %key% > %name%.txt

So whatever name they type in is dubbed the %name% variable. Whatever key they type in, is dubbed the %key% variable. Both are saved to a %name%.txt file; so that %name%.txt will be starcraft.txt or whatever name they typed in.

So, going to #2, it only displays the .txt file (via type command) after you just added it. Only then can you bring up the name and cd key. When you close the batch file and start it back up again, it can't find the file. So i dont know if I need to have some sort of memory code or something to save it?

Anyways, here #2's code:

set /p %name%= please enter name you wish to look up:
[then a bunch of timed echo stuff to make it look cool]

type %name%.txt

So, like I was saying before, it will display the %name% and cd %key% within the %name%.txt file as long as you don't close out the batch file, if you store you cd key, look it up (it will display) and then close it, and try to do it again, it wont be able to find the file.

What gives?

:-/

i'm a new guy here, and still remembering how to make batch files.

Thanks guys!

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Need help with batch program please

#2 Post by ShadowThief » 12 Sep 2013 20:03

Does the file exist if you navigate to where it should be?

Dossier90
Posts: 3
Joined: 12 Sep 2013 18:42

Re: Need help with batch program please

#3 Post by Dossier90 » 12 Sep 2013 20:46

Yes, the %name%.txt files are created in the same folder that the batch file is in. When I have both the folder and the batch file open, once I've done added the name and cd key it appears right away in the folder and stays there till its deleted.

Thanks for replying :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help with batch program please

#4 Post by foxidrive » 12 Sep 2013 22:17

The batch file will find them if it is launched in the same folder.


This will allow you to use spaces in a filename. Add the quotes here and when saving.

type "%name%.txt"

Dossier90
Posts: 3
Joined: 12 Sep 2013 18:42

Re: Need help with batch program please

#5 Post by Dossier90 » 13 Sep 2013 00:00

That's exactly what I had. But I fixed it lol I figured out what was wrong with it

Instead of "set /p %name%= Please enter name"

It needed to be "set /p name= Please enter name"

and the type stayed the same; "type" %name%.txt"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help with batch program please

#6 Post by foxidrive » 13 Sep 2013 04:09

Well spotted.

If you use the actual quotes as listed below then you can use more than one word in the variable as well as characters like an &

Code: Select all

set /p "name=Please enter name: "
set /p "key= Please enter key: "
for /f "delims=" %%a in ("%name%, %key%") do >"%name%.txt" echo %%a

Post Reply