如何处理文件名中的破折号 [英] How to deal with an em dash in a filename

查看:346
本文介绍了如何处理文件名中的破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在装有 Windows 7 的 PC 上,我使用一个简单的批处理脚本来重命名一些 Excel 文件,并在其父文件夹名称之前:

On a PC with Windows 7, I'm using a simple batch script to rename some Excel files, pre-pending their parent folder name:

for /f "delims=" %%i in ('dir /b /AD') do (
    cd "%%i"
    for /f "delims=" %%j in ('dir /b *.xl*') do ren "%%~j" "%%i_%%~j"
    cd..
)

我注意到有些文件产生了错误,系统找不到指定的文件."这些文件的文件名中都有一个破折号 (—).在我使用 Windows 资源管理器将长破折号更改为常规连字符 (-) 后,脚本在这些文件上运行良好.

I noticed that some files generated an error, "System cannot find the specified file." These files all had an em dash (—) in the filename. After I changed the em dash to a regular hyphen (-), using Windows Explorer, the script worked fine on these files.

我如何帮助编写这些文件的重命名脚本?

How can I help script the rename of these files?

推荐答案

您的问题不在于批处理变量:从命令行这工作正常:

Your problem is not with the batch variables: from the command line this works fine:

for %i in (*) do ren "%~i" "test_%~i"

但是,从以下可以看出:

but, as can be seen from:

for /f "delims=" %i in ('dir /b /a-d') do @echo ren "%~i" "test2_%~i"

dir/b 正在将破折号更改为连字符,因此 ren 命令显然找不到要更改的文件.

dir /b is changing the em dash to a hyphen, and so the ren command clearly won't find the file to change.

对于您的示例,您应该找到:

For your examples you should find:

for /d %%i in (*) do (

for %%j in (*.xl*) do ...

应该可以正常工作.

如果您出于其他原因需要 dir/b,我现在看不到解决方案.

If you need the dir /b for other reasons, I don't see a solution right now.

(我曾尝试使用 SET/? 中所述的环境变量替换"和延迟环境变量扩展"将所有连字符替换为问号,这是一个令人费解的尝试CMD/?,允许任意字符匹配,然后再次使用ren模式匹配忽略问题.

(I had a convoluted attempt exchanging all hyphens for question marks, using the "Environment variable substitution" and "delayed environment variable expansion" as described in SET /? and CMD /?, allowing any character to match, and then again use ren pattern matching to ignore the problem.

SETLOCAL ENABLEDELAYEDEXPANSION

for /f "delims=" %%I in ('dir /b /a-d') do (
 Set K=%%I
 ren "!K:-=?!" "test2_!K:-=?!"
)

but ren * x* replacesx 文件的开头,所以上面用内容替换连字符在插入 test_ 之前的那个位置.

but ren * x* replaces the start of the files with x, so the above replaces the hyphens with the content at that location before test_ was inserted.

所以这种方法可以做的最好的事情是将破折号转换为连字符:

SETLOCAL ENABLEDELAYEDEXPANSION

for /f "delims=" %%I in ('dir /b /a-d') do (
 Set K=%%I
 ren "!K:-=?!" "test2_!K!"
)

)

并确认是 dir/b 的输出是问题所在:在命令行上:

And confirming it is the output of dir /b that is the problem: on the command line:

dir /b *—* > test.txt

其中 是一个长破折号,只会列出带有长破折号的文件,但是,在输出文件中,例如Notepad test.txt,你只会找到连字符,没有破折号.

where that is an em dash, will only list the files with em dashes, but, in the output file, e.g. Notepad test.txt, you'll only find hyphens, and no em dashes.

顺便说一句,我已经在 Windows 8.1 上完成了所有这些测试,VER 显示为 Microsoft Windows [Version 6.3.9600].

BTW I've done all this testing on Windows 8.1 which VER displays as Microsoft Windows [Version 6.3.9600].

(正如我上面提到的,我在这个答案中确实有 ren * x* ,但是x 替换了第一个字符> 而不是插入它,我一直认为 ren 做到了!)

(As I mention above I did have ren * x* in this answer, but that replaces the first character with x rather than insert it, which I always thought ren did!)

这篇关于如何处理文件名中的破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