Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Shell to do Windows/file based tasks

Status
Not open for further replies.

mainit123

Technical User
Jan 24, 2005
25
Hello-
I am having a great time coding in Visual Basic using Visual Studio 7.0.9955 doing the kinds of things it was meant to do i.e., forms, dialogs, etc. However, it is probably no surprise that now and then while working alot with ASCII flat files, I need to execute a DOS type command (more, type, dir *.txt, copy file1 + file2) in order to accomplish a file based task such as capturing a specific group of filenames to a string or printing out the contents of a file without having to include a separate subroutine. I am aware of the Shell command and also Shellexecute and have tried in vain to create usable versions of these commands within my code to accomplish the above tasks. I can understand the syntax of something like Tools.Shell """c:\Program Files\SomeFile.exe"""
but simply cannot get even a slight variation of that to Build without error. In addition, I am really interested to know how to use a Windows command with an argument such as, "C:\notepad.exe c:\test\myfile.txt " or something simpler such as, "dir c:\test\*.txt". I realize this approach of using DOS based commands may be an archaic and cumbersome way to do file based work but I am not aware in VB how to gather filenames from a specific directory on the hard drive into a string variable. I know VB can do a bunch of stuff but the syntax of the Shell command is tripping me up, even after reviewing all that I could with Online Help, etc.
Thanks for your help with a couple of examples.
 
Try having a look at the System.IO namespace. I think you'll find a number of useful methods to do what you wish.

Here's a quick example. It's totally untested, but should work.

Code:
'Reference to System.IO namespace
Imports System.IO

'function to print show all files in the current directory
'as a comma-delimited string
Public Function filesInADir() as String

'get current directory as a string value 
Dim curDirectory as String = Directory.GetCurrentDirectory
Dim files as String()

'return all files in the current Directory as an array of
'strings
files = Directory.GetFiles(curDirectory)

'print files seperated by a comma
MessageBox.Show(files.join())

I'm not totally sure if the array.join() method is supported in vb.net or not, but if not, you get the idea.
Have a look at the DirectoryInfo object as well.

Hope this helps.

 
Err,

got side tracked. the last line(s) of the code should be:

Code:
Return files.join()

End Function

Or you could keep the MessagBox statement if you didn't need to return the value...
 
Join is actually a string function and needs to be used like:

[tt]MessageBox.Show(String.Join(",", files)) [/tt]

or

[tt]MessageBox.Show(String.Join(Environment.NewLine, files))[/tt]



Hope this helps.

[vampire][bat]
 
All dos commands can be executed via the process class.

But most file related things can be done via the system.io like caoamo said.




Christiaan Baes
Belgium

"My new site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top