Microsoft Access VBA Tutorials for self-paced learning, Class Modules, SQL Techniques, and AI Integration Guides.

Tuesday, April 24, 2012

Microsoft DOS Commands in VBA

Continued from Last Week.

Continued from last week’s Article: Disk Operating System Commands in VBA.

Once we determine the presence of a file in a folder with the Dir() Command, we can do certain operations on the file, like opening that file in its parent application through the Shell() Command or making a copy of that file to a different location with the FileCopy() Command or deleting it with the Kill() Command.

Example-1:

Check for the presence of a text file in a folder, and if found, open it in Notepad.exe
Public Function OpenTextFile()
Dim txtFilePath As String
Dim NotePad As String

   txtFilePath = "C:\msaccesstips\htaccess.txt"
   NotePad = "C:\Windows\System32\Notepad.exe"

If Dir(txtFilePath, vbNormal) = "htaccess.txt" Then
   Call Shell(NotePad & " " & txtFilePath, vbNormalFocus)
Else
   MsgBox "File: " & txtFilePath & vbcr & "Not Found...!"
End If

End Function

Example-2:

: Make a copy of the file with the FileCopy() Command.
Public Function CopyTextFile()
Dim SourcefilePath As String
Dim TargetFilePath As String

   SourcefilePath = "C:\msaccesstips\htaccess.txt"
   TargetFilePath = "C:\New Folder\htaccess.txt"

If Dir(SourcefilePath, vbNormal) = "htaccess.txt" Then
   FileCopy SourcefilePath, TargetFilePath
   MsgBox "File copy complete."
   
Else
   MsgBox "File Not Found...!"
End If

End Function

Example-3: Find and Delete a File from a specific location on the Hard Disk.

Public Function DeleteFile()
Dim FilePath As String, msgtxt As String

   FilePath = "C:\New Folder\htaccess.txt"

If Dir(FilePath, vbNormal) = "htaccess.txt" Then
   msgtxt = "Delete File: " & FilePath & vbCr & vbCr
   msgtxt = msgtxt & "Proceed...?"
   If MsgBox(msgtxt, vbYesNo + vbDefaultButton2 + vbQuestion, "DeleteFile()") = vbNo Then
      Exit Function
   End If
   Kill FilePath
   MsgBox "File: " & FilePath & vbCr & "Deleted from Disk."
   
Else
   MsgBox "File: " & FilePath & vbCr & "Not Found...!"
End If

End Function

Check for a Folder Name:

Dir() Function can also be used to inspect the presence of a folder in preparation for creating a new folder in a particular location on the Hard Drive.

The following Command checks for the presence of a particular folder on the C: drive:

strOut =  Dir("C:\Developers\Projects", vbDirectory)

The second parameter, vbDirectory, asks the Dir() command what to look for in the specified Path. If the folder Projects is found under the C:\Developers folder, then the folder name Projects is returned in the strOut variable; otherwise, it returns an empty string.

The MKDIR Command

The MkDir() Command can be used for creating a new folder if the Projects folder doesn't exist.

Let us write a small program to check the presence of the Projects folder. If it doesn’t exist, then let us create the folder.

Public Function CreateFolder()
Dim folderPath As String
Dim msgtxt As String

folderPath = "C:\Developers\Projects"

If Dir(folderPath, vbDirectory) = "" Then
   msgtxt = "Create new Folder: " & folderPath & vbCr & "Proceed ...?"
   If MsgBox(msgtxt, vbYesNo + vbDefaultButton1 + vbQuestion, "CreateFolder()") = vbNo Then
      Exit Function
   End If
   MkDir folderPath
   If Dir(folderPath, vbDirectory) = "Projects" Then
      msgtxt = folderPath & vbCr & "Created successfully."
      MsgBox msgtxt
   Else
      msgtxt = "Something went wrong," & vbCr & "Folder creation was not successful."
      MsgBox msgtxt
   End If
Else
   msgtxt = folderPath & vbCr & "Already exists."
   MsgBox msgtxt
End If

End Function

The Dir() Command can check the volume label of the Disk Drive.

The following command, run directly from the Debug window, gets the Volume Label of the Hard Drive if it exists; otherwise, it returns an empty string:

? Dir("D:", vbVolume)

