搜索和替换文件名-错误修复 [英] search and replace file names - bug fix

查看:87
本文介绍了搜索和替换文件名-错误修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,用于更改文件名(文件存储在"my_folder"中)中的特定字符串:

Set objFso = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("g:\my folder")
For Each File In Folder.Files
sNewFile = File.Name
sNewFile = Replace(sNewFile,"._epf","_v0_1._epf")
if (sNewFile<>File.Name) then
    File.Move(File.ParentFolder+"\"+sNewFile)
end if
Next

如果在"g:\ my folder"下没有文件夹,则scrpit可以正常工作,否则,如果"g:\ my folder"下没有文件夹,则scrpit可以正常工作.并且其中一个(或多个)文件夹的名称类似于某个文件名,该脚本会产生不需要的结果,例如将替换字符串相乘.

例如,如果我的文件夹"包含:

你好(文件夹)

hello_.epf(文件)

然后脚本最终将文件名更改为:

hello_v0_1_v0_1._epf(不需要的结果)

我希望结果是:

hello_v0_1._epf

我将以这种方式感谢您的快速帮助. 谢谢.

解决方案

我没有费心去弄清楚您的VBScript出了什么问题.但是您用batch-filebatchbatch-rename标记了您的问题.

这是一个简单的单行代码,可以从命令提示符处运行,该命令行将执行您想要的操作.它甚至不需要批处理脚本.

for %F in ("g:\my folder\*._epf") do @ren "%F" "%~nF_v0_1%~xF"

如果要在批处理脚本中运行命令,则需要将所有百分比加倍.

@echo off
for %%F in ("g:\my folder\*._epf") do ren "%%F" "%%~nF_v0_1%%~xF"

编辑

以上内容将在扩展名之前为每个文件名添加新版本后缀.

如果要替换现有的版本号,则解决方案甚至更容易.我假设您的版本始终以_v开头,并且v永远不会出现在文件扩展名中.

ren "g:\my folder\*_v0_1._epf" "*v0_2.*"

上面的命令重命名以_v0_1._epf结尾的所有文件.它会保留名称中最后出现v之前的所有字符,然后添加新的版本号,最后附加原始扩展名.

有关REN如何使用通配符的规则,请参见 Windows RENAME命令如何解释通配符?.

I have a script that changes particular string within files names (the file stores in "my_folder"):

Set objFso = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("g:\my folder")
For Each File In Folder.Files
sNewFile = File.Name
sNewFile = Replace(sNewFile,"._epf","_v0_1._epf")
if (sNewFile<>File.Name) then
    File.Move(File.ParentFolder+"\"+sNewFile)
end if
Next

the scrpit works fine if there are no folders under "g:\my folder", otherewise, if there are folders in "my folder" and the name of one (or more) of those folders are similiar to some file name, the scrip cause unwanted results like multiplying the replace string.

for example if "my folder" contain:

hello (folder)

hello_.epf (file)

then the script will eventually change the file name to:

hello_v0_1_v0_1._epf (unwanted result)

and i want the result to be:

hello_v0_1._epf

I'll appreciate quick help in this manner. thanks.

解决方案

I haven't bothered to try to figure out where your VBScript is going wrong. But you tagged your question with batch-file, batch, and batch-rename.

Here is a simple one-liner that can be run from the command prompt that will do what you want. It doesn't even need a batch script.

for %F in ("g:\my folder\*._epf") do @ren "%F" "%~nF_v0_1%~xF"

If you want to run the command within a batch script, then you need to double all percents.

@echo off
for %%F in ("g:\my folder\*._epf") do ren "%%F" "%%~nF_v0_1%%~xF"

EDIT

The above will append a new version suffix to each file name, before the extension.

If you want to replace an existing version number, then the solution is even easier. I'm assuming that your version always starts with _v, and v will never occur in the file extension.

ren "g:\my folder\*_v0_1._epf" "*v0_2.*"

The above command renames all files that end with _v0_1._epf. It preserves all characters up through the last occurance of v in the name, then adds the new version number, and finally appends the original extension.

See How does the Windows RENAME command interpret wildcards? for rules on how REN uses wildcards.

这篇关于搜索和替换文件名-错误修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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