Reputation: 441
I have directories with a lot of zips in them.
all with different names of software that have been acquired over time.
Ex:
Software1_ABCDE-FGHIJ.zip
Software2_256SS-BM42.zip
Software3_aswdfbbgtyyn.zip
I want to use batch/powershell or bash to rename them all to
Ex:
Software1.zip
Software2.zip
Software3.zip
Basically anything after _ and before .zip needs to go, But its all random content... Serial keys basically. The names are to long for my php downloader i made on my server, and limit is needed for other things to function so i need to rename 100s of zips.
I already have a for loop to zip all files/folders in a dir in to own zip in bash.
for i in */; do zip -r "${i%/}.zip" "$i"; done
and i already have this bash that takes .exe out of any file name before the .zip
rename.ul '.exe' '' *.zip
Ex:
file1_23434434.exe.zip
became
file1_23434434.zip
How can i mass rename all my zips from software1_serial-here-1234.zip to software1.zip
keeping original zip name of the software and the .zip extension
Upvotes: 1
Views: 681
Reputation: 17055
Since you tagged your question with powershell
also, this would be the PowerShell way to do it:
Get-ChildItem -Filter *.zip | Rename-Item -NewName { $_.Name -replace "_.*", ".zip" }
Short version using batch-like aliases:
dir -filter *.zip | ren -new { $_.name -replace "_.*", ".zip" }
Upvotes: 1
Reputation: 49086
In a Windows command prompt window run in directory containing the ZIP files:
for /F "tokens=1* delims=_ eol=" %I in ('dir *_*.zip /A-D-H /B 2^>nul') do ren "%I_%J" "%I.zip"
In a batch file stored in directory containing the ZIP files:
@for /F "tokens=1* delims=_ eol=" %%I in ('dir "%~dp0*_*.zip" /A-D-H /B 2^>nul') do ren "%~dp0%%I_%%J" "%%I.zip"
If there are multiple underscores after software name instead of just one _
or the software name starts with one or more underscores, it is necessary to use two FOR loops to rename really all *_*.zip
files correct:
@for /F "delims= eol=" %%I in ('dir "%~dp0*_*.zip" /A-D-H /B 2^>nul') do @for /F "delims=_ eol=" %%J in ("%%I") do ren "%~dp0%%I" "%%J.zip"
Loop variable I
of outer FOR holds complete file name of ZIP file with file extension, but without file path, and loop variable J
of inner for loop holds just the file name part before first underscore. J
has removed also all underscores at beginning of a file name. So __SoftwareX__Y.zip
is renamed to SoftwareX.zip
.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains %~dp0
... drive and path of argument 0 which is the batch file path always ending with a backslash.dir /?
for /?
ren /?
Upvotes: 1
Reputation: 2904
Get-ChildItem -Path C:\temp -Filter software* -File |
Rename-Item -NewName { ($_.BaseName -split '_')[0] + $_.Extension } -WhatIf
Remove the -whatif to apply the action. -whatif is for a preview.
This assumes that all files you want to rename begin with the word software and there is only one _ in the filename.
Upvotes: 0