Jump to content

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


Recommended Posts

I must be missing something :unsure:, but if you "fix" the path to:

%~dp0subfolder\oem\

%~dp0subfolder\oem\runonceex\

in oem.cmd, you actually KNOW the path to \OEM\RunOnceEx\ and the whole exercise is vain, you can directly hardcode it to \subfolder\oem\runonceex\ in the "main" batch.

 

I mean, the expected situation is this one:

http://www.msfn.org/board/topic/173102-batch-search-file-in-all-drives-script-help-needed/?p=1090274

 

Of course putting the actual file in root of the drive (or in a fixed, known path) would be much more convenient (and much, much faster), but if the OP really wants this (more complex than needed BTW) setup, he will probably have his own reasons for it.

 

One could even use a self-deleting :w00t: batch in root, if the issue is about "cluttering" the root of the drive, but we miss a description of the actual "final goal" and the environment/reason why this is needed.

 

jaclaz

Link to comment
Share on other sites


MHz, The A-Z driver search script was my old script but i had to look for something else because it can't be used with drivers like with a Floppy Disk Drive.

And using a fixed path is out of my control. :)

 

jaclaz ,Your last script looks like it's faster. Now i have to see how to incuded it into the script from Yzöwl. ;)

Edited by Mike88
Link to comment
Share on other sites

Just for fun!

