С помощью командлета Get-ChildItem
вы можете легко выполнить итерацию результатов, напрямую отправив их на Foreach-Object
.Внутри этого цикла каждый найденный файл представляет собой объект FileInfo , представленный автоматической переменной $_
.Используя параметр -Filter
, приведенный ниже код получает только файлы с расширением * .txt, и, добавив переключатель -File
, вы получаете только объекты FileInfo, а не объекты Directory.
Если я правильно понял вопрос, высначала нужно переименовать каждый файл * .txt в * .ini, а затем сделать еще кое-что с переименованным файлом.Это должно сделать это:
$store = "C:\Users\HH"
Get-ChildItem -Path $store -Filter '*.txt' -File | ForEach-Object {
# the automatic variable '$_' here represents a single FileInfo object in the list.
# you don't need to test if the file exists, if it doesn't, Get-ChildItem would not return it.
# create the new name for the file. Simply change the extension to '.ini'
$newName = '{0}.ini' -f $_.BaseName
# rename the file and get a reference to it using the -PassThru parameter
$renamedFile = $_ | Rename-Item -NewName $newName -PassThru
# for testing/proof:
# remember that the '$_' variable now has old file name info.
Write-Host ("File '{0}' is now renamed to '{1}'" -f $_.FullName, $renamedFile.FullName)
# now do the rest of your processing, using the $renamedFile FileInfo object.
# you can see what properties and methods a FileInfo object has here:
# https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo?view=netframework-4.8#properties
# to get the full path and filename for instance, use $renamedFile.FullName
# ........ #
}
Надеюсь, что поможет