Jump to content

[BATCH] Search file in all drives - Script help needed


Recommended Posts


Mike88

@gunsmokingman

I always included the file type that's way the VBScript didn't work: It's now working if i don't included the file type. ;)

Here I wrote you this HTA to search for the file it will either say file missing message or fill the list box. When You select an item in the list box it return the name and path to the file

post-5386-0-41595600-1417753547_thumb.pn

 

<TITLE> « Find File » </TITLE> <HTA:APPLICATION ID="FindFile"  APPLICATIONNAME="FFile"  Border="Thin"  BORDERSTYLE ="Complex"  Caption="Yes"  Icon="%Windir%\explorer.exe"  INNERBORDER ="No"  MaximizeButton="No"   MinimizeButton="Yes"   Scroll="No"   SCROLLFLAT ="No"          SingleInstance="Yes"  SysMenu="Yes"             WindowState="Normal"/><STYLE Type='text/css'>  Body{Font-Size:9.25pt;Font-Weight:Bold;Color:Black;       Font-Family:Segoe Ui, Arial,Tahoma,Comic Sans MS;           BackGround-Color:#EFE9E3;Text-Align:Center;Vertical-Align:Top;       Margin-Top:11;Margin-Bottom:1;Margin-Left:4;Margin-Right:4;       Padding-Top:1;Padding-Bottom:1;Padding-Left:4;Padding-Right:4;       Border-Top:0px Transparent;Border-Bottom:0px Transparent;       Border-Left:0px Transparent;Border-Right:0px Transparent;  }  BUTTON{Cursor:Hand;Height:19pt;Width:35pt;         Font-Size:8.25pt;Font-Weight:Bold;Color:#001141;         Font-Family:Segoe Ui, Arial,Tahoma,Comic Sans MS;         Text-Align:Center;Vertical-Align:Middle;         Filter:progid:DXImageTransform.Microsoft.Gradient         (StartColorStr='AliceBlue',endColorStr='LightSlateGray');         Border-Top:1px Transparent;Border-Bottom:1px Transparent;         Border-Left:1px Transparent;Border-Right:1px Transparent;         Padding-Top:0;Padding-Bottom:2;Padding-Left:0;Padding-Right:0;         Margin-Top:1;Margin-Bottom:1Margin-Left:1;Margin-Right:1;  }  .Tx1{Font-Size:8.25pt;Font-Weight:Bold;  Font-Family:Segoe Ui, Arial,Tahoma,Comic Sans MS;}  Input{Font-Size:8.25pt;Font-Weight:Bold;Color:#004141;}  TD{Font-Size:8.25pt;Font-Weight:Bold;Color:#414141;}  FONT{Font-Size:8.25pt;Font-Weight:Bold;Color:#414141;}  FONT.FT1{Font-Size:8.25pt;Font-Weight:Bold;Color:Red;}</STYLE><SCRIPT LANGUAGE='JScript'>//-> Resize And Place In Approx center window.resizeTo(501,225) window.moveTo(screen.availWidth/2-(501/2),screen.availHeight/2-(173/2))//-> Search Button click  function MyButton1(){   if(Str.value.length==0){alert("Please fill in a file name");}   else{SearchFile(Str.value);}}   </SCRIPT> <SCRIPT LANGUAGE="VBScript">  Dim C1, Col, Obj, Wmi, Var'-> Start The WMI Query And Then Add To Select Or No File Found  Function SearchFile(F)     Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")   Set Col = Wmi.ExecQuery("Select * from CIM_DataFile Where FileName='" & F & "'")   If Col.count = 0 Then    alert("Can Not Find This File : " & F)    Exit Function   Else    For Each Obj in Col    C1 = C1 + 1    Set Sel = Document.createElement("OPTION")     Sel.Text = Obj.FileName & "." & Obj.Extension     Sel.Value = Obj.Drive & Obj.Path & Obj.FileName & "." & Obj.Extension      If C1 Mod 2 Then       Sel.style.backgroundcolor = "#C9C9C9"        Sel.style.color = "#3A3A3A"      Else        Sel.style.backgroundcolor = "#E9E9E9"        Sel.style.color = "#235779"      End If      Found.Add(Sel)    Next    End If  End Function </SCRIPT><BODY> <TABLE> <TD><FONT CLASS=''>Type in the file name with no extension  </FONT></TD>  <TD><INPUT Type='Text' ID='Str' Size=28 MAXLENGTH=28></TD>  <TD><BUTTON ID='Btn1' OnClick="MyButton1()">Search</BUTTON></TD> </TABLE><TABLE> <FONT Class='FT1'>  Note the Search Button Query might make the HTA seem like it not reponding,  this is the result of the WMI Query and this is normal</FONT></TABLE><TABLE><TD> <select size="1" ID='Found' name="Found"  onChange='if(Found.length >= 1){alert(Found.options[Found.selectedIndex].text+"\n\r"+Found.value)}' Class='Tx1'> </select></TD></TABLE></BODY> 
FindFile.zip
Link to comment
Share on other sites

Thanks for the HTA script you always do great HTA scripts. :)

