将文件从不同的名称重命名为具有不同扩展名的相同名称

将文件从不同的名称重命名为具有不同扩展名的相同名称

我有 12 个文件夹,每个文件夹包含两个子文件夹:“Images”和“Json”。现在每个“Images”文件夹有 10 个图像文件,名称从 1.png 到 10.png,每个“Json”文件夹有 10 个文件,名称从 1.json 到 10.json。

因此,我总共有 120 张图片和 120 个 json 文件。我想将它们随机化,并将它们重命名为一个图片文件夹和一个 json 文件文件夹,因此我将在一个文件夹中放置 1.png 到 120.png(按随机顺序排列),json 文件也是如此。

每个 json 文件都与图像文件相连,因此 1.png 和 1.json 的重命名应该为相同的名称。

我尝试过这样重命名,但是由于我不擅长编写脚本,所以我不知道如何完全按照上述要求执行:

setlocal enabledelayedexpansion    
set /a count=0
for /f "tokens=*" %%a in ('dir /b /od *.png') do (    
    ren %%a ArbitraryString!count!.png
    set /a count+=1    
)

和这个

$i=1
Get-ChildItem | ForEach {    
        Rename-Item $_ -NewName ("filename" + $i + ".jpg")
        $i++    
}

我将非常感激您的帮助。谢谢。

答案1

您可以尝试以下命令:

'\Main\folder\' | ForEach-Object{$($_ + "\*.png"), $($_ + "\*.json")} | Get-ChildItem -Recurse | foreach { If ( $_.extension -ne $prevExt ) { $i=1 } Rename-Item $_.FullName -NewName ('{0}' -f $i++ +$_.extension); $prevExt = $_.extension; }
Note: 
The files will be renamed in the source folder. It would be interesting to make a backup before running the above command.

命令注释:

'\Main\folder\'                                                - is the path where the folder with all your files is.
The symbol |                                                   - it's called a pipeline, it's a concatenation command to join several functions in one line.
ForEach-Object                                                 - you are defining which file extensions you will want to change the names of.
Get-ChildItem -Recurse                                         - will search for all files in folders and subfolders.
foreach                                                        - will execute the command to rename the files until the last one found.

