I have a couple of files, let’s say numbered 40, with extensions .htm, .html, .mhtml etc (not the same – it’s important). Here’s a brief example:
`
alpha.htm
beta.htm
gamma.html
delta.htm
`
I would like to rename them all using one pattern, in this manner:
`alpha.htm → 0.htm
beta.htm → 1.htm
gamma.html → 2.htm
delta.htm → 3.htm`
Unfortunately, it would be inappropriate to create the plain ren file with instructions like above, since all the .htms appear to be welded to their related content folders, and Windows blocks any tryouts to rename them this way. Although, it suddenly works for loops.
So i would like to use loop, and the function taking two arguments – the first one for name and the second one – for the number to be used as counter:
SET %num%=0
FOR %%I in (*.*ht*) DO Call :_deactivate "%%~nxI %num%"
:_deactivate
Ren "%~1" "%~2%~x1"
Set /A "%~2+=1"
Seems to be that interpreter does not really know how to read this “%~2%~x1”, which is really a combination of the second argument (%~2) and an extension of filename from the first argument (%~x1), and I don’t really know if it is possible to combine these variables there ever, and how is this done.
“SET /A %%~2+=1” complains about the absence of operand, so i think it doesn’t really see two operands here.
I googled around to find the correct form to combine custom variables with Batch internal wildcards for files;
i tried to rewrite %num% as %%z, to use delayed expansion mode with !num! exclamation marks notation. Didn’t work. I’m out of ideas, even how to google it correctly.
barsicle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.