2013-07-15 15 views

Odpowiedz

17

Można zmienić nazwę pliku używając FSO przesuwając go. MoveFile Method

Dim Fso 
Set Fso = WScript.CreateObject("Scripting.FileSystemObject") 
Fso.MoveFile "A.txt", "B.txt" 
+0

Nie chcę, aby przenieść plik po prostu zmienić jego nazwę ..can u dać przykład co Mówiąc to. – user505210

+0

Próbowałem użyć metody MoveTo za pomocą tego kodu FSO.MoveTo \\ net \ stat \ help.txt \\ net \ stat \ main.css, ale to nie działa – user505210

+1

Przenoszenie ('MoveFile', a nie' MoveTo') w obrębie Tom nie różni się zbytnio od zmiany nazwy, jest to właściwie ta sama operacja: aktualizacja deskryptora pliku bez kopiowania rzeczywistych danych. Zasadniczo jest to powód, dla którego nie masz dedykowanej metody zmiany nazwy. –

9

widzę tylko jeden powód, Twój kod nie działa, nieodebrane cytat po nazwie pliku ciąg:

VBScript:

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

jest to prawda i znacznie bardziej eleganckie rozwiązanie. – peter

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 
1

Tak, możesz to zrobić.
Oto jestem zmiana nazwy pliku .exe na .txt

zmienić nazwę pliku

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