If the number of leading spaces is constant, then there is actually a simple one liner using nothing but REN that will remove the leading space(s)!
If you have exactly one leading space, then you can use
ren " *" "/*"
If two leading spaces, then
ren " *" "//*"
and so on…
This behavior is described at https://superuser.com/q/475874/109090. But be careful. At one point I thought I saw that a single /
could strip multiple leading characters if the leading characters were spaces. But I haven’t been able to reproduce this behavior. Now all I get is the “expected” behavior that /
strips exactly one leading character.
If the number of leading spaces varies, then you can use the following to safely remove all leading spaces (assuming there are no name collisions with the result).
for %A in (" *") do @for /f "tokens=*" %B in ("%A") do @ren "%A" "%B"
Don’t forget to double the percents if you put the code in a batch script.
8
solved Changing white-space on the beginning of a file name [closed]