Copy files and folders in several Powershell versions (2.0, 3.0-4.0) -


I have a PowerShell script in version 2.0 (Windows 2008) but this is for me in PS 3.0 - 4.0 (Windows 2012) Fails Fails because no files are found when there is no files.

And I have a PowerShell script in version 4.0 (Windows 2012). But it fails for me in PS 2.0 (Windows 2008). Fails because the directory switch is not recognized.

The script contains files-folder copy functions.

This code for Powershell 2.0

  function CopyWithFilterOlder ($ sourcePath, $ destPath) {$ Exclude = @ ('ThumbS.DB.') #This function Call again, using the hair folder of the current source folder. Include Get-ChildItem $ sourcePath- $ Excluded | Where-object {$ _. Length-EC $ faucet} | % {CopyWithFilterOlder $ _. Full name (join-path-path $ destPath -ChildPath $ _. Name}} # create the destination directory, if it is not already present (! (Test path $ destPath)) {new item path $ destPath -ItemType directory | Out-null} # Copy hair files from source to destination Get-ChildItem $ sourcePath- $ Exclude Excluded | Where-object {$ _ length -A $ faucet} | Copy-item-destination $ destPath}   

This code {$ Excluded] for Powershell 3.0 - 4.0

  function CopyWithFilter ($ sourcePath, $ destPath) = @ ('Thumb dB.') # Turn this function on again, using the child folders of the current source folder. Include Get-ChildItem $ sourcePath-directory- $ Excluded | % {CopyWithFilter $ _ Full name (join-path-path $ destPath -ChildPath $ _. Name)} # create the destination directory, if it does not already exist If ((test path $ destPath)) {new-item-path $ destPath -ItemType directory | Out-null} # Copy files from source to destination Get-ChildItem -FilePath $ sourcePath | | % {Write-host ({1} "-f $ _. Fullname, $ destPath} on` t`t`tCopy {0}; Copy-item $ _ Full Name - destination $ destPath -Force}}   

I have to create a single code independent of the PS version.

Any suggestions about this?

It appears that you depend on the behavior of PS 2.0, which is worth $ zero value. Length property on System.IO.DirectoryInfo, while when you run on PS 3/4 it returns '1'. If you need backwards compatibility with PS 2.0, then filter your first statement for direct directory Why not check the type of your object-object in this way:

  where- objects {$ _- [System.IO.DirectoryInfo]}   

And then for the last statement like this, the goal-like files:

  where-o Gekt {$ _ - [System IO.FileInfo]}   

This way you can change the version whose behavior version are not rely on such properties.

Full sample

  function CopyWithFilterCommon ($ sourcePath, $ destPath) {$ exclude = @ ('thumbs.db') # Call this function again, current Using the hair folder of the source folder. Include Get-ChildItem $ sourcePath- $ Excluded | Where-object {$ _- [[System.IO.DirectoryInfo]} | % {CopyWithFilterCommon $ _. Full name (join-path-path $ destPath -ChildPath $ _. Name)} # Create the destination directory, if it does not already exist. If ((test path $ destPath)) {new-item-path $ destPath -ItemType directory | Out-null} # Copy hair files from source to destination Get-ChildItem $ sourcePath- $ Exclude Excluded | Where-object {$ _- [[System.IO.FileInfo]} | Per-item -inition $ destPath go-ChildItem-path $ sourcePath- $ Exclude Include | Where-object {$ _- [[System.IO.FileInfo]} | % {Copy-item $ _. Absolute name = Destination $ destPath -Force}}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -