简体   繁体   English

Windows批处理重命名

[英]Windows batch renaming

Currently I am working on a small utility which processes a specific archive format. 目前,我正在开发一个小型实用程序,可以处理特定的存档格式。 The question is: suppose I have several files which either match *.zipped.* pattern either not. 问题是:假设我有几个文件都与*.zipped.*模式不匹配。 The last asterisk is guaranteed to represent the format (extension). 保证最后一个星号代表格式(扩展名)。 How do I pass all matching files to the utility via Windows .bat-file? 如何通过Windows .bat文件将所有匹配的文件传递给实用程序?

A sample script should be like that: 示例脚本应如下所示:

FOR /R "%CD%" %%I IN (*.zipped.*) DO (
 SET file_name=%%I
 offzip %%I !file_name:~end,end-7!%%~xI
)

where "offzip" represents the utility I use. 其中“ offzip”代表我使用的实用程序。 Of course, this one does not help. 当然,这没有帮助。 By the way, extensions may be of different length. 顺便说一句,扩展名可以具有不同的长度。

Example cases: 示例案例:

  • "sample.zipped.txt" -> "sample.txt" “ sample.zipped.txt”->“ sample.txt”
  • "zipped123.zipped.dat" -> "zipped123.dat" “ zipped123.zipped.dat”->“ zipped123.dat”
  • "abc.zipped.sampleextension" -> "abc.sampleextension" “ abc.zipped.sampleextension”->“ abc.sampleextension”

Thank you for your help! 谢谢您的帮助!

You could use a nested For loop to treat .zipped as another extension removing it via metavariable expansion: 您可以使用嵌套的For循环将.zipped视为另一扩展,通过元变量扩展将其删除:

For /R %%A In (*.zipped.*) Do For %%B In ("%%~dpnA"
) Do offzip "%%A" "%%~dpnB%%~xA"

Put the script below into a file such as zren.ps1, then invoke it using: 将以下脚本放入文件如zren.ps1,然后使用以下命令调用它:

powershell -NoLogo -NoProfile -File zren.ps1

=== zren.ps1 === zren.ps1

Get-ChildItem -File -Recurse -Filter '*.zipped.*' |
    ForEach-Object {
        if ($_.Name -match '.*\.zipped\.[^\.]*$') {
            $newname = $_.Name -replace '\.zipped\.','.'
            & offzip "$_.FullName" "$newname"
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM