I’m doing some batch programming and I use exlamation marks !! for a counter variable
So, whenever I use !count! it adds a nasty space directly after that variable. Since I want to use it for file renaming it’s very annoying
Here is the full code where it is inserting the space right before the .jpg
FOR %%F IN (".") DO (
MOVE "%%F" "%MyDir%-file-0!count!.jpg"
SET /a count=!count!+1
)
Any ideas how to avoid the spaces?
BAT is very quirky for sure.
Perhaps it is the space between the “+1” and the “)” ?
Does your script include DelayedExpansion? This is usually necessary.
I’d suggest though using a subroutine.
It has some very useful argument editing abilities.
Look at “call /?”.
In particular, saying
call :MySub “File name.txt”
In “MySub”, %1 will be “File name.txt” — including the quotes.
However %~1 will strip the quotes.
This lets you handle filesnames with spaces in them very well.
So “my path has spaces\%~1” results in “my path has spaces\File name.txt” – with a single set of quotes around the whole string.
You can also split of the filename and extension.