我正在尝试重命名文件,并且正在使用下面的代码,但是它似乎不起作用。有人可以告诉我为什么吗?从VBScript重命名文件的正确方法是什么?
FSO.GetFile("MyFile.txt).Name = "Hello.txt"
我正在将此线程用作参考:Rename files without copying in same folder
您可以通过以下方式使用FSO重命名文件:MoveFile Method。
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "A.txt", "B.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"
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
使用VB SCript重命名文件。
运行文件,该文件将使用现有文件名和当前日期重命名
选项显式
Dim fso,sfolder,fs,f1,CFileName,strRename,NewFilename,GFileName,CFolderName,CFolderName1,Dfolder,Afolder
Dim myDate
myDate = Date
函数pd(n,totalDigits)
if totalDigits > len(n) then
pd = String(totalDigits-len(n),"0") & n
else
pd = n
end if
结束功能
myDate =Pd(DAY(date()),2)&_
Pd(Month(date()),2)&_
YEAR(Date())
'MsgBox(“在D驱动器中创建文件夹'源''目标'和'存档。将PDF文件保存到源文件夹”)
sfolder =“ D:\ Source \”
'Dfolder =“ D:\ Destination \”
afolder =“ D:\ archive \”
Set 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“文件重命名成功!!”
Set fso = Nothing
Set fs = Nothing
据我了解,您的情况是从ALM下载。在这种情况下,ALM将文件保存在以下位置:C:/用户/ 用户 / AppData / Local / Temp / TD_80 / ALM_VERSION / 随机字符串 /附加/ 人工类型 / ID
其中:
ALM_VERSION是您的alm安装的版本,例如12.53.2.0_952
artefact_type是人工制品的类型,例如:REQ
ID是制品的ID
下面是连接到ALM实例,域'DEFAUT',项目'MY_PROJECT'的代码示例,从ID为6的REQ获取所有附件,并将其保存在c:/ tmp中。它是红宝石代码,但很容易转录为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
下面的代码绝对可以用来更新文件扩展名。
例如: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