Showing posts with label shell.application. Show all posts
Showing posts with label shell.application. Show all posts

Tuesday, 23 December 2008

Get-FileCount.ps1

  1. # Get-FileCount.ps1 
  2. # Uses shell.application to get a count of files in a folder 
  3. # Thomas Lee - tfl@psp.co.uk 
  4.  
  5. # First get shell object 
  6. $Shell = new-object -com Shell.Application 
  7.  
  8. # Now get folder details and item counts for a folder 
  9. $foldername = "D:\Foo" 
  10. $folder = $shell.namespace($foldername
  11. if (!$folder) { 
  12. "Folder: {0} does not exist" -f $foldername 
  13. return 
  14.  
  15. # Now output the count of folders 
  16. if ($folder) {  
  17.  $folderitems = $folder.items()  
  18.  $count = $folderitems.count  
  19.  
  20.  $folderps1items = $folderitems | where {$_.type -eq "PS1 File"
  21.  $countps1items=$folderps1items.count 
  22.   
  23.  "Folder `"{0}`" contains {1} files"     -f $foldername, $count 
  24.  "Folder `"{0}`" contains {1} PS1 files" -f $foldername, $countps1items 

This script produces the following output:

PSH [D:\foo]: .\shell.application\Get-FileCount.PS1'
Folder "D:\Foo" contains 38 files
Folder "D:\Foo" contains 22 PS1 files

Thursday, 17 July 2008

Zip-File.ps1

<#
.SYNOPSIS
    This script zips a particular file into an existing archive (zip file).
.DESCRIPTION
    For this script, c:\foo\test.zip must exist
    The script zips c:\test2.txt into c:\foo\test.zip
.NOTES
    File Name  : Zip-File.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2 CTP3
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/zip-fileps1.html
.EXAMPLE
#>

##
# Start of Script
##

# Get shell object and check to see that file exists
$sh = new-object -com shell.application
$fl = get-childitem c:\foo\test.zip -Ea 0
if (!$fl)
 {
   'File does not exist - stopping'
   return
 }

# Get Zip File to zip into
$targetzip = $sh.namespace("C:\Foo\test.zip")   # where the item is to go

# Copy a file into this zip
$targetzip.copyhere("C:\foo\test.txt")

# Check it now exists
ls c:\foo\test.zip
# End of Script

Tuesday, 15 July 2008

Unzip-File.ps1

<#
.SYNOPSIS
    This script unzips a particular file from an archive (zip file)
    into a folder.
.DESCRIPTION
    For this script, c:\foo\zip1.zip must exist and contain test.txt
    The script unzips test.txt into c:\foo and tests it's creation. 
    NB: if c:\foo\test.txt alreadyexists, you'll be told and 
    asked (by Windows) to confirm your actions
.NOTES
    File Name  : unzip-file.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    requires   : PowerShell V2
.LINK
    http://pshscripts.blogspot.com/2008/07/unzip-fileps1.html
.EXAMPLE
    PS C:\foo> .\unzip-file.ps1
    Before Copy:
    Target (C:\foo\test.txt)) does not exist
    After copy:
    Target (C:\foo\test.txt) now exists
#>

##
# Start of Script
##
$sh = new-object -com shell.application

# Zip File to unzip from:
$zipfolder    = $sh.namespace("C:\foo\zip1.zip")  # where the .zip is
$item         = $zipfolder.parsename("Test.txt")  # the item in the zip
$targetfolder = $sh.namespace("c:\foo")           # where the item is to go

# first does file exist?
cd c:\foo
$f = ls test.txt -erroraction silentlycontinue
"Before Copy:"
if ($f)  {"Target ($($f.versioninfo.filename)) exists and you will be promoted for an action"} 
if (!$f) {"Target (C:\foo\test.txt)) does not exist"} 

# do the copy of zipfile item to target folder
$targetfolder.copyhere($item)

# check that it exists
"After copy:"
cd c:\foo
$f = ls test.txt -erroraction silentlycontinue
if ($f) {"Target ($($f.versioninfo.filename)) now exists"}
# End of Script