【问题标题】:VBS to Rename File via Send To MenuVBS 通过发送到菜单重命名文件
【发布时间】:2019-04-24 09:44:49
【问题描述】:

每天我都必须重命名文件以用破折号替换空格,然后才能将它们发送到我们的机器(我们的一些机器不读取文件名中的空格,但我们的文件命名约定有空格,利益冲突我知道)。

有时它是一个文件,有时它是六个文件,所以我的方法是使用 Windows 发送到菜单将选定的文件发送到脚本。

我已经重命名了字符串,但是当我到达 fso.movefile 函数时,实际的移动函数说找不到路径。

这是我目前所拥有的。

Set objArgs = WScript.Arguments
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")

'Cycle through files
    For I = 0 to objArgs.Count - 1
' Assign array entry to variable
    t = objArgs(I)
' Parse variable to replace spaces with dashes
    s = Replace(t, " ", "-")
' Let me know how I did
    WScript.Echo t & vbcrlf & s
'Move 'em
    fso.movefile t, s
Next

任何帮助将不胜感激。

【问题讨论】:

  • ' Let me know how I did 你愿意分享吗?

标签: file vbscript rename movefile


【解决方案1】:

所以我的问题是我所在的文件夹也有空格。所以我的代码试图重命名为一个不存在的文件夹。

一旦我从路径中解析出文件名并将修改后的文件名重新分组,它的效果就很好。代码如下。

Dim t ' original file and path
Dim s ' file name only with spaces
Dim u ' new file name without spaces
Dim v ' path only
Dim obj
Set objArgs = WScript.Arguments
set obj = WScript.CreateObject("Scripting.FileSystemObject")

'Cycle through files
    For I = 0 to objArgs.Count - 1
        ' Assign array entry to variable
            t = objArgs(I)
        ' Parse file name from path
            s = Right(t, Len(t) - InStrRev(t, "\"))
        ' Remove file name from path
            v = left(t, InStrRev(t, "\"))
        ' Parse variable to replace spaces with dashes
            u = Replace(s, " ", "-")
'       ' Let me know how I did
'           WScript.Echo "Was(t):    " & t & vbcrlf & "Is(s):     " & s & vbcrlf & "Path:  " & v
        'Move 'em
        obj.movefile t, v & u
    Next

【讨论】:

  • 有比使用字符串操作更好的方法来获取文件名。见FileSystemObject.GetFileName(path)
  • 哈哈哈,是的,但那太容易了……(j/k)谢谢。这让事情变得容易多了。并且不那么令人沮丧。
猜你喜欢
  • 1970-01-01
  • 2013-10-17
  • 2016-11-01
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
  • 2017-01-13
  • 2023-03-09
  • 2019-10-20
相关资源
最近更新 更多