Page 1 of 1

teach a noob

Posted: 29 Mar 2011 19:19
by jomozart
hello guys! its nice to have a site like this! Im 30 years old and i wanted to start a new hobby- programming
i have red sites and discussions in forum about when should i start. Many says, that i should start python, or c, or c++.. or java..im doing java at a moment but i realized i been missing some critical topics that i need to tackle with other language ( familliarity). anyways, what am trying to say is that i decided to give effort on dos batch programming atleast 60% dos batch and 40% to laguage what im currrently doing as a hobby. that thing is am continually amazed by how batch program works. so i base my decision on how i felt about now about this batch program. i've done a simple beginner program like hiding folder and moving file ,open a multiple program and stuff.. these are really basics. but now i came to a point that i need more script to study with and that i ended up here looking. while doing my journey in batch file progamming i wanted to know about this:

- im currenlty working on a kind of script,batch program ..what ever you call it.. the thing is i want to make a script that will store a data and retain that stored data to a variable even after i exit the batch file.

here's what i really wanted:

enter path of folder and press enter : (explorer will pop-up, i'll copy the folder path)
(enter pressed)enter you fodler path here: ( paste or just type in the fodler path)
(enter pressed)hide folder? (y/n): ( enter "y" folder is hiden, if "n" goto viewfolder option)
(enter pressed)view folder?(y/n): ( enter "y" folder is availble, if "n" goto hide folder option)
(exit batch file)
(open batch file)now made a if else at the start of batch" (this part should actully be located at start)
the command is if exist "%path_entered%" goto hideOption
goto viewFolder


..lol for this... i made the script but if you exit it doesn't store a path that i entered.. lol.. im really challenged by this , i meant for a starter i want this kind of script. now i tried the for %%x (the script with %path_entered%) " but i reallized after viewing the the edited batch file(CallFodler.bat) doesn't store the path in usinhg this command statement even i put the %path_entered%" do (echo %%x >> C:\CallFolder.bat).. i tried to do this statment but every string that is seperated by space is entered on the next line.. lol..

now what is CallFOlder.bat you may ask. well since i figure atleast at the moment that i cant store a data to a variable whenever i exit the batch file, i tried to include an "editing batch" using for %%x in () do () statement within the batch file and store it in CallerFodler.bat so that if i exit the batch file the next time i enter it will do the the command to hide and view the folder respectively.

well guys good luck with this if you want to help me .. will really appreciate it. and thanks in advance! i hope you guys can teach me as i go along learning this batch scripting stuff.. GOdbless!

Re: teach a noob

Posted: 30 Mar 2011 04:50
by jomozart
Hi again! please see my simple batch script to help me explain what i want to do. .. i know this is silly but please bare with me as i want to learn the about batch script. i know i maybe wanting too past..lol.. but hey if i can do it faster why not.. lol

and by the way the reason i want this is that it will like an executable batch file can be use in diff computer.


@echo off
cls
:start
if exist "Path_of_the_test_folder" call callFolder.bat
set /p "c= press enter to view folder you want to hide."
start explorer
pause
set /p "v= copy root directory tree folder to hide:"

pause
if "%v%" == "%v%" goto hide
exit

:loop
cls
set /p "v0= Showfolder? (y/n)"
if "%v0%" == "y" goto unhide
if "%v0%" == "n" goto ask
if "%v0%" == "%v0%" echo fail!!
pause
goto loop
exit
:hide
( i wanted to put the the for %%X in () do () here to create another batch and later on will be called )

attrib +r +a +s +h "%v%"
echo Folder is hiden
pause
goto loop

:ask
cls
set /p "v1= Hide folder? (y/n):"
if "%v1%" == "y" goto hide
if "%v1%" == "n" goto unhide
if "%v1%" == "%v1%" echo fail!!
pause
goto ask
exit
:unhide
cls
attrib -r -a -s -h "%v%"
echo Folder is unhide
pause
goto loop



but this wont save path that i entered. so i wanted to create a another batch file within this script using for %%x in (@echo off,cls,:loop,cls,if not exist %v% goto ask....and so on) do (echo %%x >> "C:\callFolder.bat) to come up with:

named: callFolder.bat
the %v% here should be same us the "Path_of_the_test_folder"

@echo off
cls
:loop
cls
if not exist "%v%" goto ask
set /p "a= hide folder? (y/n)"
if "%a%" == "y" goto hide
if "%a%" == "n" goto unhide
if "%a%" == "" goto fail
if "%a%" == "%a%" goto fail
exit
:fail
cls
echo Fail!
pause
goto loop
exit

:hide
cls
attrib +r +a +s +h "%v%"
echo Folder is hiden
pause
goto ask

:ask
cls
set /p "b= view folder? (y/n)"
if "%b%" == "y" goto unhide
if "%b%" == "n" goto hide
if "%b%" == "" goto fail2
if "%b%" == "%b%" goto fail2
goto ask
exit
:fail2
cls
echo Fail!
pause
goto ask
exit
:unhide
cls
attrib -r -a -s -h "%v%"
echo Folder is unhide
pause
goto loop

exit

Re: teach a noob

Posted: 30 Mar 2011 05:37
by ghostmachine4
jomozart wrote:that thing is am continually amazed by how batch program works.

you will be amazed how batch quickly becomes cumbersome, ugly and not easy to maintain plus since its lacking a lot of programming features, you will often need workarounds, so much so that very quickly you begin to create inefficient code. Quit batch programming, and choose to learn a programming language. A good programming language can do what batch can't (often more easily as well) , and you will find more satisfaction as you program along. I recommend you start with Python.

Re: teach a noob

Posted: 31 Mar 2011 09:41
by jomozart
first of thanks for the reply and advice ghostmachine4..

i have to lol with familiar response like what my other friends just said. Python would be my choice indeed since i don't have any background of other languages(i meant not much) . but the thing is ..its just that i wanted to know more about batch stuff coz infact i can apply this stuff readily unlike other language that need more time to make it personally "usable". its true that its lucking of great feature..lol we all know that. but let me do this for a while since im starting and wanted to explore a bit.

im closer to to my objective regarding my post. i just finished a batch program that will change your password w/o editing the text inside. lol.. what i mean is like this.

program proper:

enter password: ( your current pasword)
if correct it will prompt/echo you to "correct password"
if wrong pw then echo you to "incorrect password"
then will ask you to "change password"
then will ask you to "type your old password" ( if not correct will loop you to same question)
if correct will ask you to "type your new password"
if you exit,
your new password will be the current password. thats right. i lvl up 1 bit and now i have idea
how to store variable and keep it. . will just convert this batch to exe pile then its done. :)

Re: teach a noob

Posted: 31 Mar 2011 18:01
by ghostmachine4
jomozart wrote:program proper:

enter password: ( your current pasword)
if correct it will prompt/echo you to "correct password"
if wrong pw then echo you to "incorrect password"
then will ask you to "change password"
then will ask you to "type your old password" ( if not correct will loop you to same question)
if correct will ask you to "type your new password"
if you exit,
your new password will be the current password. thats right. i lvl up 1 bit and now i have idea
how to store variable and keep it. . will just convert this batch to exe pile then its done. :)


