Scripting Question

kenny504kenny504 Users Awaiting Email Confirmation Posts: 237 ■■□□□□□□□□
I want to create a script that can search a directory for a specific file.

How do I go about doing this.

I would like for the interface to be user-friendly. Where the user can actually type in a date for when the report was created and have the script search the folder to find the name of the report.

Can anyone help me Im not fluent with this kind of scripting.
There is no better than adversity, every defeat, every loss, every heartbreak contains its seed. Its own lesson on how to improve on your performance the next time.

Comments

  • TherhinoTherhino Member Posts: 122
    windows or unix?
  • kenny504kenny504 Users Awaiting Email Confirmation Posts: 237 ■■□□□□□□□□
    sorry its a windows machine windows xp clients specifically
    There is no better than adversity, every defeat, every loss, every heartbreak contains its seed. Its own lesson on how to improve on your performance the next time.
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    With Powershell it's simple...

    get-childitem c:\path\* -include filename.txt

    Tack on -recurse to search subdirectories as well.

    You could easily write a script that asks for input of what to use for -include. Just define it as a variable (-include $variablename), make it a mandatory parameter when launching the script.

    If you need to search remote machines, you can use a get-wmiobject command to do this.

    You could go further and make a WinForm that asks for what they're looking for.
    Good luck to all!
  • aordalaordal Member Posts: 372
    Here's a quick & dirty function. You can modify it so that it doesnt prompt for a computer name and just automatically searches the local machine.

    function SearchFile {
    clear
    $computername = Read-Host "Input the name of the Computer you want to seach: "
    $path = Read-Host "Input the root Directory you want to search: "
    $file = Read-Host "Input the name of the file you want to find: "
    Get-ChildItem -erroraction silentlycontinue -recurse -Path $path $file
    " "
    write-host "End of File List"
    }
  • royalroyal Member Posts: 3,352 ■■■■□□□□□□
    Even with the computername specified, it wouldn't search that computer. There's no PowerShell Remoting in the current version of PowerShell. Would need to use WMI and re-write that function as that Get-ChildItem cmdlet doesn't even use that $computername variable.
    “For success, attitude is equally as important as ability.” - Harry F. Banks
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    royal wrote: »
    Even with the computername specified, it wouldn't search that computer. There's no PowerShell Remoting in the current version of PowerShell. Would need to use WMI and re-write that function as that Get-ChildItem cmdlet doesn't even use that $computername variable.

    Word!

    Powershell 2.0 CTP3 could work with rewrites, but that's obviously beta software and probably not a good idea to use, and that function wouldn't work anyway.
    Good luck to all!
  • royalroyal Member Posts: 3,352 ■■■■□□□□□□
    Here's a script I wrote that can be tweaked a bit to give you the functionality you want. I'm way too busy at work to tweak it. In the .txt file, you can specify the directories you want to look in with each directory on its own line. Or just replace it with Get-Content X:\. It'll search subfolders by default and you can take -recurse out of the function to stop it from doing so.

    you can also modify the LastWriteTime attribute and data to specify the attribute you want it to search on and the date.

    $directories = Get-Content C:\directories.txt

    function Get-Data {
    foreach ($dir in $directories) {
    Get-ChildItem $dir -recurse | Select-Object FullName,CreationTime,LastAccessTime,LastWriteTime | Where-Object { $_.LastWriteTime -ge [datetime] "10/10/08" } |
    }
    }
    Get-Data | Export-CSV C:\data.csv -NoTypeInformation
    “For success, attitude is equally as important as ability.” - Harry F. Banks
  • aordalaordal Member Posts: 372
    Hah yeah i totally forgot to implement that part. Let me see..
Sign In or Register to comment.