如何使用VBScript重命名文件?

如何使用VBScript重命名文件?,vbscript,Vbscript,我试图重命名一个文件,并使用了下面的代码,但它似乎不工作。有人能告诉我为什么吗?从VBScript重命名文件的正确方法是什么 FSO.GetFile("MyFile.txt).Name = "Hello.txt" 我使用此线程作为参考:您可以通过移动文件来使用FSO重命名该文件: 您可以使用FSO通过移动文件来重命名该文件: 我只看到代码不起作用的一个原因,文件名字符串后缺少引号: VBScript: FSO.GetFile("MyFile.txt[missed_quote_here]).Na

我试图重命名一个文件,并使用了下面的代码,但它似乎不工作。有人能告诉我为什么吗?从VBScript重命名文件的正确方法是什么

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

我使用此线程作为参考:

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


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


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

VBScript:

FSO.GetFile("MyFile.txt[missed_quote_here]).Name = "Hello.txt"

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

VBScript:

FSO.GetFile("MyFile.txt[missed_quote_here]).Name = "Hello.txt"
是的,你能做到。
这里我将一个.exe文件重命名为.txt文件

重命名文件

Dim objFso  
Set objFso= CreateObject("Scripting.FileSystemObject")  
objFso.MoveFile "D:\testvbs\autorun.exe", "D:\testvbs\autorun.txt"
是的,你能做到。
这里我将一个.exe文件重命名为.txt文件

重命名文件

Dim objFso  
Set objFso= CreateObject("Scripting.FileSystemObject")  
objFso.MoveFile "D:\testvbs\autorun.exe", "D:\testvbs\autorun.txt"

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

示例: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

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

示例: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

使用VB脚本重命名文件

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

    选项显式

    Dim fso、sfolder、fs、f1、CFileName、strRename、NewFilename、GFileName、CFolderName、CFolderName1、Dfolder、afolder

    暗myDate

    myDate=日期

    函数pd(n,总位数)

    端函数

    myDate= Pd(日期()),2)和

    Pd(月(日()),2)和

    年份(日期())

    'MsgBox(“在D驱动器中创建文件夹'源''目标'和'存档'。将PDF文件保存到源文件夹”)

    sfolder=“D:\Source\”

    'Dfolder=“D:\Destination\”

    afolder=“D:\archive\”

    设置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


  • 使用VB脚本重命名文件

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

    选项显式

    Dim fso、sfolder、fs、f1、CFileName、strRename、NewFilename、GFileName、CFolderName、CFolderName1、Dfolder、afolder

    暗myDate

    myDate=日期

    函数pd(n,总位数)

    端函数

    myDate= Pd(日期()),2)和

    Pd(月(日()),2)和

    年份(日期())

    'MsgBox(“在D驱动器中创建文件夹'源''目标'和'存档'。将PDF文件保存到源文件夹”)

    sfolder=“D:\Source\”

    'Dfolder=“D:\Destination\”

    afolder=“D:\archive\”

    设置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


  • 据我所知,您的上下文是从ALM下载的。 在这种情况下,ALM将文件保存在以下位置: C:/Users/user/AppData/Local/Temp/TD\u 80/ALM\u版本/随机字符串/Attach/人工制品类型/ID

    其中:

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

    人工制品类型是人工制品的类型,例如: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_14346.png

    =>在文件夹C:\Users\user\AppData\Local\Temp\TD\u 80\12.53.2.0\u 952\e68ab622\Attach\REQ\6中


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

    据我所知,您的上下文是从ALM下载的。 在这种情况下,ALM将文件保存在以下位置: C:/Users/user/AppData/Local/Temp/TD\u 80/ALM\u版本/随机字符串/Attach/人工制品类型/ID

    其中:

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

    人工制品类型是人工制品的类型,例如: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_14346.png

    =>在文件夹C:\Users\user\AppData\Local\Temp\TD\u 80\12.53.2.0\u 952\e68ab622\Attach\REQ\6中


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

    我不想移动文件,只想重命名它..你能举一个你说的例子吗。我尝试使用MoveTo方法,代码是FSO.MoveTo\\net\stat\help.txt\\net\stat\main.css,但它无法移动(
    MoveFile
    ,而不是
    MoveTo
    )在卷内重命名与重命名没有太大区别,实际上是相同的操作:更新文件描述符而不复制实际数据。这就是为什么你没有专门的重命名方法的基本原因。很抱歉使用了MoveFile..MoveTo是一个打字错误..仍然不起作用得到问题..w