and how are you going to mask user password input? using some other third party tool or some obscure DOS batch workaround? With Python, you can at least use getpass module. While i respect your decision to play with batch, I am still going to say you are wasting your time. Your time can be well spent getting in depth with Python since you already know it. A good programming language plus abundance of libraries for your tasks are all you need.

Re: teach a noob

Posted: 31 Mar 2011 18:33
by jomozart
and how are you going to mask user password input? using some other third party tool or some obscure DOS batch workaround? With Python, you can at least use getpass module. While i respect your decision to play with batch, I am still going to say you are wasting your time. Your time can be well spent getting in depth with Python since you already know it. A good programming language plus abundance of libraries for your tasks are all you need.


absolutely not! its same batch file doing that staff.. lol if that interests you a bit i can show you how. but right now i need to rush work. ill give you a hint. its a little trick using REN command. i'll catch u later!

Re: teach a noob

Posted: 31 Mar 2011 23:19
by ghostmachine4
jomozart wrote:
and how are you going to mask user password input? using some other third party tool or some obscure DOS batch workaround? With Python, you can at least use getpass module. While i respect your decision to play with batch, I am still going to say you are wasting your time. Your time can be well spent getting in depth with Python since you already know it. A good programming language plus abundance of libraries for your tasks are all you need.


