Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / July 2008



Tip: Looking for answers? Try searching our database.

Shell to a string of DOS commands rather than an exe or bat file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lamont - 04 May 2008 05:52 GMT
I need to shell to excecute a Dir/s commands in the command.com window to
build a list of files meeting the desired patter (like *.doc) in the example
below.
I found that I could get a string of commands to execute in the same window
by separating them with the ampersand character.

e.g. F:&\sourcepath&Dir/s *.doc > \targetpath\FilesList.txt&Exit
(F: set the disk  \sourcepath set the folder  Dir/s list the directories and
subs,
pipe them to \targetpath\FilesList.txt   Exit the command.com window.)

I can run my string of commands in the command.com window by shelling to the
environment.
Shell Environ("COMSPEC") & NOSTAY & cmd, vbNormalFocus

But this is no good because my program keeps on running and doesn't wait for
the shell to finish.

There are a zillion examples of shelling to an executable (i.e. an EXE or
BAT or CMD), rather than to the environment, but these fail when I try to
feed them my string of DOS commands.

I could write a batch file, and shell to execute it,  but this seems clumsy,
especially because I also want to do many shells to file copy and delete
commands in the same app.

Thanks for kind assistance and interest.
Signature

Lamont Phemister

expvb - 04 May 2008 18:44 GMT
Why not use VB's Dir function? You can use a sorting routine from here:

http://vbnet.mvps.org/index.html?code/sort/index.html

As for doing Copy/Delete/Move/Rename, you can use SHFileOperation() which
has options for including subfolders. Search the above link for
SHFileOperation() for samples.
Lamont - 05 May 2008 21:13 GMT
Thank you for kindness to reply.  About VB Dir, I understand that successive
executions of this function return the next matching file in a specified
directory.  But my problem requires a list of all the matching files in the
directory and its subdirectories.  The DOS Dir/s function is the only
function I know that recursively searches directories.  I wrote such a thing
in VB once, but it was very complicated and slow - unsatisfactory.

I was reminded in another response that VB has FileCopy and FileMove
functions that I think will suffice nicely for my purpose without needing to
specify directives etc. with SHFileOperation.  
Signature

Lamont Phemister

> Why not use VB's Dir function? You can use a sorting routine from here:
>
[quoted text clipped - 3 lines]
> has options for including subfolders. Search the above link for
> SHFileOperation() for samples.
Larry Serflaten - 05 May 2008 22:34 GMT
> Thank you for kindness to reply.  About VB Dir, I understand that successive
> executions of this function return the next matching file in a specified
> directory.  But my problem requires a list of all the matching files in the
> directory and its subdirectories.  The DOS Dir/s function is the only
> function I know that recursively searches directories.  I wrote such a thing
> in VB once, but it was very complicated and slow - unsatisfactory.

Sifting through gigabyte drives is going to take time, no matter what
method you use.  Using (compiled) VB should be comparable, but the
trick is to search each directory only once.

Try this example:  Add a Listbox and Command button to a new form
and paste in the code below.  Adjust the function call to see different
results....

(The returned item is a Collection which you can use, or save for later)

HTH
LFS

Private Sub Command1_Click()
Dim tmp
 Command1.Enabled = False
 Command1.Caption = "Working"
 Command1.Refresh
 With List1
    .Clear
    .Visible = False
    For Each tmp In FileList("e:\projects", "*.txt", True)
      .AddItem tmp
    Next
    .Visible = True
 End With
 Command1.Caption = "Done"

End Sub

Public Function FileList(ByVal Path As String, Optional Pattern As String = "*.*", Optional Recursive As Boolean = False) As
Collection
Const Slash = "\", Dot = "."
Dim Directories As Collection
Dim tmp As String
Dim PathLength As Long

 'Init variables
 Set FileList = New Collection
 Set Directories = New Collection
 ' Make it case insensative
 Pattern = LCase$(Pattern)
 'Insure Path has trailing slash
 If Right$(Path, 1) <> Slash Then Path = Path & Slash
 PathLength = Len(Path)

 'Check for files and directories in this Path
 'Trapped in case Path is bogus
 On Error Resume Next
 tmp = Path & Dir$(Path & "*.*", vbDirectory)
 On Error GoTo 0
 'Loop for entire directory contents
 Do While Len(tmp) > PathLength
   If IsDirectory(tmp) Then
     'Save for later, refuse . and ..
     If Right$(tmp, 1) <> Dot Then Directories.Add tmp
   Else
     'Add file to listing (case insensative)
     If LCase$(tmp) Like Pattern Then FileList.Add tmp
   End If
   'Get next directory item
   tmp = Path & Dir$()
 Loop
 'Catch sub directories
 If Recursive Then
   Dim tmpD, tmpF As Variant
   'Test each directory in this Path
   For Each tmpD In Directories
     'Recurse directory to add to files list
     For Each tmpF In FileList(tmpD, Pattern, True)
       FileList.Add tmpF
     Next
   Next
 End If
End Function

Public Function IsDirectory(ByVal Path As String) As Boolean
 'Trapped in case Path is bogus
 On Error Resume Next
 IsDirectory = GetAttr(Path) And vbDirectory
End Function
Lamont - 08 May 2008 11:20 GMT
Hello Again.  

I see that I responded to the same message from you twice.  Now I see your
new message.   Thanks for the recursion routine.  I've written lots of tree
searches, but I see that yours is very nice.

About speed for searching large drives:  A recursive search I wrote several
years ago in VB was a snail.   The DOS dir/s is really very fast, so that is
why I have been fixated on shelling to it.   Got to put out a few fires for a
couple of days, but will return to this topic and try your recursive search.  
Again thanks for kind assistance.
It seems like I see your messages saving various drowning software swimmers
on most every topic read about.   Is this a Microsoft "day job" for you?

Best regards, Lamont
Signature

Lamont Phemister

> > Thank you for kindness to reply.  About VB Dir, I understand that successive
> > executions of this function return the next matching file in a specified
[quoted text clipped - 84 lines]
>   IsDirectory = GetAttr(Path) And vbDirectory
> End Function
expvb - 08 May 2008 14:16 GMT
Look for "Recursive Searches" here for samples in using
FindFirstFile/FindNextFile:

http://vbnet.mvps.org/code/fileapi/index.html
Lamont - 09 May 2008 20:06 GMT
Hello expvb, Thank you for kind assistance.  The reference you gave looks
like the Mother Lode of info.  I notice it is for VB.NET.  I expect I'll find
the counterpart for VB6.   I never went to VB.NET because I like the VB6
interpreter.
Best regards, Lamont
Signature

Lamont Phemister

> Look for "Recursive Searches" here for samples in using
> FindFirstFile/FindNextFile:
>
> http://vbnet.mvps.org/code/fileapi/index.html 
expvb - 09 May 2008 22:17 GMT
> Hello expvb, Thank you for kind assistance.  The reference you gave looks
> like the Mother Lode of info.  I notice it is for VB.NET.  I expect I'll
> find
> the counterpart for VB6.   I never went to VB.NET because I like the VB6
> interpreter.

That web site is for VB6 and lower only, despite having vbnet in the name,
which was before Microsoft came up with the dotnet name.

http://vbnet.mvps.org/news/main/index.html#vbnet_eh

Quote: "I have owned and used the VBnet moniker for this site since 1996..."
Karl E. Peterson - 08 May 2008 21:23 GMT
> About speed for searching large drives:  A recursive search I wrote several
> years ago in VB was a snail.   The DOS dir/s is really very fast, so that is
> why I have been fixated on shelling to it.

Have a look at this - http://vb.mvps.org/samples/DirDrill - it seems about the same
speed as DIR/S to me.  For *really* big searches, you may be able to improve on it
with an API based solution.  But not by much.  (I know, I have another class that's
similar, but uses FileFindFirst and FileFindNext instead.)

For the shell and wait, one of the methods in http://vb.mvps.org/samples/Shell32 
oughta work for you, should you choose to keep going with DIR.
Signature

.NET: It's About Trust!
http://vfred.mvps.org

Lamont - 29 Jul 2008 03:43 GMT
Hello Karl,

I came back to this discussion topic after a long delay because of an
another application.   I see that I missed seing your last reply, which gave
a link that solved my current problem.   Thank you for much kindness to
assist.
Signature

Lamont Phemister

> > About speed for searching large drives:  A recursive search I wrote several
> > years ago in VB was a snail.   The DOS dir/s is really very fast, so that is
[quoted text clipped - 7 lines]
> For the shell and wait, one of the methods in http://vb.mvps.org/samples/Shell32 
> oughta work for you, should you choose to keep going with DIR.
Larry Serflaten - 04 May 2008 20:06 GMT
> I could write a batch file, and shell to execute it,  but this seems clumsy,
> especially because I also want to do many shells to file copy and delete
> commands in the same app.

VB has all those commands, why not use it instead of the shell operation?

LFS
Lamont - 05 May 2008 20:11 GMT
Thank you for kindness to reply to my question.   I had forgotten about
MoveFile and Copyfile in VB. That should do nicely for my problem.
I still have the need to recursively search a designated directory and its
subdirectories to create a list of files that match a specified pattern, and
the only way I have found to do that is to "shell to DOS".   A "ShellSort"
was mentioned in a response, but I don't know how that relates to this
problem.  
Signature

Lamont Phemister

> > I could write a batch file, and shell to execute it,  but this seems clumsy,
> > especially because I also want to do many shells to file copy and delete
[quoted text clipped - 3 lines]
>
> LFS
Lamont - 08 May 2008 11:10 GMT
Hello Larry,
Thank you for kind assistance.  You are right about the VBCopy and VBMove.  
I had forgotten those existed.  But, I don't think VB has a directory command
that will recursively search subdirectories.   I believe the the DIR command
in VB will only return the next file in a specified folder.  Am I missing
something?  
Best regards, Lamont
Signature

Lamont Phemister

> > I could write a batch file, and shell to execute it,  but this seems clumsy,
> > especially because I also want to do many shells to file copy and delete
[quoted text clipped - 3 lines]
>
> LFS
Dean Earley - 08 May 2008 11:57 GMT
> Hello Larry,
> Thank you for kind assistance.  You are right about the VBCopy and VBMove.  
[quoted text clipped - 3 lines]
> something?  
> Best regards, Lamont

Dir() itself can't be used recursively. If you want to stick to dir,
then you need to enumerate a folder, build up a list of sub folders,
then once you have finished, recurse and enumerate each of those.

This should be fast speed wise, but can be slowed down significantly by
bad implementation of the array handling for folders (don't resize for
every element)

Or, use the FindFirst/Nextfile API functions which can safely be used
recursively.

Signature

Dean Earley (dean.earley@icode.co.uk)
i-Catcher Development Team

iCode Systems

Karl E. Peterson - 08 May 2008 21:24 GMT
> Dir() itself can't be used recursively. If you want to stick to dir,
> then you need to enumerate a folder, build up a list of sub folders,
> then once you have finished, recurse and enumerate each of those.

Which is recursive, of course.  I "object" to your introductory line, there.
Signature

.NET: It's About Trust!
http://vfred.mvps.org

Dean Earley - 09 May 2008 08:58 GMT
>> Dir() itself can't be used recursively. If you want to stick to dir,
>> then you need to enumerate a folder, build up a list of sub folders,
>> then once you have finished, recurse and enumerate each of those.
>
> Which is recursive, of course.  I "object" to your introductory line, there.

Erm?
Yes, there is a method to use it recursively, but you can't recurse
inside the Dir() loop itself which is what I meant.

Signature

Dean Earley (dean.earley@icode.co.uk)
i-Catcher Development Team

iCode Systems

Karl E. Peterson - 12 May 2008 21:59 GMT
>>> Dir() itself can't be used recursively. If you want to stick to dir,
>>> then you need to enumerate a folder, build up a list of sub folders,
[quoted text clipped - 5 lines]
> Yes, there is a method to use it recursively, but you can't recurse
> inside the Dir() loop itself which is what I meant.

Problem is, people think otherwise when they hear the more common way this is
expressed.
Signature

.NET: It's About Trust!
http://vfred.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.