{ If ( $_.extension -ne $prevExt ) { $i=1 }                    - will compare the current file extension, with the extension that was read previously,
                                                                 the { $i=1 } is resetting the value of the counter which will be the sequential name of the file 
                                                                 when the extension changes.
                                                                 Eg: 1.png, 2.png, 3.png... 1.json, 2.json, 3.json... that is, when the extension changes, the 
                                                                 counter starts a new sequence.

Rename-Item $_.FullName -NewName ('{0}' -f $i++ +$_.extension) - Rename-Item    - is the command that will do the renaming of the files
                                                                 $_.FullName    - is the full name of the file
                                                                 -NewName       - is what name the file will be changed to, inside the parentheses are the parameters 
                                                                                  of the new file name: 
                                                                 '{0}'          - will be a file with numeric name
                                                                 -f $i++        - which will be added 1 to 1 
                                                                 +$_.extension  - keeping the original extension

$prevExt = $_.extension                                        - very important item: 
                                                                 this is where the extension of the file being seen is updated with the extension read, so that the command 
                                                                 Rename-Item can know when the file extension has changed and start the next name with numering starting from 1.

要重命名并将所有文件复制到另一个文件夹:

$fileRenamed = "C:\MyRenamedPngJsonFolder\"
foreach ($e in $("png","json")) {ls 'C:\MyOriginalPngJsonFolder' -r -filt *.$e | % {$i=1} { copy $_.FullName $("$fileRenamed"+$i+"."+$e) -Recurse; $i++}}

文件将在输出文件夹中重命名,如下所示:

1.json 1.png, 2.json 2.png, 3.json 3.png, 4.json 4.png, 5.json 5.png...

注意:您可以在计算机的任何位置创建重命名文件的文件夹。源文件夹中的文件不会被重命名。

我使用上述命令做了一些测试,并发现当输出文件夹中已经有文件时,它们会被新文件覆盖并最终丢失。

我根据您的需要制作了一个更完整的脚本。该脚本检查输入文件夹中是否有 json 和 png 文件,检查输出文件夹是否已存在,如果不存在,则自动创建该文件夹。如果文件夹已存在,它将获取文件夹中最后一个现有序列号并从最后一个数字继续重命名,以使文件不重叠。

笔记:

使用此脚本,您可以将其他文件扩展名放入文件夹中,它只会转换 png 和 json 文件。

该脚本将创建一个 output_renamed_files_png_json 文件夹,其中包含两个用于存储重命名文件的子文件夹。例如 output_renamed_files_png_json\JSON PNG。

重命名的文件将根据其扩展名复制到文件夹中。
例如 output_renamed_files_png_json\JSON 1.json, 2.json, 3.json... output_renamed_files_png_json\PNG 1.png, 2.png, 3.png...

1.json 1.png、2.json 2.png……正如您在这篇文章中所报告的那样,互相引用。

参见下面的脚本:

$p=$j=0
$countyp = (ls 'path\original_files\*' -Recurse -Include *.json, *.png | Measure-Object ).Count;             # Checks that there are json and png files in the input folder before starting the process         
$files=Get-ChildItem -Recurse 'path\original_files\*' -Include *.json, *.png | Where {! $_.PSIsContainer } | # and select files if they exist.
% { { $_.extension }
If ($countyp -ne 0) 
   {                                                                                                         
      If ($_.extension -eq ".png")                                                                           # Counts the amount of JSON and PNG files.
   { 
      $p++
   }
   If ($_.extension -eq ".json") 
   { 
      $j++
   }     
 }
}
If ($countyp -eq 0) 
   {
    Write-Host "No PNG and JSON files found to be renamed!... type exit to finish and check for these files in the input folder." -foregroundcolor Red
    Exit
   }
If ($p -ne $j)                                                                                               # If the number of files are not the same, it shows a message informing you which file 
{                                                                                                            # extension is missing and its amount.

   If ($p -gt $j) 
      { 
        $tj=($p-$j)
        Write-Host "$tj JSON file(s) is missing from input folder!... type exit to finish and check input folder." -foregroundcolor Red   # Missing JSON extension message   
   }else {
        $tp=($j-$p)
        Write-Host "$tp PNG file(s) is missing from input folder!... type exit to finish and check input folder." -foregroundcolor Red    # Missing PNG extension message
      }
}else {                                                                                                     
$Folder = 'path\Renamed_Files_png_json'                                                             # checks if the output folder already exists. If it doesn't exist, it will create it.
   if ( -not (Test-Path -Path "$Folder") ) 
   {
      $null = (new-item -type directory -path 'path\Renamed_Files_png_json', 'path\Renamed_Files_png_json\JSON', 'path\Renamed_Files_png_json\PNG' -Force)      
   }
   $pathRenamed = 'path\Renamed_Files_png_json\'                                                    # Associates the output folder path to the variable.
   $count = ( Get-ChildItem -Recurse -File "$pathRenamed" | Measure-Object ).Count 
   If ( $count -eq 0)                                                                               # If the folder was just created, the json and png file numbering starts from 1.
   {
        $ip=$ij=1
   }
   else {                                                                                           # If the folder already exists, the files in the folders will be counted for the number 
   $ip=$ij=($count/2)+1                                                                             # starting from the last number of the existing file. As you informed that the files
   }                                                                                                # are always matched ( 1.json 1.png, 2.json 2.png), the total value is divided by 2.
   foreach ($e in $("png","json")) {
   ls 'path\original_files\' -r -filt *.$e | % {                                                    # Check the extension of files to copy them to the corresponding folder.
   If ( $_.extension -eq ".json" ) 
   {
     copy $_.FullName $("$pathRenamed"+$e+"\"+$ij+"."+$e) -Recurse -Force; $ij++ 
   }
   else {
     copy $_.FullName $("$pathRenamed"+$e+"\"+$ip+"."+$e) -Recurse -Force; $ip++ 
   }
  }
 } 
     Write-Host "The files have been renamed... type exit to finish!" -foregroundcolor Green
} 
If you still haven't found a solution for your post, test it with the script!

相关内容