absolutely not! its same batch file doing that staff.. lol if that interests you a bit i can show you how. but right now i need to rush work. ill give you a hint. its a little trick using REN command. i'll catch u later!

sure, i would like to see how you use ren to mask user input.

Re: teach a noob

Posted: 01 Apr 2011 08:20
by jomozart
hello mate! just came from work and my wife jus nagged me for unknown reason.. ow! i forgot to buy the thing..lol anyways.. and ghost your hurting my feelings aswell as Mr Batch here..lol dont you remember that you used to be his fan? lol anyways.. okay this what i did to store variable value, to me this is like you took a picture and then save for.. well if you want to see it again.. lol. im not good of explaining things.

ok first off you need some thing (object) to store everything in it and eventually use it when its called.

so i can think of no other way but to make a file. in my program i used file w/o any extention. so here it goes:

@echo off
:start
cls
set "v1= null" rem if you will not input this, it will store last entered value
set /p "v1= enter pw:"
if "%v1%" == "%v1%" goto confirm
exit

:confirm
if exist "C:\Documents and Settings\username\Desktop\password\%v1%" goto yes rem the path is where the password container goes
echo.
echo wrong password
echo.
pause
echo.
set "v2= null"
set /p "v2= change password? (y/n)"
if "%v2%" == "y" goto change
if "%v2%" == "n" goto start
goto start
exit

:change
cls
echo.
set /p "v3= enter old password:"
if exist "C:\Documents and Settings\username\Desktop\password\%v3%" goto ren
echo.
echo wrong old password
set "v3= null"
pause
goto change
exit

:ren
cls
cd\ rem i just got used to place cd\ whenever i meant to use path.. just to make it sure i reset stuff
cd C:\Documents and Settings\username\Desktop\password
set /p "v4= enter new password:"
ren %v3% %v4%
echo.
echo Your new password is %v4%
echo.
pause
goto start
exit

:yes
echo.
echo yes it exist rem just to make it sure that pw(file) is correct
set "v1= null"
echo.
pause
goto start
exit


the password (container) is a folder("tangeable" object) that stores the variable(s)
everytime you change the password you actually renaming the file (w/o extention) and echo this file and later on will be asked if it exist when ask : enter password? . if it fails it doesnt mean it doesn't have any value but the input value didnt match, hence wrong password.

it aint much but in this manner i can now change my password whenever i wish and since just like an object-oriented program, it doesnt concern how you prepare thisng up but the result is the same. lol. did i say that right?

And may i add that i did say convert this into an exe file. in this sense yes your right, it will need a third party software. but ther is an alternative to make it like executable batch by saving it in to windows/system32. you can now call this in start-run-(the batch file name).

