robe007
robe007

Reputation: 3927

Recursively search for specific files in all locations, including hidden with batch

I want to search all files with .xlsx extension, for now I'm using this:

for /R c:\ %%f in (*.xlsx) do set target=%%f
echo %target%

But, only searchs in "C" and does not includes the hidden files. So, my questions:

1) How can I search in all locations, I mean: C, D, E ... drives?

2) How I can search for hidden files too?

Upvotes: 2

Views: 150

Answers (3)

Hackoo
Hackoo

Reputation: 18827

You can try something like that :

@echo off
Color 9A & Mode con cols=70 lines=5
Set "Ext=xlsx"
Title %~nx0 to search all *.%Ext% files
set "Log=%~dp0%Ext%_PATH.txt"
If Exist "%Log%" Del "%Log%"
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=3" ^|find /i ":"') do (
    set "Fixed_Drive=%%i"
    Call :ShowMsg !Fixed_Drive!
    (
        @For /f "delims=" %%x in ('Dir /b /s /a "!Fixed_Drive!\*.%Ext%"') do (
            @echo "%%x" 
        )
    )>> "%Log%"
)
Start "" "%Log%"
Exit
::******************************************************************
:ShowMsg
Cls
echo(
echo                ***********************************
Echo                 Please wait a while Scanning "%~1"
echo                ***********************************
Timeout /T 2 /nobreak>nul
exit /b
::******************************************************************

Edit :

To make a multiple search by extension like .xlsx .docx at the same time and get a separte log archive per each extension, you should try like this way :

@echo off
Color 9A & Mode con cols=70 lines=5
Set "Ext=xlsx docx"
For %%a in (%Ext%) Do (
    if exist "%~dp0%%a_PATH.txt" del "%~dp0%%a_PATH.txt"
    Call :Search "%%a" "%~dp0%%a_PATH.txt"
)

For %%a in (%Ext%) Do ( 
    If Exist "%~dp0%%a_PATH.txt" Start "" "%~dp0%%a_PATH.txt"
)
Exit
::**********************************************************************************
:Search <Ext> <Log>
Cls
Title %~nx0 to search all "*.%~1 files"
echo(
echo                ***********************************
Echo                 Please wait a while Scanning "%~1"
echo                ***********************************
Timeout /T 2 /nobreak>nul
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=3" ^|find /i ":"') do (
    set "Fixed_Drive=%%i"
    (
        @For /f "delims=" %%x in ('Dir /A:-D /b /s  "!Fixed_Drive!\*.%~1"') do (
            @echo "%%x" 
        )
    )>> "%~2"
)
exit /b
::**********************************************************************************

Upvotes: 2

Compo
Compo

Reputation: 38604

The Where command searches 'hidden files', so using a method similar to Hackoo's:

@Echo(Searching...&@(For /F "Skip=1" %%A In ('WMIC LogicalDisk Where^
    "DriveType>1 And DriveType!=5 And FreeSpace Is Not Null" Get DeviceID'
    ) Do @For %%B In (%%A) Do @For /F "Delims=" %%C In (
        'Where/F /R %%B\ *.xlsx') Do @Echo=%%C)&Timeout -1

Upvotes: 2

Tanaike
Tanaike

Reputation: 201368

At "for in", hidden files cannot be searched. Using option "/a" of "dir", hidden files can be srarched. To search all of files with the extension of "xlsx" on a drive, I thought following method. But about this, each drive name has to input by user.

Drive C:

dir c:\*.xlsx /b /s /a

Drive D:

dir d:\*.xlsx /b /s /a

If you want to use data from "dir" at a batch file, how about following script?

@echo off
setlocal enabledelayedexpansion
set ct=0
for /f "usebackq tokens=*" %%a in (`dir c:\*.xlsx /b /s /a`) do (
  set target[!ct!]=%%a
  set /a ct=!ct!+1
)
set /a ct=%ct%-1
for /l %%i in (0,1,%ct%) do echo !target[%%i]!

Upvotes: 2

Related Questions