我在一个文件夹中有许多具有相同扩展名文件的文件。我想一个接一个地重命名文件,然后执行另一个过程,即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
发布于 2019-06-24 18:47:40
使用Get-ChildItem
cmdlet,您可以通过直接将结果通过管道传递到Foreach-Object
来轻松地迭代结果。在该循环中,找到的每个文件都是一个由自动变量$_
表示的FileInfo对象。使用-Filter
参数,下面的代码只获取扩展名为*.txt的文件,并且通过添加-File
开关,您只接收FileInfo对象,而不接收目录对象。
如果我没理解错的话,您希望首先将每个*.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
# ........ #
}
希望这能有所帮助
发布于 2019-06-24 17:04:08
# 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.
}
}
发布于 2019-06-26 18:24:17
代码的一个问题是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
https://stackoverflow.com/questions/56732073
复制相似问题