DumbStudent2016
DumbStudent2016

Reputation: 89

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. The last asterisk is guaranteed to represent the format (extension). How do I pass all matching files to the utility via Windows .bat-file?

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. Of course, this one does not help. By the way, extensions may be of different length.

Example cases:

Thank you for your help!

Upvotes: 0

Views: 77

Answers (2)

lit
lit

Reputation: 16226

Put the script below into a file such as zren.ps1, then invoke it using:

powershell -NoLogo -NoProfile -File zren.ps1

=== zren.ps1

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

Upvotes: 0

Compo
Compo

Reputation: 38579

You could use a nested For loop to treat .zipped as another extension removing it via metavariable expansion:

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

Upvotes: 3

Related Questions