Copy the specified folder and rename the subfolders in batches, and the subfiles are the specified file names
ask
- 输入数值1输入数字2把“输入数值1”这个目录复制并重命名“输入数字2”,且把子目录及文件符合“输入数值1”也重命名为“输入数字2”如“输入数值1”输入2024,“输入数值2”输入2025。把“C:\path\to\destination\2024”复制成“C:\path\to\destination\2025”子目录里的电-2024年01月01日、付款凭证-2024年01月02日...等,重命名为电-2025年01月01日、付款凭证-2025年01月02日...等子文件修改完成10秒后,子文件夹2024-01、2024-02...2024-12等,重命名为2025-01、2025-02...2025-12
answer
To implement your needs, we need to write a PowerShell script that will do the following:
Duplicate the directory named "Input Value 1" (e.g. 2024) and rename it to "Input Value 2" (e.g. 2025).
Recursively iterate through all files and subdirectories in the newly copied directory.
For files that contain "Enter Value 1" in the file name, rename them to a new name that contains "Enter Value 2".
Wait 10 seconds.
For catalogs whose subdirectory names contain the Enter Numeric 1 - Month format, rename it to a new name that contains the Enter Numeric 2 - Month format.
Here's an example of a PowerShell script that meets your needs:
- # 参数输入
- param (
- [string]$sourceYear = "2024",
- [string]$targetYear = "2025"
- )
-
- # 定义源目录路径
- $sourceDirPath = "C:\path\to\destination\$sourceYear"
-
- # 定义目标目录路径
- $targetDirPath = "C:\path\to\destination\$targetYear"
-
- # 检查源目录是否存在
- if (!(Test-Path -Path $sourceDirPath)) {
- Write-Host "源目录不存在: $sourceDirPath" -ForegroundColor Red
- exit
- }
-
- # 复制源目录到目标目录,包括所有子目录和文件
- Copy-Item -Path $sourceDirPath -Destination $targetDirPath -Recurse -Force
-
- # 获取目标目录中的所有文件,并重命名
- $files = Get-ChildItem -Path $targetDirPath -Recurse -File
- foreach ($file in $files) {
- if ($file.Name -match $sourceYear) {
- $newName = $file.Name -replace $sourceYear, $targetYear
- $newFullPath = Join-Path -Path $file.DirectoryName -ChildPath $newName
- Rename-Item -Path $file.FullName -NewName $newFullPath
- }
- }
-
- # 等待10秒钟
- Start-Sleep -Seconds 10
-
- # 获取目标目录中的所有子目录,并重命名
- $directories = Get-ChildItem -Path $targetDirPath -Directory
- foreach ($directory in $directories) {
- if ($directory.Name -match "\d{4}-\d{2}$") { # 匹配格式为 YYYY-MM 的目录
- $newName = $directory.Name -replace $sourceYear, $targetYear
- $newFullPath = Join-Path -Path $directory.Parent.FullName -ChildPath $newName
- Rename-Item -Path $directory.FullName -NewName $newFullPath
- }
- }
-
- Write-Host "脚本执行完毕。" -ForegroundColor Magenta
Directions for use:
Save the above script as one
.ps1
documents, for exampleCopyAndRename.ps1
。
- .\CopyAndRename.ps1
Notes::
Please
$sourceDirPath
and$targetDirPath
to the actual source directory and destination directory paths.
script
-match "\d{4}-\d{2}$"
Used to match the format ofYYYY-MM
If your catalog is formatted differently, modify the regex accordingly.The script was used
Start-Sleep -Seconds 10
to wait 10 seconds before proceeding with the subdirectory renaming operation.Before running the script, make sure you have sufficient permissions to access and modify these directories and files.
If a directory or file is being used by another program, the rename operation may fail. In this case, close the application that is occupying these files and try running the script again.
or
To implement the function you described, we can write a PowerShell script that will receive two input parameters:输入数值1
(e.g. 2024) and输入数值2
(e.g. 2025), and then perform copying and renaming operations of directories and files based on these parameters.
Here's an example PowerShell script:
- # 参数输入
param (
[string]$sourceYear, # 输入要替换的源年份,例如2024
[string]$targetYear # 输入要替换成的新年份,例如2025
)
# 定义源目录路径
$sourceDirPath = "C:\path\to\destination\$sourceYear"
# 定义目标目录路径
$targetDirPath = "C:\path\to\destination\$targetYear"
# 检查源目录是否存在
if (!(Test-Path -Path $sourceDirPath)) {
Write-Host "源目录不存在: $sourceDirPath" -ForegroundColor Red
exit
}
# 复制源目录到目标目录,包括所有子目录和文件
Copy-Item -Path $sourceDirPath -Destination $targetDirPath -Recurse -Force
# 获取目标目录中的所有文件,并重命名
$files = Get-ChildItem -Path $targetDirPath -Recurse -File
foreach ($file in $files) {
if ($file.Name -match $sourceYear) {
$newName = $file.Name -replace $sourceYear, $targetYear
$newFullPath = Join-Path -Path $file.DirectoryName -ChildPath $newName
Rename-Item -Path $file.FullName -NewName $newFullPath
}
}
# 等待10秒钟
Start-Sleep -Seconds 10
# 获取目标目录中的所有子目录,并重命名
$directories = Get-ChildItem -Path $targetDirPath -Directory
foreach ($directory in $directories) {
if ($directory.Name -match "\d{4}-\d{2}$") { # 匹配格式为 YYYY-MM 的目录
$newName = $directory.Name -replace $sourceYear, $targetYear
$newFullPath = Join-Path -Path $directory.Parent.FullName -ChildPath $newName
Rename-Item -Path $directory.FullName -NewName $newFullPath
}
}
Write-Host "脚本执行完毕。" -ForegroundColor Magenta
Directions for use:
Save the above script as one
.ps1
documents, for exampleCopyAndRename.ps1
。Open PowerShell and navigate to the directory where the script is located.
execute the script, and provide
输入数值1
and输入数值2
As parameters, for example:
- .\CopyAndRename.ps1 -sourceYear 2024 -targetYear 2025
Script description:
The script first receives two parameters:
sourceYear
andtargetYear
。It then defines the path to the source and destination directories.
Next, the script checks if the source directory exists. If it doesn't exist, output a message and exit.
If the source directory exists, the script is used
Copy-Item
command to copy the source directory and all its contents to the destination directory.The script then takes all the subdirectories and files in the target directory and iterates through them.
For each subdirectory and file, the script is used
-replace
operators will be in the namesourceYear
replaced withtargetYear
, and then construct a new full path and use itRename-Item
command to rename.Finally, the script outputs a message indicating that the operation was successful.
Make sure to back up your data before running the script in case something unexpected happens that cause data loss. Additionally, if your directory structure or file naming differs from the example, you may need to modify the path and replacement logic in your script accordingly.
Scan the QR code to push to your mobile phone access.
Copyright Notice: This article is written byTranBon BlogPublished, if you need to reprint, please indicate the source.
Link to this article:https://m.33gd.com/?id=338