But unfortunately i have to use a batch script that automatically search for a file in all drives and execute it if found.

This batch script will be also a part of some other batch scripts, that's way i have to do it in Batch code. :(

Yzöwl batch script works but i still have to find out how to change the script so that it will automatically execute the file if it was found in a drive.

Edited by Mike88
Link to comment
Share on other sites

Please be very careful.

 

I would not recommend running another script based purely off a multiple drive file name search.

@ECHO OFF&SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSIONSET/P "TOFIND= Please enter the file search name: "ECHO(&ECHO( Searching Drives for '%TOFIND%'&ECHO(FOR /L %%A IN (90 -1 67) DO (CMD/C EXIT/B %%A    FOR /F "TOKENS=*" %%B IN (        'CALL DIR/B/S/A-D "%%=EXITCODEASCII%%:\%TOFIND%" 2^>NUL') DO (CALL %%B))
Link to comment
Share on other sites

Thanks this is now working perfect. :w00t:

Any reason why you used "90 -1 65" insteal of "MOUNTVOL"? I never saw "90 -1 65" in a batch script before. Would be nice to know how this script works because it does not work with "@ECHO ON". :)

Edited by Mike88
Link to comment
Share on other sites

@Mike88

Depending on the real use of the batch, it would be much more convenient and "safe" to have the file on *any* drive BUT on a known path, like root or (say) \mytagfiles\ , depending on the number of files/directories/subdirectories on the filesystem the search for it could take a long time.

The FOR /L loop assigns all valuee from 90 to 65 step -1.

The EXITCODEASCII "translates" them to ASCII, 65 is A and 90 is Z.

Some info on EXITCODEASCII is here, JFYI:

http://www.robvanderwoude.com/ntset.php

and here you can find a simple batch that uses it:

http://reboot.pro/topic/2986-yacbfc-dec2hexcmd-and-hex2deccmd/

http://reboot.pro/topic/2986-yacbfc-dec2hexcmd-and-hex2deccmd/?p=136708

jaclaz

Link to comment
Share on other sites

Okay now i'm understanding better how the script works, i nerver use ASCII in a batch file before. :)

I wish i could also included the subfolders but i think that would make everything more complicated, because only the last 2 subfolders will be the same everything at the bgeing of the path is not always the same.

Edited by Mike88
Link to comment
Share on other sites

I wish i could also included the subfolders but i think that would make everything more complicated, because only the last 2 subfolders will be the same everything at the being of the path is not always the same.

Still, it would make IMHO things more "sound".

Can you post a practical example of your (intended) setup?

Maybe there is some more efficient way to look for the file(s) :unsure:

I mean, would something *like*:

DIR/B/S/AD | FIND /i "name_of_the_folder"

be faster than:

DIR/B/S/A-D | FIND /i "name_of_the_file"

And (still cannot say if more efficient) have you actually tried FORFILES?

jaclaz

Link to comment
Share on other sites

I would use FORFILES if Win2k and WinXP had it too. :)

 

Here is an example what i meant before with the first subfolders.

Depending on the situation the directory where the file could be located would be like this:

<drive>:\OEM\RunOnceEx\test.cmd<drive>:\<path>\OEM\RunOnceEx\test.cmd<drive>:\<path>\<path>\OEM\RunOnceEx\test.cmd<drive>:\<path>\<path>\<path>\OEM\RunOnceEx\test.cmd...
Edited by Mike88
Link to comment
Share on other sites

Good. :)

Now, let's remove (temporarily) the looping into various drives, and let's limit the test to a single drive.

Try your on your own setup (possibly do this on a "filled" volume).

 

@ECHO OFFSETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSIONSET drive=C:SET folder=OEM\RunOnceExSET rootfolder=OEMSET partfolder=RunOnceExSET file=newtest.cmdECHO %TIME%FOR /F %%A IN ('DIR /B/S/AD %drive%\ ^|FIND "%folder%"') DO IF EXIST %%A\%file% ECHO !TIME! FOUND %%A\%file%& GOTO :NEXT1:NEXT1ECHO %TIME%FOR /F %%A IN ('DIR /B/S/A-D %drive%\%file% ^|FIND "%file%"') DO ECHO !TIME! FOUND %%A&GOTO :NEXT2:NEXT2ECHO %TIME%FOR /F %%A IN ('DIR /B/S/A-D %drive%\%file%') DO ECHO !TIME! FOUND %%A&GOTO :NEXT3:NEXT3ECHO %TIME%FOR /F %%A IN ('DIR /B/S/AD %drive%\%partfolder%') DO IF EXIST %%A\%file% ECHO !TIME! FOUND %%A\%file%& GOTO :NEXT4:NEXT4ECHO %TIME%FOR /F %%A IN ('DIR /B/S/AD %drive%\%rootfolder%') DO IF EXIST %%A\%partfolder%\%file% ECHO !TIME! FOUND %%A\%partfolder%\%file%& GOTO :NEXT5:NEXT5:OUTOFHEREECHO %TIME%
You get the idea :).

