Ignore first line in FINDSTR search - powershell

I am searching an object-oriented Modelica library for a certain string using the following command in the Windows 7 PowerShell:
findstr /s /m /i "Searchstring.*" *.*
click for findstr documentation
The library consists of several folders containing text files with the actual code in them. To reduce the number of (unwanted) results, I have to ignore the first line of every text file.
Unfortunately, I cannot work out how to do this with the findstr command.

You can use Select-String instead of findstr
To get all matches excluding the ones on the first line try something like this:
Select-String -Path C:\dir\*.* -pattern "Searchstring*" | where {$_.LineNumber -gt 1}
If you have to search subdirectories you can pair it with Get-Childitem:
Get-Childitem C:\dir\*.* -recurse | Select-String -pattern "Searchstring*" | where {$_.LineNumber -gt 1}

If you want to keep using findstr you could simply pipe the output into Select-Object:
findstr /s /m /i "Searchstring.*" *.* | select -Skip 1

Related

How to get the first word of output from a PowerShell command

I am trying to get first word from the output of this powershell command
Get-ChildItem -Path Cert:\Certificate::LocalMachine\My | findstr -i ecimas
Which is returning output like:
ffdrggjjhj ecims.example.com
How can I return the string "ffdrggjjhj" only?
You should just be able to split the output like so:
(Get-ChildItem -Path Cert:\Certificate::LocalMachine\My | findstr -i ecimas).split()[0]
Usually powershell looks more like this. Since there's objects, parsing isn't needed.
get-childitem Cert:\LocalMachine\TrustedPublisher | where subject -match wireless |
select -expand thumbprint
ABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCD

Create text file containing a list of all files of a certain type with their filesize

