Changing white-space on the beginning of a file name [closed]

感情迁移 提交于 2020-01-14 07:11:52

问题


We have randomly named pdf documents being uploaded to a Google Drive folder. When uploading these documents sometimes there is a space in front of the file name, ex. " 12345.pdf". I want to delete this whitespace in front of the file name with a batch file.


回答1:


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.



来源:https://stackoverflow.com/questions/46587646/changing-white-space-on-the-beginning-of-a-file-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!