Sunday, September 28, 2014

windows 7 - Renaming multiples files with only a part of the original file name


I want to rename 40 png files in one folder, they have very long names. They are named serially like this, "blah...blah...blah160.png", "blah...blah...blah200.png" i.e. after 40 alphanumeric characters comes the serial number in three digits (160), I want only the last three digits to remain in the file name, so, "blah...blah...blah160.png" should become "160.png", is there a simple one line DOS (cmd.exe) command in win 7?


Answer



The following cmd file should do the job:


@echo off & setlocal
for %%F in (*.png) do call :doIt %%F
goto xit
:doIt
set name=%~n1
set num=%name:~-3%
set ext=%~x1
set lentest=%name:~40,3%
if not [%lentest%]==[] (
copy "%1" %num%%ext%
:: del "%1"
)
goto :EOF
:xit
endlocal

Uncomment the "del" line to actually delete the version with the long name.


It is possible to squeeze this in fewer lines, but this would make it less comprehensible.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...