Basic DOS / Windows CMD file management

From Knowledge Kitchen
Jump to navigation Jump to search


This document outlines some basic commands that allow a user of a Windows operating system to manage their files using the command line. Historically, the Windows command line was known as the DOS prompt, from the days when Windows was an extension of the DOS operating system. However, nowadays, Windows is a stand-alone operating system, and CMD.exe is an emulator of the old DOS environment.

Mac, UNIX, and Linux users may be interested in an equivalent overview of managing files from the command line on UNIX-like systems.

Assumptions

These commands assume the user is running the CMD.exe program on Windows. This can be run directly by typing cmd.exe into Windows' run program text field.


Print the current working directory

Show what directory is the current working directory:

cd

Change current working directory

Go one level up:

cd ..

Go one level down into the 'foo' subdirectory:

cd foo

Go to the very top directory of the drive (called the root directory)

cd C:\

Go to the very top of the hard drive, then through the 'Users' subdirectory and into the 'foo' subdirectory

cd C:\Users\foo


List files and directories

Show the names of files and folders in the current working directory:

dir /W

Show all the details of the files and folders in the current directory:

dir

Show all details of the files and folders in the 'Users' subdirectory of the root (top-most) directory:

dir C:\Users

Copy a file or directory

Copy a file from one location to another:

copy file1.txt file2.txt

Move a file or directory from one location to another

Move a file from one location to another:

move file1.txt file2.txt

Delete a file or directory

Delete a file named bob1.txt:

erase bob1.txt

Delete a directory named bob2:

rmdir bob2

Create a directory

Examples:

mkdir foo
mkdir ../foo/bar

Run a Java program

Assuming there is a file named HelloWorld.java in the current working directory...

Add the Java Development Kit to the Windows PATH variable

In order to compile and execute Java programs from the Windows command line, you must make sure the javac, java, and other programs included in the Java Development Kit (JDK) are all included in the Windows PATH environmental variable. The PATH contains a set of directories where Windows looks for programs you attempt to execute from the command line. You need to add the path to your JDK's 'bin' folder, where all JDK programs reside, to this PATH only once after installing the JDK.

  1. Open System properties (run sysdm.cpl)
  2. Click on the "Advanced" or "Advanced System Settings" tab
    • In Windows 10, you may need to scroll down to the Related settings section and click the System info link. In the System window that opens, click the Advanced system settings link in the left column.
  3. Click on the "Environment variables" button
  4. In the lower list, "System variables," click on Path
  5. Click "Edit".
  6. Append the path of your JDK's 'bin' folder to the existing PATH variable data, preceded by a semi-colon, but with no extra spaces before or after it.
    • For example (your path will be different), what you append to the PATH variable will look something like this (your JDK 'bin' path will probably be different):
      • ;C:\Program Files(x86)\Java\jdk1.8.0_121\bin
    • If you don't know the path of your JDK's bin directory, find that out first!
  7. Click OK and close the windows
  8. Open a new command prompt (run CMD.exe) and run the 'set path' command (without the quotes).
    • this will output the value of the PATH variable.
    • verify that the PATH now includes the JDK 'bin' directory
    • if so, you're ready to compile and run Java programs from the Windows command prompt.

Windows path variable setting.png


Click OK on the path edit box and OK on the Ennvironment Variables box. The new setting will go into effect next time you run the command prompt, so quit and restart CMD.exe.

Compile a Java program

Compile the source code into a bytecode (*.class) file:

javac HelloWorld.java

Run it

Running a Java bytecode file means sending it to the Java Virtual Machine to be interpreted into machine code:

java HelloWorld

A note about using packages

If your Java source code indicates a package name, then the source and class files must be located within a folder appropriate to the package name. Every component of the package name must have its own folder.

For example, if the source file is HelloWorld.java and the package is named foo.bar, then you must have the following two files in the following folders:

foo/bar/HelloWorld.java
foo/bar/HelloWorld.class

To execute the program, you must run it as follows. It is critical to execute this statement from outside the top-level package folder... in this case from the parent folder of the folder named 'foo':

java -classpath . foo.bar.HelloWorld

This makes sure that the Java Virtual Machine can find the class file in its proper package.


What links here