Current location:TranBon Blog > Homepage > technology > Copy the specified folder and rename the subfolders and subfiles to the specified file name

Copy the specified folder and rename the subfolders in batches, and the subfiles are the specified file names

adminis8 months ago (12-03)technology99670

ask

  • 输入数值1输入数字2把“输入数值1”这个目录复制并重命名“输入数字2”,且把子目录及文件符合“输入数值1”也重命名为“输入数字2”如“输入数值1”输入2024,“输入数值2”输入2025。把“C:\path\to\destination\2024”复制成“C:\path\to\destination\2025”子目录里的电-20240101日、付款凭证-20240102日...等,重命名为电-20250101日、付款凭证-20250102日...等子文件修改完成10秒后,子文件夹2024-012024-02...2024-12等,重命名为2025-012025-02...2025-12


answer

To implement your needs, we need to write a PowerShell script that will do the following:

  1. Duplicate the directory named "Input Value 1" (e.g. 2024) and rename it to "Input Value 2" (e.g. 2025).

  2. Recursively iterate through all files and subdirectories in the newly copied directory.

  3. For files that contain "Enter Value 1" in the file name, rename them to a new name that contains "Enter Value 2".

  4. Wait 10 seconds.

  5. 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:

  1. Save the above script as one.ps1documents, for exampleCopyAndRename.ps1

  • .\CopyAndRename.ps1



Notes:

  1. Please$sourceDirPathand$targetDirPathto the actual source directory and destination directory paths.

  • script-match "\d{4}-\d{2}$"Used to match the format ofYYYY-MMIf your catalog is formatted differently, modify the regex accordingly.

  • The script was usedStart-Sleep -Seconds 10to 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:

  1. Save the above script as one.ps1documents, for exampleCopyAndRename.ps1

  2. Open PowerShell and navigate to the directory where the script is located.

  3. execute the script, and provide输入数值1and输入数值2As parameters, for example:

  • .\CopyAndRename.ps1 -sourceYear 2024 -targetYear 2025

Script description:

  • The script first receives two parameters:sourceYearandtargetYear

  • 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 usedCopy-Itemcommand 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-replaceoperators will be in the namesourceYearreplaced withtargetYear, and then construct a new full path and use itRename-Itemcommand 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.


Article 5 Random Copyright
Article Disclaimer
  • 尊敬的读者:
  • 本文所提供的信息、观点及数据均来源于公开资料、相关研究报告及个人理解,仅供读者参考。本文不代表任何官方立场或专业机构的意见,亦不构成任何投资建议或决策依据。
  • 在撰写本文时,我们已尽力确保信息的准确性和完整性,但鉴于信息来源的多样性及可能存在的误差,我们无法保证本文所述内容在任何时刻都绝对准确无误。因此,读者在阅读本文时,应结合自身实际情况及专业知识,进行独立分析和判断。
  • 对于因本文信息不准确、不完整或读者自身理解偏差而导致的任何损失或损害,我们概不负责。同时,我们也不承担因本文所述内容引发的任何法律责任或纠纷。
  • 此外,本文可能包含对特定公司、行业或市场的分析和预测,这些分析和预测均基于当前市场环境及可获得的信息,并可能受到多种因素的影响而发生变化。因此,读者在参考本文时,应充分考虑这些潜在的风险和不确定性。
  • 我们鼓励读者在阅读本文后,进一步查阅相关资料和咨询专业人士,以获取更全面、准确的信息和建议。同时,我们也欢迎读者就本文内容提出宝贵的意见和建议,以便我们不断改进和提升文章质量。
  • 最后,感谢读者对本文的关注和阅读。我们希望通过提供有价值的信息和观点,为读者在相关领域的学习和决策提供一定的帮助和参考。但请务必记住,本文所述内容仅供参考,不构成任何具体建议或承诺。
  • 特此声明。
阅读剩余的72%

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

Related articles on "Copying a Specified Folder and Batch Renaming Subfolders and Subfiles with Specified File Names"

Hahaha! The great god Note3 can finally flash the Coolpad system or the Coolpad system "brick" directly to 360 OS 2.0!

Hahaha! The great god Note3 can finally flash the Coolpad system or the Coolpad system "brick" directly to 360 OS 2.0!

Hahaha! The great god Note3 can finally flash the coolpad system directly to 360 OS 2.0 hahaha! The great god Note3 can finally directly flash the Coolpad system "brick" to 360 OS 2.0 The latest version of the flashing software is enough, and the Coolpad system or the Coolpad system "brick" can be directly flashed to 360 OS 2.0! The following key points: Flashing software if it is as shown in the figure below,...

SSR deploys scripts with one click

A comma script written ssr.sh script description: ShadowsocksR One-click installation/management script, support single-port/multi-port switching and management system support: CentOS6+ / Debian6+ / Ubuntu14+ Usage: https://doub.io/ss-jc42/ Project address: https://...

centos7 prompts an authentication token manipulation error when the password is broken

centos7 prompts an authentication token manipulation error when the password is broken

You need to enter chattr -i /etc/passwd chattr -i /etc/shadow before passwd as follows:...

Leave a comment

visitor

私密评论

◎ Welcome to participate in the discussion, please express your views and opinions here.
简体中文繁體中文English한국어日本語DeutschрусскийبالعربيةTÜRKÇEportuguêsคนไทยFrançais
今日推荐:【气象小贴士:脾气古怪的“秋台”】—来自短信10620121
【气象小贴士:脾气古怪的“秋台”】—来自短信10620121

【气象小贴士:脾气古怪的“秋台”】一般在9月下旬以后出现的台风称为“秋台”,其移动路径和强度多变,天气也较复杂。主要是因...

立即查看>>

x