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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching for specfic files 3

Status
Not open for further replies.

thysonj

Programmer
Jul 6, 2001
240
US
How do I search for a file on a drive?
I am trying to use the fileexists but I cannot figure it out. I am looking for a specific file on a system and not just a certain directory. I cannot input a directory path since the file may be in different places depending on the system.
 
U could use Scripting.FileSystemObject to access the drives dirs and tha files from your HDD but u need to implement the search function...
________

George
 
i know i can use dir *.sys/s to find a path for a file...
how can I get this path in a string...???
if i get it in a string then i can just use an IF statement to execute the proper function
 
Hello, thysonj.

Searching multiple file (*.sys, say) on multiple drive may take a good while. If you accept the enumeration of drive letters as an input rather than a sub-task of the script itself, the following script may serve the purpose, or at least get by the problem.

I find dir <drive:\> filename /s /b very acceptable for the job. I would prefer it over the puristic approach, which is definitely possible.

The following script set the drive set to be searched completely from root downward to (c,d,e,f) put in a string array. Modify it by adding more or taking away some drives to suit your need. (Suggest you avoid c drive during testing in order not to inflict anything on your system drive.)The array can be the output of some other script line using standard technique to complement the job, if you want automation there too.

It searches for files *.sys.

It output the result to the file: found.txt and it is placed in the same directory of the script itself.

The output to the found.txt is in the format very convenient for further use. It is like the following :

C:\CONFIG.SYS
C:\WINDOWS\HIMEM.SYS
...

No other useless stuff there.

Try and see how you like it.

regards - tsuji

'----------------------------------/tsuji/--------------
Option Explicit

Const findresult = &quot;found.txt&quot;
Const target_file = &quot;*.sys&quot;

Dim search_drive_array, drive_string
search_drive_array = Array(&quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;)
drive_string = Join(search_drive_array, &quot;,&quot;)
drive_string = &quot;(&quot; & drive_string & &quot;)&quot;

Dim outfile
outfile = WScript.ScriptFullName
outfile = Left(outfile, InStrRev(outfile, &quot;\&quot;))
outfile = outfile & findresult

Dim wsh
Set wsh = WScript.CreateObject(&quot;WScript.Shell&quot;)

wsh.Run &quot;%comspec% /c del &quot; & outfile & &quot; > nul&quot;, 0 , True

'Attention : May take a long while. Let the system run its course.
wsh.Run &quot;%comspec% /c For %%D in &quot; & drive_string & _
&quot; Do dir %%D:\&quot; & target_file & &quot; /s /b >> &quot; & outfile , 0, True

WScript.Quit
'----------------------------------/tsuji/--------------
 
I was just wondering if anyone could get this to work. I tried running it and it seems to do nothing.

Any help would be appreciated.

Thanks
 
Hello, CincyTechie.

I'm standing by to elucidate. Could this be the source of your problem?

&quot;It output the result to the file: found.txt and it is placed in the same directory of the script itself.&quot;

regards - tsuji
 
I wish that was it. I waited for about an hour (I noticed you said it could take a while, so I just did something else while I thought it was running.)

There was no file (found.txt) so I debated whether it was still running or whether it did not work. I first searched the entire HD to see if I could find found.txt. It was not there. So, I modified the script to search the A drive for a file and I put that file on the A drive. I ran the script and found.txt was still not created. Finally, I modified the script again to Echo 'Program finished' when it was done, and tried running it a third time. This time as soon as I ran the script, it gave me the message 'Program Finished' which was right above the WScript.Quit function.

Other than those modifications, I ran the script exactly as you posted it.

Note: I saved the script as a found.vbs on my machine (Win2K) and just double clicked on the vbs file to run.
 
Hello again.

You're quite right. The script is not perfect, short of progress bar like visual display. But to do so would need some 3rd party ocx which I would be reluctant even to install and complicate the matter for a forum like this. Your adding a message box is excellent. Thanks for your feedback.

regards - tsuji
 
Do you have any idea why the script is not working?

Here is what I have. I have a file calle text.txt on the a drive that I am looking for.

'----------------------------------/tsuji/--------------
Option Explicit

Const findresult = &quot;found.txt&quot;
Const target_file = &quot;*.txt&quot;

Dim search_drive_array, drive_string
search_drive_array = Array(&quot;a&quot;) ', &quot;d&quot;, &quot;e&quot;, &quot;f&quot;)
drive_string = Join(search_drive_array, &quot;,&quot;)
drive_string = &quot;(&quot; & drive_string & &quot;)&quot;

Dim outfile
outfile = WScript.ScriptFullName
outfile = Left(outfile, InStrRev(outfile, &quot;\&quot;))
outfile = outfile & findresult

Dim wsh
Set wsh = WScript.CreateObject(&quot;WScript.Shell&quot;)

wsh.Run &quot;%comspec% /c del &quot; & outfile & &quot; > nul&quot;, 0 , True

'Attention : May take a long while. Let the system run its course.
wsh.Run &quot;%comspec% /c For %%D in &quot; & drive_string & _
&quot; Do dir %%D:\&quot; & target_file & &quot; /s /b >> &quot; & outfile , 0, True

WScript.Echo &quot;Program Finished&quot;
WScript.Quit
'----------------------------------/tsuji/--------------
 
It works for me, though. Check out if %comspec% is well defined for your box by clicking out the dos window and type
set <hard enter>
see if it appears.

- tsuji -
 
It shows ComSpec = c:\winnt\system32\cmd.exe

Is this what it is supposed to be?

Thanks for all of your help. I really appreciate it.

 
cincytechie,

The Comspec is OK. What does not is that the calling of parameter follows different convention. I just check it out on my win2000 machine. I'm quite upset on this portability or the lack of it. There might be a proper switch to enforce this. But, if I need to put another switch, I would rather embed the check of OS within the script itself.

As a stop-gap measure, here is what you should modify the commandline. Rather than using %%D, replace it by %D :

wsh.Run &quot;%comspec% /c For %D in &quot; & drive_string & _
&quot; Do dir %D:\&quot; & target_file & &quot; /s /b >> &quot; & outfile , 0, True

This should work on winNT/2000 m/c. The script is deviced for multiple drive search, it is this device which gets into difficulty in portability. It can be amended, but I won't do it here.

Thank you for raising this to my attention.

regards - tsuji
 
Hello all.

What follows is a revision of the script which should then work across win9x and winNT/2000. I commented the lines nolonger used to preserve the trace and add a couple new lines. It is self-explanatory.

Thanks for your attention.

regards - tsuji

'----revised for portability--------/tsuji/--------------
Option Explicit

Const findresult = &quot;found.txt&quot;
Const target_file = &quot;*.sys&quot;

Dim search_drive_array
search_drive_array = Array(&quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;)

'Dim drive_string
'drive_string = Join(search_drive_array, &quot;,&quot;)
'drive_string = &quot;(&quot; & drive_string & &quot;)&quot;


Dim outfile
outfile = WScript.ScriptFullName
outfile = Left(outfile, InStrRev(outfile, &quot;\&quot;))
outfile = outfile & findresult

Dim wsh
Set wsh = WScript.CreateObject(&quot;WScript.Shell&quot;)

wsh.Run &quot;%comspec% /c del &quot; & outfile & &quot; > nul&quot;, 0 , True

Dim Drv
For Each Drv in search_drive_array
wsh.Run &quot;%comspec% /c dir &quot; & Drv & &quot;:\&quot; & target_file & &quot; /s /b >> &quot; & outfile, 0, True
Next


'Attention : May take a long while. Let the system run its course.
'wsh.Run &quot;%comspec% /c For %%D in &quot; & drive_string & _
' &quot; Do dir %%D:\&quot; & target_file & &quot; /s /b >> &quot; & outfile , 0, True


Set wsh = Nothing
WScript.Quit
'----revised for portability--------/tsuji/--------------
 
Tsuji,

Could you tell me how to modify the script to look for two seperate types of files.

It works great to look for *.mdb files, but I want to look for *.mdb and *.mde files.

Is there any way to do this.

TIA
 
Hello, cincytechie.

Here is further patchwork to accommodate your need. It is self-explanatory.

As a side note, I put &quot;*.htm&quot;, &quot;*.html&quot;, &quot;*.jpg&quot; and &quot;*.jpeg&quot; there just to draw your attention to certain feature of running dir in cmdline.

regards - tsuji

'-------------------------------------------------------------------
Option Explicit

Const findresult_prefix = &quot;found_&quot;
Const findresult_postfix = &quot;.txt&quot;

Dim findresult_array, target_file_array, search_drive_array

'---------------Parameters to be edited for a specfic job-----------------
target_file_array = Array(&quot;*.mdb&quot;,&quot;*.mde&quot;,&quot;*.htm&quot;,&quot;*.html&quot;,&quot;*.jpg&quot;,&quot;*.jpeg&quot;)
search_drive_array = Array(&quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;)
'------------------------------------------------------------------------

Dim i, findresult, target_file
ReDim findresult_array(UBound(target_file_array))
For i = 0 To UBound(target_file_array)
target_file = target_file_array(i)
findresult_array(i) = findresult_prefix & _
Right(target_file,len(target_file)-InStrRev(target_file,&quot;.&quot;)) & _
findresult_postfix
Next

Dim wsh, outfile, Drv
Set wsh = WScript.CreateObject(&quot;WScript.Shell&quot;)

For i = 0 To UBound(target_file_array)

findresult = findresult_array(i)
target_file = target_file_array(i)

'WScript.Echo &quot;Processing on target file : &quot; & target_file & &quot; begins.&quot;

outfile = WScript.ScriptFullName
outfile = Left(outfile, InStrRev(outfile, &quot;\&quot;))
outfile = outfile & findresult

wsh.Run &quot;%comspec% /c del &quot; & outfile & &quot; > nul&quot;, 0 , True

For Each Drv in search_drive_array
wsh.Run &quot;%comspec% /c dir &quot; & Drv & &quot;:\&quot; & target_file & &quot; /s /b >> &quot; & outfile, 0, True
Next

'WScript.Echo &quot;Processing on target file : &quot; & target_file & &quot; is done.&quot;
Next

Set wsh = Nothing
WScript.Quit
'-------------------------------------------------------------------
 
tsuji,

I'm trying to use your above script that works with 9x/NT. I need to be able to write the files to a specific drive\folder. Is that what your outfile var is for?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top