Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 27 days ago.
Improve this question
Essentially, the title
I think that generally the dir command on Windows is too verbose and slow.
(And i'm starting to think that maybe write an alternative myself would be an option)
That kind of depends on what you need. Do you just want names of files? Do you want directories and files? Does it need to be recursive? If you're simply looking at basic dir, then in the context of PowerShell you can get much faster results from using the DirectoryInfo class's GetFiles() method. I pulled C:\Windows\System32 on my system 1000 times with Get-ChildItem and with both GetDirectories() and GetFiles(), and the .Net methods were much faster (about 7x faster).
1..1000|%{Measure-Command -Expression {Get-ChildItem C:\Windows\System32}} |Measure-Object -Average -Property TotalMilliseconds |% Average
142.9717982
1..1000|%{Measure-Command -Expression {([System.IO.DirectoryInfo]'C:\Windows\System32').GetDirectories();([System.IO.DirectoryInfo]'C:\Windows\System32').GetFiles()}} |Measure-Object -Average -Property TotalMilliseconds |% Average
19.9039127
Again, that's just getting you directory names and file names for a single path, but if that's all that you're after it's a good alternative.
Related
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 7 months ago.
Improve this question
I'm looking to make a batch script. What I have is a ton of different logs in a ton of different places, all with a bit of important info and a lot of useless info.
Some of these logs are directly named, and some are named by date and time.
I'm trying to make a script that can basically scan them for me so I don't have to go through each folder and log and scan manually.
Some examples:
OmegaManager\logs\omega.log - scan for "State:" and "has been updated"
OmegaManager\logs\instance-0.log - scan for "shutdown"
KRBanking\PlayerDatabase*logs are numbers* - copy all information or scan for changes since last time?
KRBanking\ServerLogs*logs are dates and times* - scan for "Deposited" and "Withdrawed"
And then output the whole line.
Is this even moderately possible? Thanks in advance.
Yes, this is possible. Batch files support loops, conditions, and filtering/searching.
A FOR loop allows you to iterate through files or directories, you'll find more information in this SO post.
To find strings in a file, you have several options, i.e. find and findstr, see riptutorial.com:
FIND can scan large files line-by-line to find a certain string with no wildcard support.
FINDSTR has more features and supports regular expressions with wildcards in the search string.
Super simple example for one of your use cases
The batch script must be placed where your files are. Otherwise, you need to add a full path instead of just "omega.log".
FINDSTR /L /C:"State:" /C:"has been updated" omega.log
To start with batch programming, you can read the findstr documentation and some tutorials.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
what is the equivalent of command ls -la in Windows Powershell?
Get-ChildItem -Force will reveal hidden items, but the format of -la isn't directly mirror-able in powershell though as it returns objects rather than a string, use Select x,y,z to get the details you're looking for.
Not in correct order for the -l-switch, but this will get you started:
ls -force|Select-Object attributes, fullname, length, LastAccessTime, LastWriteTime, CreationTime, #{N='Owner';E={$_.GetAccessControl().Owner}}|Format-Table
To see which properties are available for select-object you can use the following:
Get-ChildItem -force|Get-Member -MemberType Properties
And as stated by Clijsters in the comments: If you are using linux, ls might not be an valid Powershell alias, so use Get-ChildItem or gci.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to make a list of items in the "C://Program Files" directory, but I'm new to Batch and I'm unsure of how I would do this. I'm more familiar with high-level languages like Java and scripting languages like Lua, but recently I've wanted to learn low(er)-level languages like Assembly and Batch (if you can actually count Batch as a language...).
How would I do this?
I've already changed the directory to the Program Files folder but I have no idea how to make a list of files (from my time in languages like Lua I know I'll need to use a For loop, but I'm unsure of how to do this).
Thanks!
Take a look at the DIR command DIR /?
Dir /b *.* >list.txt
Typing in Dir C:***\ (the rest of the path) will list all the items in that specific folder.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
How do I use the DIR command to list all directories, including subdirectories, that contain no files? I've researched this at length and can only find how to DELETE the specified directories, but not actually list them. Can anyone help? Is there a better way other than the DIR command?
You could modify the Powerscript answer on the linked question to display instead of delete like this:
Get-ChildItem -Recurse . | where { $_.PSISContainer -and #( $_ | Get-ChildItem ).Count -eq 0 } | Select-Object -Property FullName
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a large program in Ruby that's being distributed over several projects. The only problem is the other several projects all have separate members on them that have their own way of coding, and the program is set up to be alongside a specific path, which was for the first project that I used it w/. I've had so many errors to correct that were simply mistaken paths. What I want to do now is scan an entire project for an individual directory (as the program's overhead directory is constant in every instance of its usage) and then set the path to that directory. To keep things simplistic, let's say it's w/ a Rails project, so Rails.root can be the overhead, and the directory to search for is myawesomedir. Any help is greatly appreciated!
You can use the find stdlib:
require 'find'
Find.find(Rails.root) do |path|
if File.basename(path) == 'myawesomedir'
Dir.chdir path
break
end
end