2011-02-01 3 views
1

vb6에서 cmd line을 사용하여 파일의 이름을 바꾸는 방법은 무엇입니까?vb6에서 cmd line을 사용하여 파일의 이름을 바꾸는 방법은 무엇입니까?

내 코드의 문제점은 무엇입니까?

Shell "cmd ren D:\before.txt after.txt" 
+1

당신이지고 어떤 오류 :

사용, 이름 문을 VB6에서 파일 이름을 바꾸려면? –

+0

오류가 없습니다. 하지만 그것은 나에게 cmd 창만 보여 주며 파일 이름은 변경하지 않습니다. 왜 ? – faressoft

+0

파일의 이름을 바꿀 때 cmd 줄이 필요하지 않습니다. :) – faressoft

답변

-2

파일 이름을 바꾸려면 코드 대신 명령 줄을 사용하는 이유는 무엇입니까? cmd를 사용해야하는 좋은 이유가 없다면 훨씬 안정적인 Windows API를 사용하는 것이 좋습니다. 다음은 SHFileOperation을 사용하여 파일의 이름을 바꾸는 예제입니다. 이것을 어떤 모듈이나 클래스에 드롭하십시오.

Option Explicit 

Private Type SHFILEOPSTRUCT 
     hwnd As Long 
     wFunc As Long 
     pFrom As String 
     pTo As String 
     fFlags As Integer 
     fAnyOperationsAborted As Long 
     hNameMappings As Long 
     lpszProgressTitle As Long ' only used with FOF_SIMPLEPROGRESS; sets dialog title 
End Type 

' For info, see: http://msdn.microsoft.com/en-us/library/bb759795%28v=vs.85%29.aspx 
Private Const FO_COPY     As Long = &H2 
Private Const FO_DELETE     As Long = &H3 
Private Const FO_MOVE     As Long = &H1 
Private Const FO_RENAME     As Long = &H4 
Private Const FOF_ALLOWUNDO    As Long = &H40 
Private Const FOF_CONFIRMMOUSE   As Long = &H2 
Private Const FOF_FILESONLY    As Long = &H80 
Private Const FOF_MULTIDESTFILES  As Long = &H1 
Private Const FOF_NO_CONNECTED_ELEMENTS As Long = &H2000 
Private Const FOF_NOCONFIRMATION  As Long = &H10 
Private Const FOF_NOCONFIRMMKDIR  As Long = &H200 
Private Const FOF_NOCOPYSECURITYATTRIBS As Long = &H800 
Private Const FOF_NOERRORUI    As Long = &H400 
Private Const FOF_NORECURSION   As Long = &H1000 
Private Const FOF_RENAMEONCOLLISION  As Long = &H8 
Private Const FOF_SILENT    As Long = &H4 
Private Const FOF_SIMPLEPROGRESS  As Long = &H100 
Private Const FOF_WANTMAPPINGHANDLE  As Long = &H20 
Private Const FOF_WANTNUKEWARNING  As Long = &H4000 

Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long 

Public Function FileRename(OldFileNameAndPath As String, NewFileNameAndPath As String) As Boolean 
    Dim oP As SHFILEOPSTRUCT, lRet As Long 
    On Error GoTo Err 

    ' Prepare FILEOP struct 
    With oP 
     .wFunc = FO_RENAME       ' Set function 
     .pTo = NewFileNameAndPath     ' Set new path 
     .pFrom = OldFileNameAndPath     ' Set current path 
     .fFlags = FOF_SILENT + FOF_SIMPLEPROGRESS ' Set your flags; FOF_SILENT + FOF_SIMPLEPROGRESS will silently rename a file 
    End With 

    ' Perform operation 
    lRet = SHFileOperation(oP) 

    If lRet = 0 Then 
     FileRename = True 
    Else 
     FileRename = False 
    End If 

    Exit Function 
Err: 
    ' TODO: Log Error 
End Function 
+1

OMG 왜 VB6 내장 명령'Name "D : \ before.txt"As "after.txt" – MarkJ

+1

을 사용하면 프로세스를 더 잘 제어 할 수 있습니다. –

7

정말 제대로 작동하려면/C 또는/K가 필요합니다. Cmd /?를 참조하십시오. 자세한 내용은.

셸 기능도 명령 줄 프롬프트와 다릅니다. 이전 버전의 VB와 Windows의 잔재이며 WinExec() API의 래퍼입니다.

Name "D:\before.txt" As "after.txt" 
관련 문제