1folder (cover.jpg)
2folder (cover.jpg)
3folder (cover.jpg)
.
.
多个文件夹中有许多同名的 cover.jpg jpg 文件。
我正在尝试复制并重命名另一个文件夹中的所有 cover.jpg,名称如下 1.jpg 2.jpg 3.jpg.....
我如何使用批处理来执行此操作?(Windows)
我试过
setlocal enabledelayedexpansion
set "files=F:\rsack\다이나믹 듀오\*.jpg"
set "destDir=F:\rsack\다이나믹 듀오"
for /f "delims=" %%f in ('dir "%files%" /b /s') do (
for %%d in ("%destDir%\%%~nf*") do (
set /a count+=1
)
xcopy "%%f" "%destDir%\%%~nf!count!%%~xf*"
set count=
)
和
for /F "delims=" %f in ('dir "F:\rsack\?ㅼ씠?섎? ???*.jpg" /b /s') do (
for %d in ("F:\rsack\?ㅼ씠?섎? ???\%~nf*") do (set /a count+=1 )
xcopy "%f" "F:\rsack\?ㅼ씠?섎? ???\%~nf!count!%~xf*"
set count=
)
找不到文件。
答案1
将其保存为 RenameThemAll.ps1 并保存在电脑上的任何位置(我猜,没有真正测试过),然后将整个代码粘贴到该文件中,保存,右键单击并使用 PowerShell 运行...
-这将询问您要检查哪个目录,并且它将遍历所有子目录 -它将要求您提供文件名作为名称示例 -如果存在具有不同扩展名的同名文件,它将尝试保留原有的扩展名
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$folderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$folderBrowser.Description = "Select Directory"
$folderBrowser.RootFolder = "Desktop"
$folderBrowser.ShowNewFolderButton = $false
$dialogResult = $folderBrowser.ShowDialog()
if ($dialogResult -ne [System.Windows.Forms.DialogResult]::OK) {
Write-Output "Folder selection canceled."
exit
}
$selectedDirectory = $folderBrowser.SelectedPath
$fileDialog = New-Object System.Windows.Forms.OpenFileDialog
$fileDialog.Title = "Select File to Rename"
$fileDialog.InitialDirectory = $selectedDirectory
$fileDialog.Filter = "All Files (*.*)|*.*"
$fileDialog.Multiselect = $false
$dialogResult = $fileDialog.ShowDialog()
if ($dialogResult -ne [System.Windows.Forms.DialogResult]::OK) {
Write-Output "File selection canceled."
exit
}
$selectedFile = $fileDialog.FileName
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($selectedFile)
$files = Get-ChildItem -Path $selectedDirectory -Recurse -File
$index = 1
foreach ($file in $files) {
if ($file.BaseName -eq $baseName) {
$extension = [System.IO.Path]::GetExtension($file)
$newName = "$baseName" + "_$index" + $extension
$file | Rename-Item -NewName $newName
$index++
}
}