I want to create a text file with all filenames of a certain filetype plus the filesize, recursively from a specified directory and all subdirectories.
For example: Listing all .jpg files plus their sizes from a huge photo-collection.
I have found several similar questions, but not this specific listing.
One did this with the full path name, but I don't need this and it would become very long.
Another lists all files, but without size.
Another lists all filenames with size, but I can't specify a filetype.
This PowerShell command creates the desired list, but I don't know how to limit it to a certain filetype (e.g. .jpg)
gci -rec|?{!$_.PSIsContainer}|%{"$($_.Fullname) $($_.Length)"} >filelist.txt
This batch file lists all .jpg's, but without showing the filesize.
dir /b /s z:\Filme\*.jpg > list1.txt
for /f "tokens=*" %%A in (list1.txt) do echo %%~nxA >> list.txt
del list1.txt
Could anyone edit one of these? so I get the desired list, or come up with a different solution?
Could anyone edit one of these so I get the desired list?
You are almost there with the batch script.
%~z1 will display the file size (in bytes).
You can also get rid of the temporary file by using a slightly different version of the for command.
Use the following batch file:
#echo off
setlocal
for /f "tokens=*" %%A in ('dir /b /s z:\Filme*.jpg') do (
if /i "%%~xf" equ ".jpg" echo %%~nxf %%~zf
) > list.txt
endlocal
Further Reading
An A-Z Index of the Windows CMD command line | SS64.com
Windows CMD Commands (categorized) - Windows CMD - SS64.com
Command Redirection, Pipes - Windows CMD - SS64.com
Dir - list files and folders - Windows CMD - SS64.com
For - Loop through command output - Windows CMD - SS64.com
If - Conditionally perform command - Windows CMD - SS64.com
Parameters / Arguments - Windows CMD - SS64.com
You know about the %%~nxA modifier, so I'm a bit surprised you didn't notice the %%~zA modifier.
To simplify it even more, use a for /R loop and don't use a temp file:
(for /R %%A in (*.jpg) do echo %%~nxA %%~zA)>list.txt
or if you need the full path\name, use %%~fA (explicite) or even just %%A
Text output:
Get-ChildItem -Path 'X:\PHOTO' -Filter '*.jp*g' -Recurse |
Where-Object {-not $_.PsIsContainer} |
Select-Object Name, Length |
Out-File -FilePath '.\FileList.txt'
CSV output:
Get-ChildItem -Path 'X:\PHOTO' -Filter '*.jp*g' -Recurse |
Where-Object {-not $_.PsIsContainer} |
Select-Object Name, Length |
Export-Csv -Path '.\FileList.csv' -NoTypeInformation
P.S. I've used *.jp*g wildcard that will also match *.jpeg files. Unfortunately, * wildcard matches zero or more symbols, so you can get files like zzz.jpXXXg in your list. There are other ways to filter Get-ChildItem output that don't suffer from this issue, such as filtering with pipeline and regex but they're slower: Where-Object {$_.Extension -match '^\.jp[e]{1}g$'}
Another option would be to not use the -Filter parameter, but the -Include instead where the wildcard pattern works as expected, like this:
PowerShell version 3.0 and up
Get-ChildItem 'z:\Filme' -File -Include '*.jpg' -Recurse |
Select FullName, Length |
Export-Csv '.\FileList.csv' -NoTypeInformation
PowerShell version below 3.0
Get-ChildItem 'z:\Filme' -Include '*.jpg' -Recurse |
Where-Object { !$_.PsIsContainer} |
Select FullName, Length |
Export-Csv '.\FileList.csv' -NoTypeInformation
Note that -Include only works if you also specify -Recurse or if you have the path end in \* like in Get-Childitem 'z:\Filme\*'.
Also, -Filter works faster than -Include (or -Exclude) parameters.
As stated in the docs:
"Filters are more efficient than other parameters, because the provider applies them when the cmdlet gets the objects. Otherwise, PowerShell filters the objects after they are retrieved."
I have never looked into the layout from the Where command, but if it does not alter between languages/locales, or technically if your layout is not too dissimilar to that of my test system, you could do it on your machine like this:
From the Command Prompt:
(For /F "Tokens=1,3*" %A In ('Where/T /R . *.jpg 2^>Nul')Do #Echo("%C","%A")>"list.txt"
From a batch file:
#(For /F "Tokens=1,3*" %%A In ('Where/T /R . *.jpg 2^>Nul')Do #Echo("%%C","%%A")>"list.txt"
Obviously if the layout from your Where command output differs there's still a possibility to adjust the Tokens and/or include delimiters to suit your target system.
In the examples above, I've used . to represent the current directory, you could of course change that to another relative path, e.g. ..\Pictures, or full path, e.g. C:\Users\Patrick\Pictures as necessary.
And a powershell option:
Ls -Filt '*.jpg' -Fo -Rec -EA SilentlyContinue|?{!$_.PSIsContainer -And $_.Extension -Eq '.jpg'}|Select FullName,Length|ConvertTo-CSV -NoT|Select -Skip 1|SC '.\list.txt'
This will also include e.g. system and hidden files, will not include files with extensions other than .jpg and will not include an unrequested header with that listing.
try this
Get-ChildItem "yourdir" -File -Filter '*.jpg' -Recurse |
Select FullName, Length |
Export-Csv '.\FileList.csv' -NoType

What is the equivalent of »dir /s /b *foo*.docx« in powershell

I am looking for the powershell equivalent for cmd.exe's
dir /s /b *foo*.docx
I'd like to create an alias (or scriptlet?) for this.
I have found How do I do 'dir /s /b' in PowerShell? but with this approach I seem unable to pass the argument *foo*.docx.
As stated in my comment above, you're looking for:
gci -recurse -filter *foo*.docx | select -expandproperty fullname
gci or Get-ChildItem will return a list of file system objects that match the filter you specified. Each object will have a whole load of information attached with it, but as you're only interested in the path, you can just take the fullname property by piping to select as shown. The -expandproperty flag will ensure the result is returned as a string array rather than a list of objects.
Get-ChildItem -Recurse -Filter *foo*.docx | Select-Object FullName | Sort-Object Length

equivalent of (dir/b > files.txt) in PowerShell

dir/b > files.txt
I guess it has to be done in PowerShell to preserve unicode signs.
Get-ChildItem | Select-Object -ExpandProperty Name > files.txt
or shorter:
ls | % Name > files.txt
However, you can easily do the same in cmd:
cmd /u /c "dir /b > files.txt"
The /u switch tells cmd to write things redirected into files as Unicode.
Get-ChildItem actually already has a flag for the equivalent of dir /b:
Get-ChildItem -name (or dir -name)
Simply put:
dir -Name > files.txt
In PSH dir (which aliases Get-ChildItem) gives you objects (as noted in another answer), so you need to select what properties you want. Either with Select-Object (alias select) to create custom objects with a subset of the original object's properties (or additional properties can be added).
However in this can doing it at the format stage is probably simplest
dir | ft Name -HideTableHeaders | Out-File files.txt
(ft is format-table.)
If you want a different character encoding in files.txt (out-file will use UTF-16 by default) use the -encoding flag, you can also append:
dir | ft Name -HideTableHeaders | Out-File -append -encoding UTF8 files.txt
Since powershell deals with objects, you need to specify how you want to process each object in the pipe.
This command will get print only the name of each object:
dir | ForEach-Object { $_.name }
Just found this great post, but needed it for sub directories as well:
DIR /B /S >somefile.txt
use:
Get-ChildItem -Recurse | Select-Object -ExpandProperty Fullname | Out-File Somefile.txt
or the short version:
ls | % fullname > somefile.txt
I am using:
(dir -r).FullName > somefile.txt
and with filter for *.log:
(dir -r *.log).FullName > somefile.txt
Note:
dir is equal to `gci` but fits the naming used in cmd
-r recursive (all subfolders too)
.FullName is the path only

Powershell 2.0 out-file formatting nightmare

How can I get Powershell to output a file IDENTICAL to the file produced by the following command?
dir /s /b /a-d *.* > C:\files.txt
should be easy, right?!!
EDIT:
I found ps was truncating the output based on the screen buffer width. Fix that with format-table and it pads with spaces... try format-list and you get property headings...you get the idea.
Does it have to be exact? Why? As the saying goes, if you're parsing strings in Powershell, you're probably doing something wrong...anyway...
1) Just call into cmd.exe.
PS> cmd /c "dir /s /b /a-d *.* > c:\files.txt"
2) I believe you can get the same results from native Powershell. But I can't be responsible for testing every edge case with NTFS junctions, hidden files, etc.
PS> gci -r | ?{ !$_.psiscontainer } | %{ $_.fullname } | out-file c:\files.txt
I personally hate the fact you can't use "select" to retrieve the FullName property without weird side effects on downstream cmdlets. If the pointlessness of the foreach loop bothers you as much as it does me, use Get-PropertyValue from PSCX or Linq-Select from Josh Einstein.
There are many ways to write information to a file. One is Set-Content, which does not have the width problem. Also, converting a FileInfo object to a string results in the FullName.
dir * -r | ?{!$_.PSIsCcontainer} | Set-Content C:\files.txt
The reason you have a problem with Out-File is that all the Out-* cmdlets use the automatic formatting views. Those views are created with the console in mind.