Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

[PowerShell] File Type Locator Function

So I'm still translating all my scripts when I have the chance from batch to PowerShell. I just re-wrote this one, but it is waaay better than the original. I had some help from Google with this one, but I hope you can find a use for it.

Any feedback is more than welcome. 🎉

Code
# -- Create function
Function FileFind {
#--- Create variable and get input from user
$File = Read-Host -Prompt "file extension to find: "
$Location = Read-Host "directory to search in: "
#--- Use Test-Path to check if path is true/false
$Result = (Test-Path($Location));
# --- search directory
IF ($Location)
{
#--- Create directory variable to recursively look through
    $Dir = Get-ChildItem $Location -recurse
#--- pass the variable $File though the $_. to check if it exists
#--- in the directory which holds the $Location variable
    $List = $Dir | where {$_.extension -EQ $File}
#--- Pipe the output of $List into a table
    $List | Format-Table Name
}
# --- error handle non-existent diretory
else
{
    "Folder path does not exist"
}
# --- display results
# --- create function
Function TestPath()
    {
# ---See if the path in $Location is true or false
    $FileExists = Test-Path $Location
    IF ($FileExists -eq $true)
        {
        Return $true
        }
    Else
        {
        Return $false
        }
    }
}

Edit: added many comments.
[Image: pia_banner_3.png]
Would you be able to comment your code? Most/many here (including myself) cannot fully understand Powershell.
(Aug 7, 2017 03:12 PM)ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™ Wrote:  Would you be able to comment your code? Most/many here (including myself) cannot fully understand Powershell.

Absolutely. I apologize. I have one other dude I work with who I bounce code off of, so we generally don't comment too, too much. I'll correct it now.
[Image: pia_banner_3.png]
(Aug 7, 2017 03:55 PM)Travelers Wrote:  Absolutely. I apologize. I have one other dude I work with who I bounce code off of, so we generally don't comment too, too much. I'll correct it now.

Please, and thank you! It may be unapparent sometimes, but there are people who read code. Also, Powershell isn't a strength of mine, so it absolutely helps me write hybrid scripts with batch.
(Aug 7, 2017 05:56 PM)ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™ Wrote:  Please, and thank you! It may be unapparent sometimes, but there are people who read code. Also, Powershell isn't a strength of mine, so it absolutely helps me write hybrid scripts with batch.

Sure thing. I didn't really think of it. Really one of the best ways to learn is to use the "get-alias" and "get-help" cmdlets. Learning the Powershell equivalents of batch commands is the best way to get started.
[Image: pia_banner_3.png]
Hi ✌
Can you post an example to show us how we can use this portion of powershell with a batch file ?
Thank you !
(Aug 8, 2017 02:16 AM)Hackoo Wrote:  Hi ✌
Can you post an example to show us how we can use this portion of powershell with a batch file ?
Thank you !

Absolutely. This is the batch file that I was using until I re-wrote in Powershell and added some features.

Code
@echo off

for %%a IN (*.txt *.doc *.bat) do (
    IF EXIST %%a (echo File found: %%a) ELSE (echo File NOT found: %%a)
)
pause

You can see where I added support for user input and the ability to specify which directory you would like to search, straight from the command line. This script only got me so far, and then I needed to expand it. Hope this helps. 🤝
[Image: pia_banner_3.png]
Hi ✌
Here is an example that i made it just right now to show you how we can use the command Where /? to locate and display files in a directory tree !
Code
@echo off
Title Find Location Files using the command Where by Hackoo 2017
Mode con cols=90 lines=5 & color 9E
REM The search will be recursively on the current directory
Set "Source=%~dp0"
Set "Ext=txt doc bat"
set "LogSearch=%~dp0%~n0_SearchLog.txt"
If exist "%LogSearch%" Del "%LogSearch%"
Cls
echo(
echo       --------------------------------------------------------------------------
echo              Please Wait a while ....... Searching is in progress .........
echo       --------------------------------------------------------------------------

@for %%a IN (%Ext%) do (
    Where /R %Source% "*.%%a" /F >>"%LogSearch%" 2>&1
)
Start "" "%LogSearch%"

I use the same logic in my last batch file called Incremental_Copy.bat for searching files paths !
(Aug 8, 2017 03:04 AM)Hackoo Wrote:  Hi ✌
Here is an example that i made it just right now to show you how we can use the command Where /?
Code
@echo off
Title Find Location Files using the command Where by Hackoo 2017
Mode con cols=90 lines=5 & color 9E
REM The search will be recursively on the current directory
Set "Source=%~dp0"
Set "Ext=txt doc bat"
set "LogSearch=%~dp0%~n0_SearchLog.txt"
If exist "%LogSearch%" Del "%LogSearch%"
Cls
echo(
echo       --------------------------------------------------------------------------
echo              Please Wait a while ....... Searching is in progress .........
echo       --------------------------------------------------------------------------

@for %%a IN (%Ext%) do (
    Where /R %Source% "*.%%a" /F >>"%LogSearch%" 2>&1
)
Start "" "%LogSearch%"

Nice job Hackoo.

The Poweshell version allows me to grab the user input and search for the file type and directory that they specify, though. This batch example is great, thank you for your effort🤝
[Image: pia_banner_3.png]
Here is another batch script that i made it before called :
Local_Search_Engine.bat
Local_Search_Engine.bat (Click to View)


You can also get the input string and browse for folder from user using a vbscript like this improved version :
Code
@echo off
Title Find Location Files using the command Where by Hackoo 2017
Mode con cols=90 lines=5 & color 9E
Call :inputbox "Please enter extensions to search separted with space like [bat cmd vbs txt doc] without brackets " "Search for files with their extensions by Hackoo 2017" "bat cmd vbs txt doc"
If  "%input%" == ""  Color 0C & (
    echo(
    echo       You must enter extensions separted with space to continue with this program
    pause>nul & exit
) else (
    Call :Browse4Folder "Choose source folder to scan for %input%" "c:\scripts"
)
::******************************************************************************************
Set "Source=%Location%"
If "%ErrorLevel%" EQU "1" Exit
Set "Ext=%input%"
set "LogSearch=%~dp0%~n0_SearchLog.txt"
If exist "%LogSearch%" Del "%LogSearch%"
Cls
echo(
echo       --------------------------------------------------------------------------
echo              Please Wait a while ....... Searching is in progress .........
echo       --------------------------------------------------------------------------

@for %%a IN (%Ext%) do (
    Where /R %Source% "*.%%a" /F >>"%LogSearch%" 2>&1
)
Start "" "%LogSearch%"
Exit
::***************************************************************************
:Browse4Folder
set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
(
    echo set shell=WScript.CreateObject("Shell.Application"^)
    echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^)
    echo if typename(f^)="Nothing" Then  
    echo wscript.echo "set Location=Dialog Cancelled"
    echo WScript.Quit(1^)
    echo end if
    echo set fs=f.Items(^):set fi=fs.Item(^)
    echo p=fi.Path:wscript.echo "set Location=" ^& p
)>%vbs%
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof
::***************************************************************************
:InputBox
set "input="
set "heading=%~2"
set "message=%~1"
set "Default=%~3"
echo wscript.echo inputbox(WScript.Arguments(0),WScript.Arguments(1),WScript.Arguments(2)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%" "%Default%"') do (
    set "input=%%a"
)
exit /b
::***************************************************************************
(Aug 8, 2017 03:09 AM)Travelers Wrote:  Nice job Hackoo.

The Poweshell version allows me to grab the user input and search for the file type and directory that they specify, though. This batch example is great, thank you for your effort🤝

You're so open to conversation, example, correction, and suggestion. You're fitting in quite nicely here!
(Aug 8, 2017 05:21 AM)ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™ Wrote:  You're so open to conversation, example, correction, and suggestion. You're fitting in quite nicely here!

Ha. Well, what other way is there to be? I don't wanna be known as one of those people who is a douche bag and can't take criticism.
[Image: pia_banner_3.png]
@Travelers
Did you checked my last edit ?
(Aug 8, 2017 01:21 PM)Hackoo Wrote:  @Travelers
Did you checked my last edit ?

I did. I enjoyed it. That's some good stuff to be able to produce from batch.

I could never write anything like that from just batch. 🤝
[Image: pia_banner_3.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by samir5000Mar 13, 2025 01:32 PM 3 328 Mar 13, 2025 01:32 PM
Last Post: Bastille
[Shell] by PELockDec 9, 2024 09:42 AM 3 661 Dec 9, 2024 09:42 AM
Last Post: PELock
[Source] by QismonNov 27, 2024 04:29 AM 2 308 Nov 27, 2024 04:29 AM
Last Post: mikeyp