Command Prompt: dir file whose File size is greater than 1024KB - windows

I am currently using the following command to find out how many documents with pdf format is there with there complete path but it shows the list of like 11,000 documents,
dir *.pdf /s /b**
I'd like to list only those images that has the file size greater than 1024KB , the file size shouldn't be displayed yet the file should be greater than 1024KB in size.
is that possible using command prompt ?

Since you're using windows, you will most likely have powershell:
ls *.pdf | where-object {$_.length -gt 1048576} | format-table -property Name
ls will list the files with .pdf extensions. where-object will filter the result set to files with length greater than 1MB (1048576 = 1MB). format-table will format the final output to display only the name of the file

Related

cmd dir write file size in windows 10 in Gb instead of byte [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I use command
dir L01\*.gz /o-s/-c > L01.txt
to find all gz files in directory and sort by size. It works well!
But, I need to take size in Gb not in bytes!
01/24/2020 12:36 AM 3018394731 v3000418221_L01_82_1.fq.gz
01/24/2020 12:36 AM 2883376937 v3000418221_L01_82_2.fq.gz
01/24/2020 12:36 AM 2875257587 v3000418221_L01_69_1.fq.gz
01/24/2020 12:36 AM 2785098410 v3000418221_L01_69_2.fq.gz
01/24/2020 12:36 AM 2520038171 v3000418221_L01_99_1.fq.gz
01/24/2020 12:36 AM 2478618550 v3000418221_L01_62_1.fq.gz
01/24/2020 12:36 AM 2470651439 v3000418221_L01_99_2.fq.gz
also I need only filenames and sizes in Gb without date and time
And, it will be great if command do it with files in all subdirectures and give output like:
directory L01 (or smth else):
v3000418221_L01_82_1.fq.gz 2.5 Gb
v3000418221_L01_82_2.fq.gz 2.4 Gb
directory L02 :
v3000418221_L01_12_1.fq.gz 2.1 Gb
v3000418221_L01_32_2.fq.gz 0.4 Gb
v3000418221_L01_42_1.fq.gz 1.5 Gb
v3000418221_L01_8_2.fq.gz 2.4 Gb
It is not my computer so I try to do it in cmd without installing python.
You can do the following in PowerShell to search the current directory, which will output FileInfo objects sorted by Length (Size) and with size converted to GB.
Output to Console Only
Get-ChildItem -Filter '*L01*.gz' | Sort Length -Desc |
Select LastWriteTime,Name,#{n='SizeGB';e={$_.Length/1GB}}
If you want set the directory within the command, you can add -Path DirectoryPath to your Get-ChildItem command. The -Recurse switch allows for a recursive directory search from the search directory. See Get-ChildItem.
If you want the pipe the output to a file as is, you can just add | Out-File L01.txt or | Set-Content L01.txt.
In PowerShell, dir is an alias for Get-ChildItem. So you can use dir -Filter '*L01*.gz' if you feel the need.
Output to File Without Table Headers
PowerShell works with objects. If your objects have property names, they will by default appear as column headers in a table output. If you want the headers removed, you can just pipe your output to Format-Table -HideTableHeaders.
Get-ChildItem -Filter '*L01*.gz' | Sort Length -Desc |
Select LastWriteTime,Name,#{n='SizeGB';e={$_.Length/1GB}} |
Format-Table -HideTableHeaders | Set-Content L01.txt
Output to File From CMD Shell
If you only want to run code from the cmd shell, then you can still execute the PowerShell code there. Just put the PowerShell code in a script file Something.ps1.
Powershell.exe -File Something.ps1
There are some differences in default encoding for Out-File and Set-Content. In Windows PowerShell > redirects output (can target a file or stream) and uses Unicode with BOM. Out-File behaves the same as the redirect operator when no parameters are supplied. In PowerShell Core or just PowerShell (as of v7), both commands should output to files by default in UTF-8 with no BOM. Set-Content outputs using the culture-specific Default encoding. Both commands have an -Encoding parameter for you to control your output encoding.

Powershell script to list all open Explorer windows

This question shows a Powershell script to generate a list of open File Explorer windows and their path.
My goal is to capture the currently open set of explorer windows, and write out a CMD file with commands like: C:\WINDOWS\explorer.exe /e, "C:\open\this\folder"
So I would like to have the full path and folder name in normal path notation. This is what is showing in titlebar of the Explorer Windows: "C:\open\this\Favorite folder"
The proposed code is:
function Get-WindowTitle($handle) {
Get-Process |
Where-Object { $_.MainWindowHandle -eq $handle } |
Select-Object -Expand MainWindowTitle
}
$app = New-Object -COM 'Shell.Application'
$app.Windows() |
Select-Object LocationURL, #{n='Title';e={Get-WindowTitle $_.HWND}}
As shown above, LocationURL provides a full path in an escaped-URL style:
file:///C:/open/this/Favorite%20%folder"
The #{n='Title';e={Get-WindowTitle $_.HWND}} component produces a column "Title" which is truncated to 5 characters:
C:\...
The full output for one explorer window looks like:
LocationURL Title
----------- -----
file:///C:/open/this/Favorite%20%folder C:...
I found I could avoid the truncation by padding the string 'Title' with many spaces. That string's width seems to determine the maximum width of the output.
Still, I observe that only about 60% of the open explorer windows list a path. The rest are just a blank line.
I tried "$app.Windows() | Select-Object LocationName", but the output only contains the Explorer folder name only, not the full path and folder that is displayed in the Explorer title.
Another mystery is why the script runs so slowly. If I have 10 explorer windows open, the script runs for 30 seconds, taking about 3 seconds per path.
For this script:
function Get-WindowTitle($handle) {
Get-Process |
Where-Object { $_.MainWindowHandle -eq $handle } |
Select-Object -Expand MainWindowTitle
}
$app = New-Object -COM 'Shell.Application'
$app.Windows() |
Select-Object LocationName,#{n=' ------------Title---------------- ';e={Get-WindowTitle $_.HWND}}
This is the output (with some redacting with *** for privacy)
PS C:\E***> .\OpenExplorer.ps1
LocationName ------------Title----------------
------------ ----------------------------------------------------------------------------------
2019-07
Ame****
2019 Priv...
2019-10-3... C:\E\Event Presentations\2019-10-31 Priv**********bcast
E C:\E
5G Brief ... C:\E\Tech************ing\5G Brief (2018)
36 Series...
2019 DE* ... C:\E\16*****N\2019 DE*******************
Newsletters C:\E\Newsletters
Reports C:\E\Tech************ing\Reports
2019-10-2... C:\E\16**********s\2019-10-29 *********************
2019-11 C:\Data\Docs\Stand*********24\2019-11
UB****
Financial... C:\E\Financ************
Expenses C:\E\Internal\Expenses
E C:\E
E***
I assume what you're really interested is the local filesystem paths of the open Explorer windows, not necessarily the window titles (which aren't guaranteed to reflect the full paths).
Somewhat obscurely, the window objects returned by the .Windows() method contain the local path representation in their .Document.Folder.Self.Path property.
(New-Object -ComObject 'Shell.Application').Windows() | ForEach-Object {
$localPath = $_.Document.Folder.Self.Path
"C:\WINDOWS\explorer.exe /e, `"$localPath`""
}
The above produces output such as:
C:\WINDOWS\explorer.exe /e, "C:\Users\jdoe"
C:\WINDOWS\explorer.exe /e, "C:\Program Files"
You can output this to a batch file file as needed, e.g. by appending | Set-Content file.cmd to the above command.
Note: The windows are listed in the order in which they were created, so you cannot infer which among them was most recently activated. See this answer for a solution that finds the topmost File Explorer window and determines the path shown in it.
I found I could avoid the truncation
The truncation is just a display artifact - the data is still there.
You can make the data visible in one of two ways:
pipe to Format-Table -AutoSize to make sure that column values aren't truncated, space permitting
pipe to Format-List, which will show each property on its own line (line-wrapping overly long values).

Recursively count filtered files in subfolders and group results

I want to count all files with a particular prefix in a directory and then display the results based on each sub directory.
The directory tree is as follows
Test
January
sms20180101_110.unl
rec20180101_110.unl
data20180101_110.unl
February
sms20180201_110.unl
rec20180201_110.unl
data20180201_110.unl
March
sms20180301_110.unl
rec20180301_110.unl
data20180301_110.unl
So, I need to count for example the total data files in each subdirectory and display results as follows
January 1
February 5
March 10
I wrote the following command in Powershell
Get-ChildItem -Path D:\Test -Filter *data* -Force -Recurse | Measure-Object | %{$_.Count}
So, the problem is it is giving me the total files in the root directory
A similar question was asked here Recursively count files in subfolders but I have not been able to customize the solutions provided here to my need
Based on your scenario, you can use Group-Object like this -
Get-ChildItem -Path D:\Test -Filter *data* -Force -Recurse | Group-Object -Property Directory | Select-Object Name, Count
This will list all the name of the folders and sub-folders along with the count of files having data in it's name.

Powershell script to output Size on disk

I'm very new to powershell and is it possible to obtain the actual size of disk of a file? I was able to use the du, but is there another way of doing this without using that application?
This will give you the actual size of a file in bytes:
(gci <insert file path> | Measure-Object -Property length -Sum).sum
You can then use other logic to convert to KB, MB, GB, whatever you want. You can use the same command for size of directories, with the -Recurse option to get the size of all subdirectories and files in the root.

Why folder size is different from result of PowerShell?

I am running PowerShell code to check a folder size. I just noticed that the result is different from what I see in Windows GUI of folder properties.
part of PowerShell code:
#{label="Size" ; expression={(Get-childitem "c:\windows" -recurse | measure-object length -sum).sum}}
Results are not the same....?
BTW, how to display the size in GB based on the code above?
Hint please!
The reason you get different results is hidden files and folders. To account for these use -Force switch on Get-childitem:
#{label="Size" ; expression={(Get-childitem -force "c:\windows" -recurse | measure-object length -sum).sum}}
To get your total into GBs just divide the result by 1Gb, ie:
#{label="Size" ; expression={(Get-childitem -force "c:\windows" -recurse | measure-object length -sum).sum/1Gb}}
Or to round it up to one decimal point:
#{label="Size" ; expression={[math]::round(((Get-childitem -force "c:\windows" -recurse | measure-object length -sum).sum/1Gb),1)}}

Resources