2 に答える
There may be more elegant methods, but you could do something like this:
1. Append "*" to your parameter value and use Test-Path against it. In this case, you're treating it like a folder, therefore c:\test would become c:\test\*.
2a. If Test-Path returns true, you have a folder and can proceed with copying its content.
2b. If Test-Path returns false, go to step 3.
3. Use Test-Path against the parameter as it is. If it returns true, then it is a file.
Update
Actually, it's much simpler than I thought. You can use parameter PathType with TestPath and specify if you're looking for a folder or a file.
- PathType Container
will look for a folder.
- PahType Leaf
will look for a file.
I'd determine if it's a text file or a folder and go from there. The function below should get you started and after the script has been run it can be executed like Copy-Thing -filename "sourcefile.txt" -Destination "C:\place"
Function Copy-Thing([string]$fileName,[string]$destination){
$thing = Get-Item $fileName
if ($thing.Extension -eq ".txt"){
Get-Content | %{
Copy-Item -Path $_ -Destination $destination
}
}
elseif ($thing.PSIsContainer){
Get-ChildItem -Path $fileName | %{
Copy-Item -Path $_.FullName -Destination $destination
}
}
else{
Write-Host "Please specifiy a valid filetype (.txt) or folder"
}
}