1

In Windows Terminal, I am able to execute several commands at the same i.e.:

echo %time% && timeout 3 && echo %time%

However, when executing the above command, It will result in something like:

9:27:57.41
0 秒待っています。続行するには何かキーを押してください ... (My operating system 
is Japanese)
9:27:57.41

As you can see, echo %time% is not executed at a different time even though there was a 3 second timeout in between. How would I separate these two executions?

The End goal here is to measure the the time it takes to perform dir /s C:\.

I tried different things such as:

Powershell -C "Measure-Command {cmd /c dir /s C:\ | Output-Default}" > test.txt

or from Powershell:

Measure-Command{cmd /c dir /s C:\ | Output-Default} > test.txt

But neither of these commands result in the same result I get from running just dir /s C:\ in Terminal. There are differences in time and file count.

I thought I could try:

echo %time% >> test & dir /s C:\ >> test & echo %time% >> test

as an alternative to the Measure-Command but that is not the case...

Something similar can easily be done in linux i.e date +%N && sleep 1 && date +%N or date +%s && sleep 1 && date +%s so why not the almighty Powershell? (a bit of sarcasm)

Any other alternatives I could try or shedding some light to how this all works would help a ton.

EDIT:

With the help from Drew, I was able to do this with the Get-ChildItem method in Powershell. However, this only took a fraction of the time it would take with dir /s. The key goal here is to measure how long it takes with dir so this is mandatory.

EDIT2:

Seems like writing the standard output to the terminal was taking time and not the command itself. Redirecting the output to a File or > NUL was much faster.

Kind regards.

tinnick
  • 147
  • 1
  • 10
  • Possible duplicate of [How do I measure execution time of a command on the Windows command line?](https://stackoverflow.com/questions/673523/how-do-i-measure-execution-time-of-a-command-on-the-windows-command-line) – aschipfl Jul 11 '19 at 09:15
  • Take a look at [this thread](https://stackoverflow.com/q/673523) and also [my answer](https://stackoverflow.com/a/32342984) to learn how to measure execution time in `cmd`. Anyway, you do need delayed expansion and therefore use `!time!` rather than `%time%` -- see [this](https://stackoverflow.com/q/10558316) and [that](https://stackoverflow.com/q/30282784)... – aschipfl Jul 11 '19 at 09:18

1 Answers1

1

So DIR /S will scan all files in the directory and sub-directory. The PowerShell equivalent of this is -File and -Recurse. So putting these into practice, the entire PowerShell command would be:

Measure-Command -Expression {
    Get-ChildItem -Path "C:\" -Recurse -File
} | Out-File C:\test.txt

This will only give you the time taken, not the amount of files. To get the amount of files, you will need to change it up a little.

Measure-Command -Expression {
    (Get-ChildItem -Path "C:\" -Recurse -File).Count | Out-File C:\FileCount.txt
} | Out-File C:\test.txt

Alternatively, you could run this and get something a little nicer. and each task is split up on a new line.

$Path = "C:\"
$StartTime = Get-Date
$FileCount = (Get-ChildItem -Path $Path -Recurse -File).Count
$EndTime = Get-Date
$TimeTaken = New-TimeSpan –Start $StartTime –End $EndTime
"Found $FileCount files in $Path which took $($TimeTaken.Seconds) Seconds" | Out-File C:\test.txt
Drew
  • 3,398
  • 2
  • 7
  • 25
  • Would you kindly explain to me what the```-Expression``` and ```Get-ChildItem``` does and mean? Also, I would like the standard output of the "dir" command in the file as well to reference the last bit in the output i.e.: ```9 File(s) 993,890 bytes 37 Dir(s) 319,640,371,200 bytes free``` Kind regards – tinnick Jul 11 '19 at 01:10
  • I'll find out what each of these mean. Thank you so much for the post! – tinnick Jul 11 '19 at 01:23