Please I need help. I had never worked before with powershell and I am trying to execute a instruction only on a folder that has (at the same time) two files (one is update.exe and the other one is *.new)
The idea is "if the folder contains a file called update.txt and (the same time) a file with extension .new I will execute the following instruction:
Foreach ($file In $files)
{
$folderPath = Split-Path $file.fullname -parent
$oldfile = $folderPath + "\" + [System.IO.Path]::GetFileNameWithoutExtension($file.FullName) + ".old"
#remove if .old extension file exists
if (Test-Path $oldfile)
{
Remove-Item $oldfile
}
#change .exe to .old
$exefile = $folderPath + "\" +[System.IO.Path]::GetFileNameWithoutExtension($file.FullName) + ".exe"
if (Test-Path $exefile)
{
$oldfile = $folderPath + "\" + [System.IO.Path]::GetFileNameWithoutExtension($file.FullName) + ".old"
Rename-Item $exefile -newname $oldfile
}
#change .new to .exe
$newfile = $folderPath + "\" +[System.IO.Path]::GetFileNameWithoutExtension($file.FullName) + ".new"
if (Test-Path $newfile)
{
$oldfile = $folderPath + "\" + [System.IO.Path]::GetFileNameWithoutExtension($file.FullName) + ".exe"
Rename-Item $newfile -newname $oldfile
}
}
}