diff compare directories by filename only - linux

Is it possible to use any sort of diff utility to diff based on filename only, excluding the file extensions? I have multiple dirs that have various versions of a file, ie media.mov, media.mp4, media.jpg, etc. I want to make sure all versions were made for each file (1000s). So /dir1/media_99.mov and /dir2/media_99.mp4 would yield a TRUE condition. Man diff does not have "--ignore-extension" and I'm not sure how to possibly use "--exclude-from=FILE". I can use Linux (preferred) or PowerShell (if I must)

In PowerShell, if you want to know which file names are unique to /dir1, use a Compare-Object call, followed by reducing those file names to their base name (file name without extension), weeding out duplicates, and sorting via Sort-Object
Compare-Object -PassThru -Property Name (Get-ChildItem -File /dir1) (Get-ChildItem -File /dir2) |
Where-Object SideIndicator -eq '<=' |
ForEach-Object BaseName |
Sort-Object -Unique
Note: The assumption is that both Get-ChildItem calls return at least one file-info object, otherwise the Compare-Object call will fail - guard against that with if statements, if necessary.

Related

List files with strings and then ALL files checked with powershell

I want to search for all files, of .exe.config for certain strings. I want to list the files with those strings and then list all of the files that were checked. I can get it to find the files with the strings with:
Get-ChildItem -Path c:\ -I *.exe.config -R| Select-String 'string1','string2'
My issue, I can't figure out how to get it to show all the .exe.config files that it checked (without searching the computer again). I thought I could save the intial search results into a variable. Then run through that with a for loop, but I can't even get the right info into a variable. I tried several variations of below, but $files is always empty. Also, I'm not sold on this approach, so if anyone has a completely different method, that would be fine
$files = (Get-ChildItem -Path c:\ -I *.exe.config -R).Path
$files = (Get-ChildItem -Path c:\ -I *.exe.config -R | select fullname)
Use the common -OutVariable parameter to capture Get-ChildItem's output in a variable of your choice, separately from what the pipeline overall outputs:
Get-ChildItem c:\ -Include *.exe.config -Recurse -OutVariable files |
Select-String 'string1','string2'
Variable $file now contains all System.IO.FileInfo instances output by Get-ChildItem; note how you must pass the variable name without the $ sigil to -OutVariable.
A small caveat is that -OutVariable, as of PowerShell [Core] 7.0:
collects multi-object output in a System.Collections.ArrayList instance rather than in a regular PowerShell array of type [object[]] (fixed-size array of System.Object instances).
even collects single-object output that way (wrapped in a collection) - whereas direct pipeline output just emits the object itself.
See this answer for background information and a workaround.

Removing duplicate shortcuts containing a specific string

We have a lot of users with duplicate files on their Desktop (Windows 10). I want to create a script which runs every X minutes or just once after logging on to the system.
Our users are currently seeing the following shortcuts (.lnk):
Dummy.lnk (correct shortcut, should remain unchanged)
Dummy - copy.lnk (duplicated shortcut, should be removed)
Dummy-OFFICE-QB4V70A.lnk (duplicated shortcut containing the name of the computer, should be removed)
So in short I want to keep the 'original' shortcuts but I want to remove all shortcuts containing '- copy.lnk' and/or '-computername.lnk'.
I also tried using the %computername% variable but I have no idea how to implement this in my code. Every computer has a different and unique name so I can not use pre-defined computernames.
Any tips on how I can achieve this? I have tried using the code below, but this removes all .lnk (shortcut) files and not just the duplicates.
#(Get-ChildItem Dummy.lnk | Select-String -Pattern "OFFICE" | Select-Object -ExpandProperty path -Unique) | ForEach-Object{Remove-Item -Force -LiteralPath $_}
I prefer using Powershell so that I can deploy the script using Group Policy.
I would use the Where-Object cmdlet to filter all shortcuts you want to remove and pipe them to the Remove-Item cmdlet:
Get-ChildItem -Path 'c:\yourPath' -Filter '*.lnk' |
Where-Object { $_.Name -match '- Copy\.lnk$' -or $_.Name -like "*$($env:Computername)*"} |
Remove-Item

How to search a string in multiple files and return file name with line number/text in an Excel or csv in Powershell

I have found an answer that gives the list of files in powershell here
How to search a string in multiple files and return the names of files in Powershell?
but I need to know the line number and line text also where that text is found. After this I need to write the output to an excel or csv.
edit:
Get-ChildItem -recurse | Select-String -pattern "ftp" | Write-Host script is giving me the output as C:\codes\prog1.txt:10:FTP but I want the output as path - C:\codes\prog1.txt line number-10 Line text-FTP "server1" in Excel
Try this (don't know if you only want the filename or the path to the file, just remove the one you dont want):
Get-ChildItem -recurse | Select-String -pattern "string" | Select-Object path,line,linenumber,filename | Export-Csv -Path c:\somepath\result.csv

Powershell - Files not older than

I´m trying to make a Powershell script that reports if there´s a file not older than x hours which contains some string pattern. I made this:
Get-ChildItem C:\Folder -recurse | Select-String -pattern "err" | group path | select name | Where {$_.LastWriteTime -gt (Get-Date).AddHours(-12)}
Problem is that the last part of code which should select only files younger than x hours does not work - shows no files. When I change -gt to -lt it shows every file in the folder which contains pattern including younger than defined hours.
Does anyone has a solution please?
Thank you in advance
Your pipeline is in the wrong order. You are piping a collection of strings to Group-Object, which pipes a different collection to Select-Object, etc. Your call to Where-Object is receiving the output of Select-Object, which is a collection of PSCustomObjects.
What you want is to pipe the file objects themselves to Where-Object, and then pass those file objects down the pipeline:
Get-ChildItem C:\Folder -recurse |
Where {$_.LastWriteTime -gt (Get-Date).AddHours(-12)} |
Select-String -pattern "err" | group path | select name

Powershell Select-String and then obtain data on returned files

I admit I am quite tired today but even that is no excuse for the nightmare i seem to be creating for myself with Powershell at present.
Basically the aim is to search a directory for a string that is contained in some word documents. Then I need to return the file name, created date and the last write time. Easy I thought but at some stage I have gone well off track and am still climbing.
What I have done so far is run the search and export the path variable to a text file giving me the path to the files that contain the string. I have then tried to use a foreach loop to load the contents of the file and run a Get-ChildItem against each entry piped to a Select-Object Name,CreationTime,LastWriteTime. I have finally > this to a text file. However it now seems to be returing the info for every file in teh directory and not just those that contain the string I am searching for. I get the feeling I am vastly over complicating this as i tend to with these things. Any help greatly appreciated.
Get-ChildItem -Recurse -Include *.doc | Select-String "Shiba" | select-object path > C:\TRCALM\shibapath.txt
$files = get-content C:\TRCALM\shibapath.txt
foreach($i in $files){gci $i | select-object Name,CreationTime,LastWriteTime > C:\TRCALM\SHIBADates.txt}
You were almost there:
$files = (Get-ChildItem -Recurse -Include *.doc | Select-String "Shiba"| select-object path )
$k = foreach($i in $files){ (gci $i.Path | select-object Name,CreationTime,LastWriteTime) }
$k > C:\pst\SHIBADates.txt

Resources