更改文件名开头的空白 [英] Changing white-space on the beginning of a file name
问题描述
我们随机命名了要上传到Google云端硬盘文件夹的pdf文档.上载这些文档时,有时在文件名 ex前面有一个空格. "12345.pdf" .我想使用批处理文件删除文件名前面的空白.
如果前导空格的数目是恒定的,那么实际上只有一个简单的划线器,除了REN之外什么都不用,将删除前导空格!>
如果您只有一个前导空格,则可以使用
ren " *" "/*"
如果有两个前导空格,则
ren " *" "//*"
以此类推...
https://superuser.com/q/475874/109090 中对此行为进行了描述.不过要小心.有一次我想我看到,如果前导字符为空格,单个/
可以剥离多个前导字符.但是我无法重现此行为.现在,我得到的只是/
精确剥离一个前导字符的预期"行为.
如果前导空格的数量不同,那么您可以使用以下命令安全地删除所有前导空格(假设结果没有名称冲突).
for %A in (" *") do @for /f "tokens=*" %B in ("%A") do @ren "%A" "%B"
如果将代码放入批处理脚本中,请不要忘记将百分比提高一倍.
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.
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.
这篇关于更改文件名开头的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!