Question:
I am new to command line and batch writing and need some assistance please?
Michael
2011-07-22 04:53:29 UTC
Hey, I am just trying to learn how to write .bat files and use command line for my own benefit, eventually to be more comfortable with Linux. My first project for myself is to write a program that deletes a file and I did that, del "C:\Users\username\Desktop\test.txt"/Q/S>nul . Is there a way to detect the username? I want to be able to have it run on any computer, filename being the same, and have it autodetect the username.
Four answers:
Locoluis
2011-07-22 05:03:29 UTC
If you want to be more comfortable with Linux, stay away from the MS-DOS command line.



Besides, Linux (and Unix in general) pathnames don't look like that "C:\Users\username\Desktop\test.txt" nonsense. They look like, say, "/home/username/Desktop/test.txt".



Why? Because it's a different operating system, it's not like MS-DOS/MS-Windows.



If you used Linux, the answer would be:



rm ~/Desktop/test.txt



(The shell automagically replaces the ~ with the path to the user's home directory)
Dave, the IT guy
2011-07-22 12:27:02 UTC
Well you have a couple options. If the file is always in the desktop folder, you could use:



del %USERPROFILE%\desktop\test.txt /q/s



If the file is somewhere else, but you still require the username, it would just be: %USERNAME%



Anything between %'s is a variable as far as batch file scripting for Windows goes. Some variables are set by the operating system, some are set by the script itself.



Linux is quite different but the principles are the same. If you have an idea of what you want to do, it isn't hard to find the way to do it on Windows or Linux.



You can even install a free program that will give you lots of Linux like options for Windows:

http://www.cygwin.com/



Good luck!
?
2011-07-22 12:05:59 UTC
BIt off topc but dos in Windows is NOTHING like bash in Linux. The only way to become comfortable with Linux is to just use it. Try VirtualBox if you don't want to make the jump to dual booting yet.
pmustang
2011-07-22 16:02:25 UTC
Dave is correct. However i believe your trying to say you want to be more comfortable with programming, not exactly linux. If its this there are some good batch tutorials at http://www.youtube.com/results?search_query=batch+file+tutorial&aq=0&oq=batch+file+tut



or if you want a site try

http://www.computerhope.com/batch.htm


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...