The point is that (hopefully/maybe) making the list for only directories might be faster and compensate the additional IF exist.  

It is entirely possible that *somehow* Windows "caches" results of a previous DIR command :unsure:, so each snippet should probably be tested "separately".

jaclaz

Link to comment
Share on other sites

Which version of the command is faster? (they should ALL work).

 

In my quick tests ":NEXT4" seemed slightly faster, or, if you prefer, I tentatively set in a "slower to faster" order in the batch. :unsure:

 

Using the IF EXIST should also represent an added "check", if the .cmd file will end up named with a rather common name, to avoid "false positives" or "filename collisions".

 

jaclaz

Link to comment
Share on other sites

This is the results i get on a Hard Disk that has a lot of files on it:

22:32:10,3122:41:35,44 FOUND F:\xfolder1\OEM\RunOnceEx\newtest.cmd22:41:35,4422:41:50,71 FOUND F:\xfolder1\OEM\RunOnceEx\newtest.cmd22:41:50,7122:42:05,87 FOUND F:\xfolder1\OEM\RunOnceEx\newtest.cmd22:42:05,8722:42:21,01 FOUND F:\xfolder1\OEM\RunOnceEx\newtest.cmd22:42:21,0122:42:35,93 FOUND F:\xfolder1\OEM\RunOnceEx\newtest.cmd22:42:35,93Press any key to continue . . .

[EDIT]

I'm having the feeling that this is not really faster because if i look at the "DIR" command then it will always start scanning all the directories from A to Z.

The first "DIR" scan on an Hard Disk will be always the slowest if i run it a second time then i get a different result.

22:52:35,6822:52:56,26 FOUND F:\xfolder1\OEM\RunOnceEx\newtest.cmd22:52:56,2622:53:12,12 FOUND F:\xfolder1\OEM\RunOnceEx\newtest.cmd22:53:12,1222:53:28,04 FOUND F:\xfolder1\OEM\RunOnceEx\newtest.cmd22:53:28,0422:53:44,40 FOUND F:\xfolder1\OEM\RunOnceEx\newtest.cmd22:53:44,4022:54:00,89 FOUND F:\xfolder1\OEM\RunOnceEx\newtest.cmd22:54:00,89Press any key to continue . . .
Edited by Mike88
Link to comment
Share on other sites

Well, the result will be slightly different each time, and of course "afolder" will be listed before "xfolder".

Hint: If you have any control on the first level directory, you'd better name it starting with "a" than with "z".

 

The "hope" is that, on average, there are far more files on a system than directories and that this will speed up (a little bit) the execution, and that a "higher" level such as "\OEM\" will be reached faster than a "lower" one.

 

You can try also with :unsure::

 

:NEXT5ECHO %TIME%FOR /F %%A IN ('DIR /B/S/OG %drive%\%rootfolder%') DO IF EXIST %%A\%partfolder%\%file% ECHO !TIME! FOUND %%A\%partfolder%\%file%& GOTO :NEXT5
jaclaz
Link to comment
Share on other sites

Since it is a sort of known path, then perhaps a tag file could be used like many others use (probably with the path in it) or even a script put in the root of the drive to set certain variables.

 

I.e.

oem.cmd

rem Paths to RunOnceEx folderset oem=%~dp0subfolder\oem\set runonceex=%~dp0subfolder\oem\runonceex\

main.cmd (searchs for oem.cmd and calls it if it exist)

@echo offsetlocal enabledelayedexpansionecho %time%for %%A in (c d e f g h i j k l m n o p q r s t u v w x y z) do (	set drive=%%A:	if exist !drive! (		if exist !drive!\oem.cmd call !drive!\oem.cmd	))echo %time%if defined oem echo oem = !oem!if defined runonceex echo runonceex = !runonceex!pauseendlocalgoto :eof

output:

 3:15:17.36 3:15:17.39oem = e:\subfolder\oem\runonceex = e:\subfolder\oem\runonceex\Press any key to continue . . .

Just one of many alternatives that could be used.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...