
我有 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 資料夾,其中包含兩個用於重新命名檔案的子資料夾。例如。輸出重新命名檔案_png_json\JSON PNG。
重新命名的檔案將根據其副檔名複製到資料夾中。
例如。輸出重新命名檔案_png_json\JSON 1.json、2.json、3.json... 輸出重新命名檔案_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!