Answer the question
In order to leave comments, you need to log in
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
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.
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 questionAsk a Question
731 491 924 answers to any question