如何执行循环以使用PowerShell重命名和读取文件?

时间:2019-06-24 07:52:48

标签: powershell

我的文件夹中有很多文件,扩展名相同。我要一个文件重命名,然后执行另一个过程,即Proc_After_Rename。在此过程中,我将阅读文件的一些信息。在此过程中,我想根据前一个过程逐一读取文件信息,以重命名扩展名。完成该过程后,然后再次选择要重命名的文件并执行该过程。 现在,我可以重命名该文件,但是在执行其他过程之前,它会直接重命名所有文件。 ANf在执行此过程Proc_After_Rename时,我读取了所有文件的信息,因为所有文件都已重命名了扩展名。任何人都可以帮忙

已更新

Function Proc_After_Rename
{
$Path = "C:\Users\SS\PowerShell\"
Write-Host "Do some process with .pro file"
$Job_Info =  Get-ChildItem -Path "$store\*.ini" -File -Force
& $Path\UIni.exe $Job_Info AGM CRM AGM_CUR_CRM AGM_CUR_CRM.CMD #this how I read the .ini file
start-sleep -s 1

$Read_AGM_CUR_CRM = Get-Content .\AGM_CUR_CRM.CMD
$a_AGM_CUR_CRM,$b_AGM_CUR_CRM     = $Read_AGM_CUR_CRM -split "="
$b_AGM_CUR_CRM 
Pick_file

}

Function Pick_file
{
$WKFD= "C:\Users\SS\PowerShell\"
$store = "$WKFD\GM"
$files = @(Get-ChildItem -Path "$store\*.txt") 
Foreach ($file in $files)
{
    # Check file existence
    if (Test-Path -Path $file -PathType Leaf)
    {
        # Get file name from object path file $file
        $file_name = @(Get-ChildItem -Path "$file" -Name)
        # Replace the .cue with .pro
        $new_name = $file_name -replace ".txt", ".ini" 
        # Rename the file
        Rename-Item -Path $file -NewName "$new_name"

    }
Proc_After_Rename

}
}

$A = Pick_file

3 个答案:

答案 0 :(得分:3)

使用Get-ChildItem cmdlet,可以通过将结果直接传递到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

    # ........ #
}

希望有帮助

答案 1 :(得分:1)

    # Rename the file
    Rename-Item -Path $file -NewName "$new_name"

    # path of the renamed file
    $new_path_file = "$store\$new_name"

    # This is the process after rename the file
    # ........ #
   #Put your process here and make sure you reference the new file, as long as its in 
   #the foreach you are good. 
    }
}

答案 2 :(得分:0)

您的代码的一个问题是Proc_After_Rename中的Get-ChildItem。这会向UIni提供文件列表而不是文件列表。我试图通过重新编写代码并将Proc_After_Rename的一部分滑入Pick_File来解决此问题。我没有测试过任何一个,但是希望它能使您更好地了解如何组织代码。

如果我是从头开始编写的,那么我将使用管道。

Function Pick_file
{
$WKFD= "C:\Users\SS\PowerShell\"
$store = "$WKFD\GM"
$files = @(Get-ChildItem -Path "$store\*.txt") 
Foreach ($file in $files)
{
    # Check file existence
    if (Test-Path -Path $file -PathType Leaf)
    {
        # Get file name from object path file $file
        $file_name = @(Get-ChildItem -Path "$file" -Name)
        # Replace the .cue with .pro
        $new_name = $file_name -replace ".txt", ".ini" 
        # Rename the file
        Rename-Item -Path $file -NewName "$new_name"

        $new_file_name = $file.fullname
        & $Path\UIni.exe $new_file_name AGM CRM AGM_CUR_CRM AGM_CUR_CRM.CMD 
                   #this how I read the .ini file
        start-sleep -s 1

        $Read_AGM_CUR_CRM = Get-Content .\AGM_CUR_CRM.CMD
        $a_AGM_CUR_CRM,$b_AGM_CUR_CRM     = $Read_AGM_CUR_CRM -split "="
        $b_AGM_CUR_CRM 
    }


}
}

$A = Pick_file