okay i now i shift my learning % to 60% language 40% batch since you seemed affirmitive with your suggestion. :( . poor Mr Batch. i was thinking C++ but i guess Python would do for now..

Re: teach a noob

Posted: 01 Apr 2011 09:06
by ghostmachine4
jomozart wrote:.lol dont you remember that you used to be his fan?

i am never a batch fan. i say again, never.

Code: Select all

set /p "v1= enter pw:"


this is the part I am talking about. when I say mask the user input, i mean when the user keys in his password, the letters he typed should not be visible to the screen.

And may i add that i did say convert this into an exe file. in this sense yes your right, it will need a third party software. but ther is an alternative to make it like executable batch by saving it in to windows/system32. you can now call this in start-run-(the batch file name).

There are 3rd party Python modules to convert batch to exe. eg py2exe.

okay i now i shift my learning % to 60% language 40% batch since you seemed affirmitive with your suggestion. :( . poor Mr Batch. i was thinking C++ but i guess Python would do for now..

For practical reasons, C++ is not a good choice, unless you plan to program in low level. eg games with intensive graphics, device drivers, etc. For general purpose programming , a language like Python (Perl, Ruby etc) would be good enough. You only need a bit of batch , yes, to run your Python scripts, that's all to it for batch.

Re: teach a noob

Posted: 01 Apr 2011 10:04
by jomozart
jomozart wrote:
.lol dont you remember that you used to be his fan?


i am never a batch fan. i say again, never.

lol. ok.ok never thought you hate Mr. Batch so much.

Quote:
Code:
set /p "v1= enter pw:"


this is the part I am talking about. when I say mask the user input, i mean when the user keys in his password, the letters he typed should not be visible to the screen.



well you'll just have to hide it with your hands while typing pw.. lol. kidding aside.. actually i was imagining last night when i made this script. iwas thinking *** while typing the pw.. and yeah i was thinking of whos mask your talking about. lol sorry not too familiar with sort.

For practical reasons, C++ is not a good choice, unless you plan to program in low level. eg games with intensive graphics, device drivers, etc..


yeah you hit it right! i was thinking of creating a game (someday) like rpg and stuff. like in a browser gam and stuff. just fancy making games one day.

Re: teach a noob

Posted: 01 Apr 2011 17:55
by ghostmachine4
jomozart wrote: yeah you hit it right! i was thinking of creating a game (someday) like rpg and stuff. like in a browser gam and stuff. just fancy making games one day.


you can create games also with Python. Type "pygame" on Google search

Re: teach a noob

Posted: 02 Apr 2011 18:10
by jomozart
okay ghost you made your point.. 1 more thing.. for a bit of inspiration. i assumed you are good at pyhton. can you do me a program that will perform what i want from batch script? im not yet on that part and im not in hurry but i just want see how you do it. of course this doesn't mean you do it but i will appreaciate it if you do. that is: hide any folder with options to hide more in different locations. and a password protection that i can replace and of course "masking" it would really be a must. thanks!

Re: teach a noob

Posted: 02 Apr 2011 23:33
by ghostmachine4
jomozart wrote:okay ghost you made your point.. 1 more thing.. for a bit of inspiration. i assumed you are good at pyhton. can you do me a program that will perform what i want from batch script? im not yet on that part and im not in hurry but i just want see how you do it. of course this doesn't mean you do it but i will appreaciate it if you do. that is: hide any folder with options to hide more in different locations. and a password protection that i can replace and of course "masking" it would really be a must. thanks!

no I will am not going to do the whole program for you (unless you are prepared to pay me for my service), but I will show you how some of the batch stuff is done in Python.

1) Asking for user input

Code: Select all

folder = raw_input("Enter the folder to view: ") # now the variable "folder" contains user input


2) To view folder. Use the os module

Code: Select all

import os
os.chdir("c:\\folder_to_view)
for file in os.listdir("."):
    print file


3) Hiding files, making it read only ... see here for a way. Otherwise, you can always
make a system call to "attrib" command, but i highly discourage it. Another way is to use tempfile

4) to hide password as you type

Code: Select all

import getpass
word=getpass.getpass("Enter your password:" )


Loops and such are the basics of any programming language, so I suggest you start reading up on Python if you are really into it.

Re: teach a noob

Posted: 03 Apr 2011 01:17
by jomozart
ok thanks for the reply. i guess you need not to show me the entire program.. :) anyways, i appreaciate your effort giving time to share your point. i might ask for another pointers but for now thanks.