我正在尝试重命名文件并使用下面的代码,但它似乎不起作用。有人可以告诉我为什么吗?我正在使用此帖子作为参考Rename files without copying in same folder
FSO.GetFile(“MyFile.txt).Name =”Hello.txt“
答案 0 :(得分:30)
您可以使用FSO重命名该文件:MoveFile Method。
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "A.txt", "B.txt"
答案 1 :(得分:10)
我发现只有一个原因导致您的代码无效,错过了文件名字符串后的引用:
的VBScript:
FSO.GetFile("MyFile.txt[missed_quote_here]).Name = "Hello.txt"
答案 2 :(得分:1)
是的,你可以这样做 这里我将.exe文件重命名为.txt文件
重命名文件
Dim objFso
Set objFso= CreateObject("Scripting.FileSystemObject")
objFso.MoveFile "D:\testvbs\autorun.exe", "D:\testvbs\autorun.txt"
答案 3 :(得分: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
答案 4 :(得分:0)
使用VB SCript重命名文件。
运行文件,该文件将使用现有文件名和当前日期重命名
显式选项
Dim fso,folder,fs,f1,CFileName,strRename,NewFilename,GFileName,CFolderName,CFolderName1,Dfolder,文件夹
关闭myDate
myDate =日期
函数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 \”
设置fso = CreateObject(“ Scripting.FileSystemObject”)
设置fs = fso.GetFolder(文件夹)
对于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
答案 5 :(得分:0)
据我了解,您的上下文是从ALM下载。 在这种情况下,ALM将文件保存在以下位置: C:/用户/ 用户 / 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中。这是红宝石代码,但是很容易转录为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
答案 6 :(得分:-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