0

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:

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

Thank you for your help!

4
  • 1
    You could do offzip "%%~I" "!file_name:.zipped.=."...
    – aschipfl
    Commented Apr 22, 2018 at 20:53
  • 1
    @aschipfl The closing exclamation mark is missing. offzip "%%~I" "!file_name:.zipped.=.!"
    – user6811411
    Commented Apr 22, 2018 at 22:03
  • 2
    You don't need %CD%, /R recurses the current working directory by default.
    – Compo
    Commented Apr 22, 2018 at 22:43
  • Doing that way: FOR /R "%CD%" %%I IN (*.zipped.*) DO (SET file_name=%%I offzip "%%~I" "!file_name:.zipped.=.!")... Now it saves the content to the file named "!file_name", with no extention... What am I doing wrong?( P.S. Cmd/bat do not seem to be that fast and that easy... Funny. Commented Apr 23, 2018 at 19:32

2 Answers 2

3

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"
1
  • Nice one, eliminates the DelayedExpansion.
    – user6811411
    Commented Apr 22, 2018 at 22:56
0

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"
        }
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.