@ECHO OFFSETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION(SET _DT=\oem\runonceex\)(SET _FN=test.cmd)ECHO(&ECHO( Searching Drives for '%_DT%%_FN%'&ECHO(FOR /L %%A IN (90 -1 67) DO (CMD/C EXIT/B %%A    FOR /F "TOKENS=*" %%B IN ('DIR/B/S/A-D-R-S-H/L "!=EXITCODEASCII!:\%_FN%"^ 2^>NUL^|FIND "%_DT%%_FN%"') DO (SET "_FP=%%B"        SET "_TT=!_FP:%_DT%%_FN%=!"        ECHO( %_DT%%_FN% Found in !_TT! [!_FP!]&ECHO())PAUSEEXIT/B
Link to comment
Share on other sites

Here is a VBS script that will look only for test.cmd and when found will run the test.cmd

 

 Dim Obj Dim Wmi :Set Wmi = GetObject( _  "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") Dim Col :Set Col = Wmi.ExecQuery( _ "Select * from CIM_DataFile Where Extension = 'cmd' And FileName='test'")  If Col.count = 0 Then  WScript.Echo "Missing : Test.cmd"  WScript.Quit  Else    For Each Obj in Col    Wscript.Echo Obj.Drive & Obj.Path & Obj.FileName & "." & Obj.Extension    CreateObject("Wscript.Shell").Run("Cmd /c " & _    Obj.Drive & Obj.Path & Obj.FileName & "." & Obj.Extension),1,True    Next   End if
Tested Output, Z my mapped network drive

c:\users\gunsmokingman\desktop\test.cmd

e:\test.cmd

z:\test.cmd

Test.cmd I tested the above script with

@Echo Off && CLS && MODE 55,5 && COLOR 8F && TITLE TEST.CMDEcho. && Echo Test Command && ping -n 3 127.0.0.1>nul

@gunsmokingman

If you're going to use the slow Wmi method then why Select all properties,

  • , from CIM_DataFile instead of selecting just the four properties you need, [Drive Path FileName Extension].

    e.g. SELECT Drive,FileName,Extension,Path FROM CIM_DataFile WHERE…

1:\ I am not aware of all properties of WMI, other than this will check every available drive, found the file on my map drive

2:\ I set Col properties because it give you a simple way of saying yes only if FILE.EXT

than list or do something, if not closes the script.

3:\ You may think it slow but at least I have simple error control with the if something and no need to define a path, this will execute test.cmd anywhere it found.

Link to comment
Share on other sites

@gunsmokingman

 

If you're going to use the Wmi method then why Select all properties, [*], from CIM_DataFile instead of selecting just the four properties you need, [Drive Path FileName Extension].

 

e.g. SELECT Drive,FileName,Extension,Path FROM CIM_DataFile WHERE…

 

<EDIT />

I'd be more tempted to use LIKE and ignore anyone using Windows 2000

Dim objWMIService, colQuery, objstrComputer = "."Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _  & strComputer & "\root\cimv2")Set colQuery = objWMIService.ExecQuery("SELECT Extension, Name FROM " & _  "CIM_DataFile WHERE Extension = 'cmd' AND Name LIKE " & _  "'%\\OEM\\RunOnceEx\\test.cmd'")If colQuery.Count = 0 Then    WScript.Echo "File not found"    WScript.QuitElse     For Each obj In colQuery        Wscript.Echo obj.Name    Next End If
Edited by Yzöwl
example added
Link to comment
Share on other sites

Here is why VBS is so much more better than CMD please replicate the following script, in CMD

post-5386-0-66290200-1418154884_thumb.pn

 

 Dim A, F, i, Obj, R, V Dim Act :Set Act = CreateObject("Wscript.Shell") Dim Wmi :Set Wmi = GetObject( _  "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") A = Array("Test.cmd","Test.exe", "Test.msi")    For Each i In A   F = Split(i,".")   V=True   Dim Col :Set Col = Wmi.ExecQuery( _   "Select * from CIM_DataFile Where Extension = '"&F(1)&"' And FileName='"&F(0)&"'")     If Col.count = 0 Then      R =  R & vbCrLf & "Missing : " & i    Else      For Each Obj in Col      F = Chr(34)& Obj.Drive & Obj.Path & Obj.FileName & "." & Obj.Extension &Chr(34)            R = R & vbCrLf & "Confirm : " & F       If V = True Then        V = False       Act.Run("Cmd /c " & F),1,True       R = R & vbCrLf & "Install : " & F       End If       Next      End If     Next        Act.Popup vbTab & "Results" & vbCrLf & R,300,"Query Results",4128
I could code this in MS Jscript Vb.Net and you CMD that is so 1985

I forgot you know Powershell, myself I chose not to learn that language

but I can read it and just not my cup of tea.

Just to note you can add any install switch to each array item

This script will only execute each item one time, but will list found file

in the final report.

Link to comment
Share on other sites

Another idea:

C:\>attrib/s R:\test.cmd | find/i "\oem\runonc~1\test.cmd"
A HR TEST.CMD R:\OEM\RUNONC~1\test.cmd
A TEST.CMD R:\Z\OEM\RUNONC~1\test.cmd


Edited by jumper
See formatting: web.archive.org/web/20150701114147/http://www.msfn.org/board/topic/173102-batch-search-file-in-all-drives-script-help-needed
Link to comment
Share on other sites

@gunsmokingman

The point is (was) IMHO to find a "fast" (as opposed to or at least not necessarily "elegant") way to find that stupid file, in my (BTW VERY 1984ish :w00t::ph34r:) experience WMI is not that fast (please read as usually slow) and it may actually not be running (or running properly) on the target machines.

Could you try "timing" your WMI approach? :unsure:

 

@jumper

Another interesting idea :), that should also be timed to have a comparison with the other tests.

 

jaclaz

Link to comment
Share on other sites

@gunsmokingman

 

If you cannot understand that there is no benefit in making collections of all of the properties of CIM_DataFile when you only require a few of them, then you may be better advised moving forward to refrain from taking your usual path.

Link to comment
Share on other sites

@gunsmokingman

 

If you cannot understand that there is no benefit in making collections of all of the properties of CIM_DataFile when you only require a few of them, then you may be better advised moving forward to refrain from taking your usual path.

From your point of view, yours will not find every instance of Test.cmd, mine will finds every instance of Test.cmd, regardless of path.

post-5386-0-82205600-1418167573_thumb.pn

Results From Mine

---------------------------Query Results---------------------------	ResultsConfirm : "d:\oem\runonceex\test.cmd"Install : "d:\oem\runonceex\test.cmd"Confirm : "e:\test.cmd"Confirm : "k:\oem\runonceex\Test.cmd"Confirm : "z:\oem\runonceex\test.cmd"Confirm : "z:\oem\runoncex\test.cmd"Confirm : "z:\test.cmd"Confirm : "z:\tempmusic\test.exe"Install : "z:\tempmusic\test.exe"Missing : Test.msi---------------------------OK   ---------------------------
FileSystemObject with checks for drive isready and FileExists and Execute it only Once

post-5386-0-70340100-1418166441_thumb.pn

Dim Act :Set Act = CreateObject("Wscript.Shell")Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Dim Drv,Rpt, Var, Z :Z="\oem\runonceex\test.cmd"Var = True   For Each Drv In Fso.Drives     If Drv.IsReady Then       If Fso.FileExists(Drv & Z) And Var = True Then      Rpt=Rpt&vbCrLf&"Installed File : "&Drv&Z      Var=False :      Act.Run(Drv & Z),1,true      ElseIf Fso.FileExists(Drv & Z) And Var = False Then      Rpt=Rpt&vbCrLf&"Found File: "&Drv&Z     Else     Rpt=Rpt&vbCrLf&"Missing File: "&Drv&Z     End If    End If   Next   Act.Popup vbTab & "Results" & vbCrLf & Rpt,300,"Query Results",4128
Link to comment
Share on other sites

@gunsmokingman

Please take note of the quoted posts, (from this topic), below.

 

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. :(

 

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.

 

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...

If you wish for Mike88 to be persuaded to use a vb script, even if run as part of batch routine then you need to fulfil the requirements first.

Link to comment
Share on other sites

My script that I posted could work for all of Mike88 needs, all he would have to do is fill the array with the Batch files he want to run and then they would be executed. My script does not need to be in the same folder as the file.Type and can be run from anywhere.

My goal was to show another way of doing something without the need to hardcode any paths.

Here is a Microsoft Jscript that does almost the same thing as the VBS, the difference

it does not report missing file in the query.

 

var Act = new ActiveXObject("Wscript.Shell");var Wmi = GetObject("winmgmts:\\\\.\\root\\cimv2")var A = new Array("Test.cmd","Test.exe","Test.msi")var Rpt="", V1=""; c34=String.fromCharCode(34); c92=String.fromCharCode(92)  for(var i = 0; i<A.length; i++){ var f =A[i].split(".")  Rpt=Rpt+"\n\r"+"Processing : " + A[i]; V1=true   var Obj = Wmi.ExecQuery(  "Select * from CIM_Datafile Where Extension = '"+f[1]+"' and FileName='"+f[0]+"'")   for (var e=new Enumerator(Obj); !e.atEnd(); e.moveNext()){  var s = e.item();F = c34+s.Drive+s.Path+A[i]+c34;   if(V1==true){F=Replace(F, c92,c92+c92)   V1=false;Rpt=Rpt+"\n\r"+"Installed : " + F;    Act.Run("Cmd /c " + F,1,true)   }else{Rpt=Rpt+"\n\r"+"Found File : " + F;}}}Act.Popup("\tResults" + "\n\r" + Rpt,300,"Query Results",4128) //--> VBS Script Custom Functions For Text  function LCase(str){return str.toLowerCase();}  function Left(str, n){return str.substring(0,n)}  function Right(str, N){return str.substring(str.length, str.length - N);}  function Replace(Arg,R1,R2){var i = Arg.split(""), RTurn   for(j=0; j < i.length; j++){RTurn += i[j].replace(R1,R2);}        if(Left(LCase(RTurn),"undefined".length)=="undefined"){   return Right(RTurn, RTurn.length-"undefined".length)   }else{return RTurn}}
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...