A
A
alexxxhost2016-03-10 11:36:28
PowerShell
alexxxhost, 2016-03-10 11:36:28

How to delete files and folders using powershell and write new ones in their place?

While I'm trying to make a script for local files. In the future, it is planned to copy from one source to several servers. There is one moment, it does not work for me. It is required to first clear the contents of the directory from old files and directories. Can you tell me how to implement this in a script?

Import-Module BitsTransfer
$Source="G:\1"
$Destination="G:\2"
$folders = Get-ChildItem -Name -Path $source -Recurse
$job = Start-BitsTransfer -Source $Source\*.* -Destination $Destination -asynchronous -Priority low
while( ($job.JobState.ToString() -eq 'Transferring') -or ($job.JobState.ToString() -eq 'Connecting') )
{
Sleep 3
}
Complete-BitsTransfer -BitsJob $job
foreach ($i in $folders)
{
$exists = Test-Path $Destination\$i
if ($exists -eq $false) {New-Item $Destination\$i -ItemType Directory}
$job = Start-BitsTransfer -Source $Source\$i\*.* -Destination $Destination\$i -asynchronous -Priority low
while( ($job.JobState.ToString() -eq 'Transferring') -or ($job.JobState.ToString() -eq 'Connecting') )
{
Sleep 3
}
Complete-BitsTransfer -BitsJob $job
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Gennady, 2016-03-10
@genana40

Remove-Item won't work?
get-help remove-item -examples
-------------------- EXAMPLE 1 -------------- ------------
PS C:\>remove-item C:\Test\*.*
This command removes all files with names containing a dot (.) from the C:\Test directory. Because the command includes a dot, it does not remove directories and files without extensions.

I
Ivan Bazaichenko, 2016-05-20
@Banzaii

Get-ChildItem "path" -recurse | Remove-Item
Will remove all files of any nesting from "path"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question