Created
          August 15, 2025 15:11 
        
      - 
      
 - 
        
Save KosarevDmitry/e9b48825c8f8dbffabf781a1e4d3a6d6 to your computer and use it in GitHub Desktop.  
    searching path to files like `node`, `curl`, `dotnet.exe` in folders defined by the environment variable "Path"
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /* | |
| dotnet-script search.cs dotnet.exe | |
| searching path to files like `node`, `curl`, `dotnet.exe` in folders defined by the environment variable "Path" | |
| */ | |
| #nullable enable | |
| //filename to search | |
| string? pattern = null; | |
| if (Args.Count() > 0) | |
| { | |
| pattern = Args[0].Trim(); | |
| } | |
| else | |
| { | |
| Console.WriteLine("arg is not specified"); | |
| return; | |
| } | |
| bool TryGetPath(string scriptName, out string? res){ | |
| var path = (Environment.GetEnvironmentVariables()["Path"] as string)!; | |
| var paths = path.Split(';'); | |
| foreach (var p in paths) | |
| { | |
| if (!Directory.Exists(p)) continue; | |
| var file = Directory.GetFiles(p, scriptName, SearchOption.TopDirectoryOnly).SingleOrDefault(); | |
| if (file is not null) | |
| { | |
| res = file; | |
| return true; | |
| } | |
| } | |
| res = null; | |
| return false; | |
| } | |
| if (TryGetPath(pattern!, out string? result)) | |
| { | |
| Console.WriteLine($"Path to {pattern}:\n{result}"); | |
| } | |
| else | |
| { | |
| Console.WriteLine($"No `{pattern}` found."); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment