15

我正在尝试重命名文件并使用以下代码,但它似乎不起作用。有人可以告诉我为什么吗?从 VBScript 重命名文件的正确方法是什么?

FSO.GetFile("MyFile.txt).Name = "Hello.txt"

我正在使用此线程作为参考:重命名文件而不在同一文件夹中复制

4

7 回答 7

36

您可以通过移动文件使用 FSO 重命名文件:MoveFile Method

Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "A.txt", "B.txt"
于 2013-07-15T17:37:08.643 回答
13

我只看到您的代码不起作用的一个原因,文件名字符串后缺少引号:

VB脚本:

FSO.GetFile("MyFile.txt[missed_quote_here]).Name = "Hello.txt"
于 2013-07-15T19:52:27.757 回答
1

是的,你可以这么做。
在这里,我将 .exe 文件重命名为 .txt 文件

重命名文件

Dim objFso  
Set objFso= CreateObject("Scripting.FileSystemObject")  
objFso.MoveFile "D:\testvbs\autorun.exe", "D:\testvbs\autorun.txt"
于 2017-12-07T05:41:51.997 回答
0
Rename filename by searching the last character of name. For example, 

Original Filename: TestFile.txt_001
Begin Character need to be removed: _
Result: TestFile.txt

Option Explicit

Dim oWSH
Dim vbsInterpreter
Dim arg1 'As String
Dim arg2 'As String
Dim newFilename 'As string

Set oWSH = CreateObject("WScript.Shell")
vbsInterpreter = "cscript.exe"

ForceConsole()

arg1 = WScript.Arguments(0)
arg2 = WScript.Arguments(1)

WScript.StdOut.WriteLine "This is a test script."
Dim result 
result = InstrRev(arg1, arg2, -1)
If result > 0 then
    newFilename = Mid(arg1, 1, result - 1)
    Dim Fso
    Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
    Fso.MoveFile arg1, newFilename
    WScript.StdOut.WriteLine newFilename
End If



Function ForceConsole()
    If InStr(LCase(WScript.FullName), vbsInterpreter) = 0 Then
        oWSH.Run vbsInterpreter & " //NoLogo " & Chr(34) &     WScript.ScriptFullName & Chr(34)
        WScript.Quit
    End If
 End Function
于 2016-05-27T12:15:06.100 回答
0

据我了解,您的上下文是从 ALM 下载。在这种情况下,ALM 将文件保存在:C:/Users/ user /AppData/Local/Temp/TD_80/ ALM_VERSION / random_string /Attach/ artefact_type / ID下

在哪里 :

ALM_VERSION是您的 alm 安装版本,例如 12.53.2.0_952

artefact_type是人工制品的类型,例如:REQ

ID是工件的 ID

下面的代码示例连接到 ALM 实例、域“DEFAUT”、项目“MY_PROJECT”,从 ID 为 6 的 REQ 获取所有附件并将它们保存在 c:/tmp 中。它是 ruby​​ 代码,但很容易转录为 VBSctript

require 'win32ole'
require 'fileutils'

# login to ALM and domain/project 
alm_server = ENV['CURRRENT_ALM_SERVER']
tdc = WIN32OLE.new('TDApiOle80.TDConnection')
tdc.InitConnectionEx(alm_server)
username, password = ENV['ALM_CREDENTIALS'].split(':')
tdc.Login(username, password)
tdc.Connect('DEFAULT', 'MY_PROJECT')

# get a handle for the Requirements 
reqFact = tdc.ReqFactory

# get Requirement with ID=6
req = reqFact.item(6)

# get a handle for the attachment of REQ 
att = req.Attachments

# get a handle for the list of attachements
attList = att.NewList("")

thePath= 'c:/tmp'

# for each attachment:
attList.each do |el|
  clientPath = nil

  # download the attachment to its default location
  el.Load true, clientPath

  baseName = File.basename(el.FileName)
  dirName = File.dirname(el.FileName)
  puts "file downloaded as : #{baseName}\n in Folder #{dirName}"  
  FileUtils.mkdir_p thePath
  puts "now moving #{baseName} to #{thePath}"  
  FileUtils.mv el.FileName, thePath
end

输出:

=> 文件下载为:REQ_6_20191112_143346.png

=> 在文件夹 C:\Users\user\AppData\Local\Temp\TD_80\12.53.2.0_952\e68ab622\Attach\REQ\6

=> 现在将 REQ_6_20191112_143346.png 移动到 c:/tmp

于 2019-12-13T08:16:19.703 回答
-1

下面的代码绝对适用于我更新文件扩展名。

例如:abc.pdf 到 abc.txt

Filepath = "Pls mention your Filepath"

Set objFso = CreateObject("Scripting.FileSystemObject")

'' Below line of code is to get the object for Folder where list of files are located 
Set objFolder = objFso.GetFolder(Filepath)

'' Below line of code used to get the collection object to hold list of files located in the Filepath.
Set FileCollection = objFolder.Files

For Each file In FileCollection

    WScript.Echo "File name ->" + file.Name
    ''Instr used to Return the position of the first occurrence of "." within the File name
    s = InStr(1, file.Name, ".",1)
    WScript.Echo s
    WScript.Echo "Extn --> " + Mid(file.Name, s, Len(file.Name))

    'Left(file.Name,s-1) = Used to fetch the file name without extension
    ' Move method is used to move the file in the Desitnation folder you mentioned
    file.Move(Filepath & Left(file.Name,s-1)&".txt") 

Next
于 2018-12-21T06:15:53.997 回答
-1

使用 VB SCript 重命名文件。

  1. 在 D : Drive 中创建文件夹源和存档。[您可以选择其他驱动器,但将代码从 D:\Source 更改为 C:\Source,以防您在 C:Drive 中创建文件夹]
  2. 将文件保存在要重命名的 Source 文件夹中。
  3. 保存以下代码并将其另存为 .vbs 例如 ChangeFileName.vbs
  4. 运行文件,文件将使用现有文件名和当前日期重命名

    选项显式

    暗淡 fso,sfolder,fs,f1,CFileName,strRename,NewFilename,GFileName,CFolderName,CFolderName1,Dfolder,afolder

    暗淡我的日期

    我的日期 =日期

    函数 pd(n, totalDigits)

        if totalDigits > len(n) then 
    
            pd = String(totalDigits-len(n),"0") & n 
    
        else 
    
            pd = n 
    
        end if 
    

    结束功能

    我的日期= Pd(DAY(日期()),2) & _

    Pd(月(日期()),2) & _

    年(日期())

    'MsgBox ("在 D 盘中创建文件夹 'Source' 'Destination' 和 'Archive'。将 PDF 文件保存到源文件夹 ")

    sfolder="D:\源\"

    'Dfolder="D:\目的地\"

    afolder="D:\存档\"

    设置 fso= CreateObject("Scripting.FileSystemObject")

    设置 fs= fso.GetFolder(sfolder)

    对于 fs.files 中的每个 f1

            CFileName=sfolder & f1.name
    
            CFolderName1=f1.name
    
            CFolderName=Replace(CFolderName1,"." & fso.GetExtensionName(f1.Path),"")
    
            'Msgbox CFileName 
    
            'MsgBox CFolderName 
    
            'MsgBox myDate
    
            GFileName=fso.GetFileName(sfolder)
    
            'strRename="DA009B_"& CFolderName &"_20032019"
    
            strRename= "DA009B_"& CFolderName &"_"& myDate &""
    
            NewFilename=replace(CFileName,CFolderName,strRename)
    
            'fso.CopyFile CFolderName1 , afolder
    
            fso.MoveFile CFileName , NewFilename
    
            'fso.CopyFile CFolderName, Dfolder
    

    下一个

    MsgBox "文件重命名成功!!!"

    设置 fso= 无

    设置 fs=Nothing

于 2019-03-26T09:46:57.350 回答