0

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

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

我已经重命名了字符串,但是当我到达 fso.movefile 函数时,实际的 move 函数说 path not found 。

这是我到目前为止所拥有的。

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

任何帮助将不胜感激。

4

1 回答 1

0

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

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

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
于 2018-11-21T21:33:46.903 回答