This script will only delete files if all exists. How to rewrite the script, so it will delete files even one of the file is missing.
$paths = $outpath, $outPath2, $outPath3
if ((test-path $paths) -notcontains $false){
Remove-Item -Path $paths
}
If the problem is the error exception when it doesn't find a file , you can use the remove-item parameter -erroraction silentlycontinue :
$paths| % { Remove-Item "$_" -ErrorAction SilentlyContinue }
Related
I have the following code
$top = Get-ChildItem -File C:\SomeFolder\1\ -Recurse -filter *.evtx
foreach ($file in $top) {
$get = (Get-WinEvent -Path C:\SomeFolder\1\$file -erroraction 'silentlycontinue').count
if ($get -eq '0') {
Remove-Item -Path C:\SomeFolder\1\$file -Recurse -Force
}
}
The problem is that the files that I'm trying to delete are in use by the loop.
The following error appears - "Remove-Item : Cannot remove item The process cannot access the file because it is being used by another process."
I even tried exiting with break but it didn't help.
Any suggestions?
Thanks!
I'm trying to copy files from a source folder to a destination folder, and rename the files in the process.
$Source = "C:\Source"
$File01 = Get-ChildItem $Source | Where-Object {$_.name -like "File*"}
$Destination = "\\Server01\Destination"
Copy-Item "$Source\$File01" "$Destination\File01.test" -Force -
Confirm:$False -ErrorAction silentlyContinue
if(-not $?) {write-warning "Copy Failed"}
else {write-host "Successfully moved $Source\$File01 to
$Destination\File01.test"}
The problem is that since Get-ChildItem doesn't throw an error message if the file is not found, but rather just gives you a blank, I end up with a folder called File01.test in destination if no file named File* exists in $Source.
If it does exist, the copy operation carries out just fine. But I don't want a folder to be created if no matching files exist in $Source, rather I just want an error message logged in a log file, and no file operation to occur.
This shouldn't matter what the file name is, but it won't account for files that already exist in the destination. So if there is already File01.txt and you're trying to copy File01.txt again you'll have problems.
param
(
$Source = "C:\Source",
$Destination = "\\Server01\Destination",
$Filter = "File*"
)
$Files = `
Get-ChildItem -Path $Source `
| Where-Object -Property Name -Like -Value $Filter
for ($i=0;$i -lt $Files.Count;$i++ )
{
$NewName = '{0}{1:D2}{3}' -f $Files[$i].BaseName,$i,$Files[$i].Extension
$NewPath = Join-Path -Path $Destination -ChildPath $NewName
try
{
Write-Host "Moving file from '$($Files[$i].FullName)' to '$NewPath'"
Copy-Item -Path $Files[$i] -Destination
}
catch
{
throw "Error moving file from '$($Files[$i].FullName)' to '$NewPath'"
}
}
You can add an "if" statement to ensure that the code to copy the files only runs when the file exists.
$Source = "C:\Source"
$Destination = "\\Server01\Destination"
$File01 = Get-ChildItem $Source | Where-Object {$_.name -like "File*"}
if ($File01) {
Copy-Item "$Source\$File01" "$Destination\File01.test" -Force -Confirm:$False -ErrorAction silentlyContinue
if(-not $?) {write-warning "Copy Failed"}
else {write-host "Successfully moved $Source\$File01 to
$Destination\File01.test"}
} else {
Write-Output "File did not exist in $source" | Out-File log.log
}
In the "if" block, it will check to see if $File01 has anything in it, and if so, then it'll run the subsequent code. In the "else" block, if the previous code did not run, it'll send the output to the log file "log.log".
so i had writen this script that will clear out the files in thedownload but it doesn't work
$DaysToDelete = 1
download
Get-ChildItem "C:\users\*\Downloads\*"-Recurse -Force -ErrorAction SilentlyContinue |
Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete))} |
remove-item -force -Verbose -recurse -ErrorAction SilentlyContinue
This code is valid in what it is supposed to do. If there are any errors with file access or anything else, you can inspect them via $Error
automatic variable
How to rewrite below code so it can be grouped into only one IF statements instead of multiple IF and only execute remove command if all files (outPath,outPath2, outPath3) are exists.
If ($outPath){
Remove-Item $outPath
}
If ($outPath2){
Remove-Item $outPath2
}
If ($outPath3){
Remove-Item $outPath3
}
If you want to be sure all the paths exist you can use Test-Path with -notcontains to get the results you want.
$paths = $outPath1, $outPath2, $outPath3
if ((test-path $paths) -notcontains $false){
Remove-Item -Path $paths
}
Test-Path works with arrays of paths. It will return an array booleans. If one of the paths didn't exist then a false would be returned. The if is conditional based on that logic.
Since you will only remove them if they exist you don't need to worry about their existence with the Remove-Item cmdlet.
this will remove the files if they exist and supress errors if the files are not found. The downside is that if there should be errors other than (path not found) then those would be supressed as well.
Remove-Item -Path $outPath,$outPath1,$outPath2 -ErrorAction SilentlyContinue
EDIT
if you only want to remove if all 3 files exist then:
if( (Test-Path $outPath) -and (Test-Path $outPath1) -and (Test-Path $outPath2) )
{
try
{
Remove-Item -Path $outPath,$outPath1,$outPath2 -ErrorAction Stop
}
Catch
{
throw $_
}
}
I am running the below script. If the Copy-Item command is successfully completed, it does not show any messages such as how many files are copied. How do I capture this?
Note: I also need to capture the error message which the script is doing correctly.
$LogFile = "P:\users\Logname.log"
$msg = Copy-Item P:\Bkp_20130610\* P:\users -force -recurse -ErrorAction SilentlyContinue
if (-not $?)
{
msg1 = $Error[0].Exception.Message
Write-Host "Encountered error. Error Message is $msg1."
exit
}
$msg > $LogFile
Write-Host "Hello"
You can obtein a list of copied files in this way
$files = copy-item -path $from -destination $to -passthru
pipe it to | ? { -not $_.psiscontainer } if you are copying folder and you don't want them in the count
then use
$files.count
You can use the -Verbose switch with the Copy-Item cmdlet:
$msg=Copy-Item P:\Bkp_20130610\* P:\users -force -recurse -ErrorAction SilentlyContinue -Verbose