Showing posts with label system.io.directory. Show all posts
Showing posts with label system.io.directory. Show all posts

Monday, 1 October 2012

Show-FolderCreation.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This example shows how to create a new directory and  
  4.     subdirectory, and then delete only the subdirectory. 
  5. .DESCRIPTION 
  6.     This sample is a re-write of an MSDN Sample,  
  7.     but in PowerShell. The sample firsts creates then removes 
  8.     a folder then looks to see what is left. The target 
  9.     folder is removed, but intermediate folders remain. 
  10. .NOTES 
  11.     File Name  : Show-FolderCreation.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17.     MSDN sample posted to: 
  18.          http://msdn.microsoft.com/en-us/library/62t64db3.aspx 
  19. .EXAMPLE 
  20.     Psh>  Show-FolderCreation.ps1 
  21.     Created: C:\NewDirectory\NewSubDirectory 
  22.     Deleted: C:\NewDirectory\NewSubDirectory 
  23.     Top-level directory exists:  True 
  24.     Sub-directory exists      :  False  
  25. #> 
  26.  
  27. [CmdletBinding()] 
  28. Param ( 
  29. $Path = "C:\NewDirectory\NewSubDirectory" 
  30.  
  31. Try 
  32.   { 
  33. # Create then remove directory 
  34.      $result = [System.IO.Directory]::CreateDirectory($Path
  35.      "Created: $path"    
  36.      [System.IO.Directory]::Delete($Path
  37.      "Deleted: $path" 
  38.  
  39. # Check existance for top and sub dirs then display results 
  40.      $directoryExists    = [System.Io.Directory]::Exists("C:\NewDirectory"
  41.      $subDirectoryExists = [System.Io.Directory]::Exists($Path
  42.      "Top-level directory exists:  $directoryExists" 
  43.      "Sub-directory exists      :  $subDirectoryExists" 
  44.    } 
  45. Catch  
  46.    { 
  47.        "The process failed: {0}" -f $($error[0].Message) 
  48.    }    

Wednesday, 29 October 2008

Get-Files.ps1

<#
.SYNOPSIS
Gets and displays files from a folder
.DESCRIPTION
This script uses the getfiles method to get and display files in a folder
.NOTES
File Name : Get-Files.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
http://www.pshscripts.blogspot.com
.EXAMPLE
PSH [C:\foo]: .\get-files.PS1
Files in C:\Foo\test
C:\foo\test\div.xml
C:\foo\test\foo.txt
C:\foo\test\foo1.txt
C:\foo\test\foo1.xls
C:\foo\test\foo1.xlsx
C:\foo\test\foo2.txt
C:\foo\test\shows (Autosaved).xlsx
C:\foo\test\shows.xlsx
C:\foo\test\tfl1.xlsx
C:\foo\test\xlsx1.xlsx
C:\foo\test\xlsx2.xlsx

Files in C:\Foo\test\*.txt
C:\foo\test\foo.txt
C:\foo\test\foo1.txt
C:\foo\test\foo2.txt

Files in C:\Foo\test /s
C:\foo\test\div.xml
C:\foo\test\foo.txt
C:\foo\test\foo1.txt
C:\foo\test\foo1.xls
C:\foo\test\foo1.xlsx
C:\foo\test\foo2.txt
C:\foo\test\shows (Autosaved).xlsx
C:\foo\test\shows.xlsx
C:\foo\test\tfl1.xlsx
C:\foo\test\xlsx1.xlsx
C:\foo\test\xlsx2.xlsx
#>

##
# Start of Script
##

# Get all files in C:\Foo\test
'Files in C:\Foo\test'
[System.IO.Directory]::GetFiles('C:\foo\test')
''

# Get all files in c:\foo\test\*.txt
'Files in C:\Foo\test\*.txt'
[System.IO.Directory]::GetFiles('C:\foo\test', '*.txt')
''

# Get all files in c:\foo\test\*.* /s
'Files in C:\Foo\test /s'
$s = [System.IO.SearchOption]::AllDirectories
[System.IO.Directory]::GetFiles('C:\foo\test\', '*.*',$s)
# End of Script