Reputation: 237
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?
Upvotes: 1
Views: 361
Reputation: 2096
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.
@echo off
setlocal ENABLEDELAYEDEXPANSION
set count=0
set MyDir=MyDir
FOR %%F IN ("*.*") DO call :MoveThatFile %%F
goto :EOF
:MoveThatFile
echo MOVE "%~1" "%MyDir%-file-0!count!.jpg"
SET /a count=!count!+1
goto :EOF
Upvotes: 2
Reputation: 20464
If that is really the full code then all is bad, you're not concatenating the commands, and you are trying to use the variable !COUNT! before setting the variable value.
Apart, is a bad practice not enclose the variables.
@Echo OFF
Setlocal enabledelayedexpansion
FOR %%F IN (*) DO (
SET /A "Count+=1"
MOVE "%%F" "%MyDir%-file-0!COUNT!.jpg"
)
Pause&Exit
Upvotes: 0