Solved

powershell dos equivalent for dir *.gh* /s /b

Posted on 2010-09-22
12
1,263 Views
Last Modified: 2013-11-09
I've been asked to write a script that works in powershell, the script needs to list all the ghost files (gho and ghs) on the system and extract the directory the company name and the file name.

The file names are listed like; s:\images\<company>\<imagename>
Where <company> could be any company name, and imagename could be any image name (ending in gho/ghs)

I then need to get the file sizes for each of the files.

I can list the stuff in a batch file and strip out the information using the dir *.gh* /s /b and go from there but powershell seems wholly over complicated but I've been asked to do this in powershell so so be it.

So far I have tried lots of combinations around the following;

Anyone out there who knows powershell? The last time I wrote a DOS script was when DOS 5 was all the rage.


cls
$Dir = get-childitem s:\images | select-object fullname
$List = $Dir
$List


I've also tried;
$List = $Dir | where {$_.extension -eq ".gh?"}

but I get nothing back...

Open in new window

0
Comment
Question by:jools
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
12 Comments
 
LVL 26

Expert Comment

by:pony10us
ID: 33738119
Here is a start to see if I am headed in the correct direction
cls
$Dir = get-childitem -recurse s:\images | select-object Directory, Name
$List = $Dir
$List

Open in new window

0
 
LVL 26

Expert Comment

by:pony10us
ID: 33738159
Oops, I forgot to include to look for *.gh* files only.  Try this instead.
cls
$Dir = get-childitem -recurse s:\images -include *.gh* | select-object Directory, Name
$List = $Dir
$List

Open in new window

0
 
LVL 19

Author Comment

by:jools
ID: 33744595
Thanks, It works if I just run the get-childitem from powershell, If I keep the $List=$Dir and then $List I get no output.

   get-childitem -recurse s:\images -include *.gh* | select-object directory,name

I've also tried fullname as the object, I've also included length which was quite handy.

Unfortunately the directory and names get truncated after about 26chars, the last three chars are then "..." which is not good, frankly I think they should drop the "power" from the powershell. I'm sure it's good for some things but this is supposed to be a simple directory list.

Is there any way of displaying the full directory and file names so that they are not truncated?

Thanks

Jools
0
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
LVL 12

Accepted Solution

by:
GusGallows earned 250 total points
ID: 33746856
Instead of doing a Select object, try the following:

$DIR = get-childitem -recurse s:\images -include *.gh*
foreach ($item in $Dir)
{
     $Directory = $item.Directory
     $Name = $item.Name
     $PathFile = "$Directory\$Name"
     $PathFile
}
 
0
 
LVL 19

Author Comment

by:jools
ID: 33746951
Thanks, I'll try it again in the morning....
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33747809
@Gus

Here is what I get when I run your code:

[PS] C:\>$DIR = get-childitem -recurse c:\batch -include *.ba*
[PS] C:\>foreach ($item in $Dir)
>> {
>>      $Directory = $item.Directory
>>      $Name = $item.Name
>>      $PathFile = "$Directory\$Name"
>>      $PathFile
>> }
>>
C:\batch\security\whom.bat
C:\batch\oldfile.bat
[PS] C:\>
0
 
LVL 26

Assisted Solution

by:pony10us
pony10us earned 250 total points
ID: 33750164
There are two options in the code. The first one wraps the text that is to long to fit instead of truncating. The second sizes the column to fit the text.  I ran both and found that I like the first better. I seemed to lose the other columns off to the right becuase the path was so large when I used the AutoSize.

Give them each a try and see what you think.  I included the Length like you wanted as well.
option 1

cls
$Dir = get-childitem -recurse c:\ -include *.ba* | select-object Directory, Name, Length | format-table -Wrap
$List = $Dir
$List

Option 2

cls
$Dir = get-childitem -recurse c:\ -include *.ba* | select-object Directory, Name, Length | format-table -AutoSize
$List = $Dir
$List

Open in new window

0
 
LVL 19

Author Comment

by:jools
ID: 33752554
Thanks for the responses,

@gus, your example did not truncate the output and I was able to modify it to include the length (size). I used the code below.

@pony10us thanks for the examples, theve been a great help

I've got a better idea about powershell and whilst I think it makes simple tasks more difficult (at least for my needs) I am getting a better idea how it works.

That said, if I continue to have issues finding information on powershell I may just end up installing unxutils and doing a simple `find` command.

$Dir = get-child-item -recurse s:\images -include *.gh*
foreach ($item in $Dir)
{
    $Directory = $item.Directory
    $Name = $item.Name
    $Length = $item.Length
    $PathFile = "$Directory\$Name,$Length"
    $PathFile
}

Open in new window

0
 
LVL 19

Author Closing Comment

by:jools
ID: 33752573
see previous post
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33755487
@jools:

Glad we could help.  As demonstrated, there is always more than one way to "skin a cat"  :)

Thanks for the points.
0
 
LVL 12

Expert Comment

by:GusGallows
ID: 33756820
Don't give up on Powershell. Once in to it, I found it to get a lot easier and now pretty much use it for everything. Just remember that everything is an object and every object has various attributes. If you want to see all the attributes of that object, just add |fl * to the end of it (format list).
For instance, $item |FL *

Glad I could help.
0
 
LVL 19

Author Comment

by:jools
ID: 33756849
@Gus
>> just add |fl * to the end of it (format list).
Thats probably one of the best bits of Powershell advice I've seen.

Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question