- <#
- .SYNOPSIS
- This example shows how to create a new directory and
- subdirectory, and then delete only the subdirectory.
- .DESCRIPTION
- This sample is a re-write of an MSDN Sample,
- but in PowerShell. The sample firsts creates then removes
- a folder then looks to see what is left. The target
- folder is removed, but intermediate folders remain.
- .NOTES
- File Name : Show-FolderCreation.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/62t64db3.aspx
- .EXAMPLE
- Psh> Show-FolderCreation.ps1
- Created: C:\NewDirectory\NewSubDirectory
- Deleted: C:\NewDirectory\NewSubDirectory
- Top-level directory exists: True
- Sub-directory exists : False
- #>
- [CmdletBinding()]
- Param (
- $Path = "C:\NewDirectory\NewSubDirectory"
- )
- Try
- {
- # Create then remove directory
- $result = [System.IO.Directory]::CreateDirectory($Path)
- "Created: $path"
- [System.IO.Directory]::Delete($Path)
- "Deleted: $path"
- # Check existance for top and sub dirs then display results
- $directoryExists = [System.Io.Directory]::Exists("C:\NewDirectory")
- $subDirectoryExists = [System.Io.Directory]::Exists($Path)
- "Top-level directory exists: $directoryExists"
- "Sub-directory exists : $subDirectoryExists"
- }
- Catch
- {
- "The process failed: {0}" -f $($error[0].Message)
- }
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
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
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
Subscribe to:
Posts (Atom)