Skip to content

added parameter -AsObject #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion PSEverything/Everything.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Text;
using static PSEverything.Status;
using System.IO;

namespace PSEverything
{
Expand All @@ -25,7 +26,7 @@ public static class Everything
public static void SetSearch(string text)
{
int res = Is64Bit ? NativeMethods64.Everything_SetSearchW(text) : NativeMethods32.Everything_SetSearchW(text);
if (res != (int) Ok)
if (res != (int)Ok)
{
Throw(res);
}
Expand Down Expand Up @@ -128,6 +129,15 @@ public static string GetFullPathName(int index, StringBuilder buf)

return buf.ToString();
}
public static bool GetIsFolderResult(int index)
{
return Is64Bit ? NativeMethods64.Everything_IsFolderResult(nIndex: index) : NativeMethods32.Everything_IsFolderResult(nIndex: index);
}

public static bool GetIsFileResult(int index)
{
return Is64Bit ? NativeMethods64.Everything_IsFileResult(nIndex: index) : NativeMethods32.Everything_IsFileResult(nIndex: index);
}

public static void Cleanup()
{
Expand All @@ -151,6 +161,22 @@ public static string[] GetAllResults(int count)
return retVal;
}

public static FileSystemInfo[] GetAllResultsAsFileSystemInfo(int count)
{
FileSystemInfo[] retVal = new FileSystemInfo[count];
StringBuilder buf = new(32767);

for (int i = 0; i < count; ++i)
{
string path = GetFullPathName(i, buf);
retVal[i] = GetIsFileResult(i) ? (FileSystemInfo)new FileInfo(path)
: GetIsFolderResult(i) ? new DirectoryInfo(path)
: null;
buf.Clear();
}
return retVal;
}

static int GetLastError()
{
return Is64Bit ? NativeMethods64.Everything_GetLastError() : NativeMethods32.Everything_GetLastError();
Expand Down
2 changes: 1 addition & 1 deletion PSEverything/PSEverything.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0-preview-01" />
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
34 changes: 16 additions & 18 deletions PSEverything/PSEverything.psd1
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@

@{
RootModule = 'PSEverything.dll'
ModuleVersion = '3.3.0'
GUID = 'f262ec02-4a88-49e5-94da-e25aab9cbf7a'
Author = 'Staffan Gustafsson'
CompanyName = 'PowerCode Consulting AB'
Copyright = '(c) 2016 sgustafsson. All rights reserved.'
Description = 'Powershell access to Everything - Blazingly fast file system searches'
PowerShellVersion = '5.1'
RootModule = 'PSEverything.dll'
ModuleVersion = '3.3.1'
GUID = 'f262ec02-4a88-49e5-94da-e25aab9cbf7a'
Author = 'Staffan Gustafsson'
CompanyName = 'PowerCode Consulting AB'
Copyright = '(c) 2016 sgustafsson. All rights reserved.'
Description = 'Powershell access to Everything - Blazingly fast file system searches'
PowerShellVersion = '5.1'
CompatiblePSEditions = "Desktop", "Core"
FunctionsToExport = ''
CmdletsToExport = 'Search-Everything', 'Select-EverythingString'
VariablesToExport = ''
AliasesToExport = 'se', 'sles'
FileList = 'Everything32.dll', 'Everything64.dll', 'LICENSE', 'PSEverything.dll', 'PSEverything.dll-Help.xml', 'PSEverything.psd1'
PrivateData = @{
FunctionsToExport = ''
CmdletsToExport = 'Search-Everything', 'Select-EverythingString'
VariablesToExport = ''
FileList = 'Everything32.dll', 'Everything64.dll', 'LICENSE', 'PSEverything.dll', 'PSEverything.dll-Help.xml', 'PSEverything.psd1'
PrivateData = @{
PSData = @{
Tags = @('Search', 'Everything', 'voidtools', 'regex', 'grep')
LicenseUri = 'https://raw.githubusercontent.com/powercode/PSEverything/master/LICENSE'
ProjectUri = 'https://github.com/powercode/PSEverything'
Tags = @('Search', 'Everything', 'voidtools', 'regex', 'grep')
LicenseUri = 'https://raw.githubusercontent.com/powercode/PSEverything/master/LICENSE'
ProjectUri = 'https://github.com/powercode/PSEverything'
ReleaseNotes = @'
2.3: Bug fixes. Sorted output.
2.1: Upgrading to SDK matching 1.4.1.809b - Fixing hang when calling from Eleveated powershell
Expand All @@ -38,4 +37,3 @@ Bug fix for -Filter that didn't work in combination with non-global searches.
}
}
}

41 changes: 28 additions & 13 deletions PSEverything/SearchEverythingCommand.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using System.Management.Automation;
using System.Text;
using System.IO;

namespace PSEverything
{
[Cmdlet(VerbsCommon.Search, "Everything", SupportsPaging = true, DefaultParameterSetName = "default")]
[OutputType(new []{typeof(string)})]
[OutputType(new[]{typeof(string[])})]
[Alias(new[]{"se" })]
public sealed class SearchEverythingCommand : PSCmdlet , IDisposable
[OutputType(new Type[] { typeof(string) })]
[OutputType(new Type[] { typeof(string[]) })]
[OutputType(new Type[] { typeof(FileSystemInfo) })]
[OutputType(new Type[] { typeof(FileSystemInfo[]) })]
public sealed class SearchEverythingCommand : PSCmdlet, IDisposable
{
[Parameter(ParameterSetName = "default")]
public string Filter { get; set; }
Expand All @@ -22,19 +24,19 @@ public sealed class SearchEverythingCommand : PSCmdlet , IDisposable
[Parameter(ParameterSetName = "default")]
public string[] Extension { get; set; }

[Alias(new[] {"pi"})]
[Alias(new[] { "pi" })]
[Parameter(ParameterSetName = "default")]
public string[] PathInclude { get; set; }

[Alias(new[] {"pe"})]
[Alias(new[] { "pe" })]
[Parameter(ParameterSetName = "default")]
public string[] PathExclude { get; set; }

[Alias(new[] {"foi"})]
[Alias(new[] { "foi" })]
[Parameter(ParameterSetName = "default")]
public string[] FolderInclude { get; set; }

[Alias(new[] {"foe"})]
[Alias(new[] { "foe" })]
[Parameter(ParameterSetName = "default")]
public string[] FolderExclude { get; set; }

Expand Down Expand Up @@ -68,6 +70,10 @@ public sealed class SearchEverythingCommand : PSCmdlet , IDisposable
[Parameter()]
public SwitchParameter AsArray { get; set; }

[Alias(["AsFS"])]
[Parameter()]
public SwitchParameter AsObject { get; set; }

private string GetSearchString()
{
if (ParameterSetName == "regex") { return RegularExpression; }
Expand Down Expand Up @@ -167,7 +173,7 @@ void AddExtensionFilter(StringBuilder searchBuilder)

foreach (var item in Extension)
{
var ext = item.StartsWith(".") ? item.Substring(1) : item;
var ext = item.StartsWith(".") ? item.Substring(1) : item;
searchBuilder.Append(ext);
searchBuilder.Append(";");
}
Expand Down Expand Up @@ -267,7 +273,8 @@ protected override void ProcessRecord()
WriteDebug("Search-Everything search pattern:" + searchPattern);
Everything.SetSearch(searchPattern);

try {
try
{
Everything.Query(true);
Everything.SortResultsByPath();
int resCount = Everything.GetTotalNumberOfResults();
Expand All @@ -276,8 +283,16 @@ protected override void ProcessRecord()
var total = PagingParameters.NewTotalCount((ulong)resCount, 1.0);
WriteObject(total);
}
var res = Everything.GetAllResults(Math.Min(resCount, (int)first));
WriteObject(res, enumerateCollection: !AsArray);
if (AsObject)
{
FileSystemInfo[] res = Everything.GetAllResultsAsFileSystemInfo(Math.Min(resCount, (int)first));
WriteObject(res, enumerateCollection: !AsArray);
}
else
{
var res = Everything.GetAllResults(Math.Min(resCount, (int)first));
WriteObject(res, enumerateCollection: !AsArray);
}
}
catch (Exception e)
{
Expand All @@ -290,4 +305,4 @@ void IDisposable.Dispose()
Everything.Cleanup();
}
}
}
}
18 changes: 9 additions & 9 deletions PSEverything/SelectEverythingStringCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
namespace PSEverything
{
[Cmdlet(VerbsCommon.Select, "EverythingString", DefaultParameterSetName = "default")]
[OutputType(new[]{"Microsoft.PowerShell.Commands.MatchInfo" })]
[Alias(new[] {"sles"})]
[OutputType(new[] { "Microsoft.PowerShell.Commands.MatchInfo" })]
public class SelectEverythingStringCommand : PSCmdlet
{
static readonly string[] SearchParamNames = new[]{
Expand Down Expand Up @@ -49,7 +48,7 @@ public class SelectEverythingStringCommand : PSCmdlet
[Parameter]
public SwitchParameter NotMatch { get; set; }

[ValidateSet(new[]{"unicode", "utf7", "utf8", "utf32", "ascii", "bigendianunicode", "default", "oem" })]
[ValidateSet(new[] { "unicode", "utf7", "utf8", "utf32", "ascii", "bigendianunicode", "default", "oem" })]
[ValidateNotNullOrEmpty]
public string Encoding { get; set; }

Expand All @@ -71,19 +70,19 @@ public class SelectEverythingStringCommand : PSCmdlet
public string[] Extension { get; set; }

[Parameter(ParameterSetName = "default")]
[Alias(new[] {"pi"})]
[Alias(new[] { "pi" })]
public string[] PathInclude { get; set; }

[Parameter(ParameterSetName = "default")]
[Alias(new[] {"pe"})]
[Alias(new[] { "pe" })]
public string[] PathExclude { get; set; }

[Parameter(ParameterSetName = "default")]
[Alias(new[] {"fi"})]
[Alias(new[] { "fi" })]
public string[] FolderInclude { get; set; }

[Parameter(ParameterSetName = "default")]
[Alias(new[] {"fe"})]
[Alias(new[] { "fe" })]
public string[] FolderExclude { get; set; }

[Parameter(ParameterSetName = "default")]
Expand Down Expand Up @@ -124,7 +123,7 @@ protected override void EndProcessing()
{
if (bound.TryGetValue(sp, out object val))
{
searchParams.Add(sp == nameof(CaseSensitiveSearch) ? "CaseSenitive" : sp, val);
searchParams.Add(sp == nameof(CaseSensitiveSearch) ? "CaseSensitive" : sp, val);
bound.Remove(sp);
}
}
Expand All @@ -135,7 +134,8 @@ protected override void EndProcessing()
bound.Remove(nameof(CaseSensitivePattern));
}
var slsParams = bound;
using (_powershell = PowerShell.Create(RunspaceMode.CurrentRunspace)) {
using (_powershell = PowerShell.Create(RunspaceMode.CurrentRunspace))
{
_powershell.AddCommand("Search-Everything").AddParameters(searchParams);
var paths = _powershell.Invoke<string[]>().First();
if (_powershell.HadErrors)
Expand Down
8 changes: 0 additions & 8 deletions PSEverythingStandard/Class1.cs

This file was deleted.

7 changes: 4 additions & 3 deletions Tests/PSEverythingTests/SelectEverythingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ public class SelectEverythingTests
public void TestSelectEverythingString()
{
var iss = InitialSessionState.CreateDefault2();
iss.ImportPSModule(new []{(typeof(SelectEverythingStringCommand).Assembly.Location)});
iss.ImportPSModule(new[] { (typeof(SelectEverythingStringCommand).Assembly.Location) });
using (var ps = PowerShell.Create(iss))
{
ps.Commands.AddCommand("Select-EverythingString")
.AddParameter("-Extension", "ps1")
.AddParameter("-Extension", "psd1")
.AddParameter("-Global", true)
.AddParameter("-Pattern", "function (\\S+)");
.AddParameter("-Pattern", "RootModule")
.AddParameter("-Exclude", "$env:SystemRoot");


var res = ps.Invoke();
Expand Down