Переименование папок из списка - PullRequest
0 голосов
/ 02 мая 2019

У меня есть список текстовых файлов из 56 папок, который включает полные пути к набору папок.Мне нужно переименовать папку в конце пути.Пример:

Original: \this folder\needs to\move  
New: \this folder\needs to\move.moved

Я совершенно новый в powershell и пытаюсь учиться.Я подумал, что это может быть хорошим способом начать.Любая помощь будет принята с благодарностью.

1 Ответ

0 голосов
/ 02 мая 2019

Добро пожаловать в SO @Bob Moore.Надеюсь, это поможет вам, @ me в комментарии, если у вас есть какие-либо вопросы

# Get the content of the list
# in this case, a text file with no heading, and one path per line
$listContent = Get-Content $ENV:USERPROFILE\Desktop\list.txt


# Loop over each child folder and rename it

foreach($line in $listContent)
{
    # check if the current path is valid

    $pathTest = Test-Path -Path $line

    if($pathTest -eq $True)
    {
        Write-Output "`nOld path: $($line)"
        $newName = $line + ".moved"

        Write-Output "New name: $newName"

        try 
        {
            # on success, write out message
            Rename-Item -Path $line -NewName $newName -Force

            # split the string from the file and get the data after the last \ for readability
            Write-Output "`nSuccessfully changed directory name $($line.split('\')[-1]) to $newName"
        }
        catch 
        {
            # on error, throw first error in the Error array
            throw $Error[0]
        }
    }
    else {
        Write-Output "$($line) is not a valid path"
    }

}

Write-Output "`nEnd of script!"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...