4.2. Creating and Deleting a Folder

Problem

You want to create or delete a folder.

Solution

Using a graphical user interface

  1. Open Windows Explorer.

  2. In the left pane, browse to the folder where you want to create a new folder or that contains the folder you want to delete. Click on the folder.

  3. To create a new folder, right-click in the right pane and select New and the type of folder you want to create.

  4. To delete a folder, right-click the folder in the right pane and select Delete. Click Yes to confirm. This moves the folder and its contents to the Recycle Bin.

Tip

If you want a New Folder option when you right-click in the left pane of Windows Explorer, see http://www.createwindow.com/freeware/newfold.htm.

Using a command-line interface

To create a folder, use the mkdir command (or md for short):

> mkdir c:\scripts

To remove a folder, use the rmdir command (or rd for short):

> rmdir c:\scripts

Use the /s option to remove a folder and all files and subfolders contained within it. Use the /q option to bypass the confirmation prompt when using /s.

To delete a folder on a remote server, use the psexec.exe command:

> psexec \\<ServerName> cmd.exe /c rmdir /s c:\temp

To provide alternate credentials with psexec, use the /u and /p options to specify a username and password, respectively.

Using VBScript

' This code deletes a folder
' ------ SCRIPT CONFIGURATION ------
strFolderPath = "<FolderPath>" ' e.g., "d:\temp"
' ------ END CONFIGURATION ---------
set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder(strFolderPath)
WScript.Echo "Successfully deleted folder"

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.