Result: RECOVERY

Earlier Post Link References:

Friday, April 13, 2012

Disk Operating System Commands in VBA

Disk Operating System Commands in VBA.

The Microsoft Disk Operating System (MS-DOS 1.0) was launched in 1982.  The first edition of the Microsoft Windows 1.0 Operating System, Disk Operating System with Graphical User Interface (GUI), was released on November 20, 1985 – Source: www.wikipedia.org.  The Disk Operating System Version under Windows 7 is 6.1.7600.

Disk Operating System Commands (both Internal and External) are used in the Command Prompt for Managing Files and folders on Disks, and for retrieving information about them.

DIR Command.

For example, the Directory Command (Dir /S/B/P) provides a file list with full Path Names (like 'C:\My Documents\New Folder\Resume.doc') from your C: Drive and displays them on screen, one page at a time. 

Let us try an example.

Click on the Start Menu.

Type cmd and press Enter; the DOS Window will open up with the Command Prompt C:\>.  Type the following command to display a list of Folders and Files from your C: drive, one page at a time.  You must press a key to advance to the next.

Warning:  The Folders/Files listing on your C: drive may run to hundreds of pages.  Press Ctrl+C (break the command) to terminate the list from displaying further.

C:\> Dir /S/B/P

C:\>> is the command prompt

Dir (command stands for Directory)

The Command switch /S includes Files in subdirectories in the output.

The Command switch /B provides a bare-formatted list of files, i.e., gives only the file Path names without creation date, file sizes, or any other information about the files.

The Command switch /P displays the output on Screen one Page (one screen full) at a time.  Need to press a key on the keyboard to advance the files list to the next Page.

If you want to save the entire list to a text file, without page breaks, issue the following command with the output redirection symbol (>) and a text File name.  The redirection symbol will send the output of the Directory command to a specified text file, without displaying it on the screen.

C:> Dir /S/B > myDirList.txt

Note: Leave a space on either side of the > symbol.

TYPE and MORE Command.

Open myDirList.txt file in Notepad and check the contents. You can display the contents of the myDirList.txt file with the following DOS Command:

C:\> TYPE myDirList.txt

Press Ctrl+C to stop the runaway display. The TYPE command displays the contents of a text file on screen. But it will not display the output one screen full at a time. To do that, we can use another DOS command: MORE, both Commands joined with the piping symbol (|).

C:\> TYPE myDirList.txt | MORE

In the above command, the piping symbol (|) is used to join the TYPE filename.txt | MORE commands to get the required output. The TYPE command reads the text file Contents and passes it to the next command, MORE, without directly sending the output to the Screen. The MORE Command takes its input from the TYPE command and displays it one screen-full at a time. Press SPACEBAR to display the next screen-full of text.

OR

C:\> MORE < myDirList.txt

If the > (greater than) symbol is known as a redirection symbol in DOS, then the < (less than) symbol is known as a Source symbol for the MORE command. The MORE command reads data from the filename given immediately after the Source Symbol (<) and displays one screen-full at a time.

DIR Command in VBA.

The Dir Command is available in VBA too. But it is used for a different purpose. We can use this command to check for a particular file or the presence of any file in a folder. The usage of this command is shown below:

strOutput = Dir("C:\My Documents\Resume.doc", vbNormal)

The Dir command checks for the Word File Resume.doc in the Folder C:\My Documents. If found, the file name 'Resume.doc' is returned in the strOutput Variable; otherwise, an empty String is returned.

strOutput = Dir("C:\My Documents\*.*", vbNormal)

This command gets the first file name from the specified folder, and returns it in the strOutput Variable. You may try out this command in the Debug Window directly, like:

? Dir("C:\My Documents\*.*")

This will print the first file name found in the folder C:\My Documents in the Debug Window. To get subsequent file names from the same folder, you can run the command without any parameters to the function, like:

? Dir()

Note: The First time you run this Command, you should provide a Path as a parameter; otherwise, it will run into an error.

Place the insertion point on the Dir() Command, and press F1 to display the details of this Command in Access Help Documents.

There are other interesting Disk Operating System Commands, like ChDrive, ChDir, MkDir, RmDir, etc., and we will learn their usage in VBA Next week.

Earlier Post Link References:

Powered by Blogger.