2013-09-22 1 views
0

작업중인 vbscript에 문제가 있습니다. 스크립트의 이름을 변경 한 다음 일부 FTP 로그 파일을 한 폴더에서 다른 폴더로 이동합니다. 스크립트가 작동하지만 delete_junkfiles.log라는 파일이 있다는 것을 알았지 만이 파일의 이름을 바꾸거나 이동하지 않으려 고합니다. 원본 폴더에서 그대로 남아 있습니다.VBScript로 로그 파일 이름 바꾸기 및 이동 문제가 발생했습니다.

현재 스크립트의 이름을 바꾸고 모든 파일을 이동 중입니다. 가능하면 스크립트 파일의 이름이 바뀌 었음에도 불구하고이 스크립트는 파일을 찾을 수 없으므로 오류가 발생합니다.

vbscript 전문가가 알고있는 사실은 매우 쉽습니다. ,하지만 난 꽤 vbs에 새로운이야 그리고 난 그냥 delete_junkfiles.log 무시하고 그냥 혼자두고 떠날 방법을 알아낼 수 없습니다. 녀석이 도움을 줄 수 있다면 도움이 될 것입니다. 다음은 내 스크립트 ....

Dim WshShell, FileManagement, BrowseDialogBox, SelectedFolder, OldString, NewString, FullPath, TheFolder, FileList 
Dim File, ThisFile, TheString, AlreadyRenamed, TempName, FlagName, Success, FindFlag, NewName, Dummy 

Set WshShell = WScript.CreateObject("WScript.Shell") 
Set FileManagement = WScript.CreateObject ("Scripting.FileSystemObject") 
Set BrowseDialogBox = WScript.CreateObject("Shell.Application") 
Set SelectedFolder = BrowseDialogBox.BrowseForFolder(0, "Select the folder containing the files you want to rename.", &H0001) 

If InStr(1, TypeName(SelectedFolder), "Folder") = 0 Then 
Wscript.Quit 
Else 
OldString = InputBox("Enter the characters in the filename that you want to replace","Rename Files") 
If OldString = "" Then Wscript.Quit 

NewString = InputBox("Enter the characters that you want to replace them with","Rename Files") 
If NewString = "" Then Wscript.Quit 
End If 

FullPath = SelectedFolder.ParentFolder.ParseName(SelectedFolder.Title).Path 

Set TheFolder = FileManagement.GetFolder(FullPath) 
Set FileList = TheFolder.Files 
Success = 0 


ThisFile = File.Name 
TheString = InStr(ThisFile, OldString) 
AlreadyRenamed = InStr(ThisFile, "%") 

If (TheString <> 0) AND (AlreadyRenamed = 0) Then 
Success = 1 

TempName = Replace(ThisFile, OldString, NewString) 
FlagName = "%" + TempName 
File.Name = FlagName 
End If 
Next 

For Each File in FileList 
ThisFile = File.Name 
FindFlag = InStr(ThisFile, "%") 


If FindFlag <> 0 Then 
NewName = Replace(ThisFile, "%", "") 
File.Name = NewName 
End If 
Next 

'Move the files 
For Each File in FileList 
FileManagement.MoveFile "C:\Users\lislej\Desktop\test_move\*.log", "C:\Users\lislej\Desktop\test_move_to\" 
Next 

If Success = 1 Then 
Dummy = WshShell.Popup ("Rename Files operation complete!",5,"Rename Files",64) 
Else 
Dummy = WshShell.Popup ("Rename Files operation failed! Please repeat the operation.",0,"Rename Files",16) 
End If 

Wscript.Quit 

답변

0

For없는 Next이있는 한 그것은 뭔가가 누락의 코드처럼 보이는 것입니다. 그러나 파일 이동 부분은 그대로이고 파일 이름이 건너 뛸 수 있도록이 확인해야 곳이다 :

Set FileList = TheFolder.Files 
'Move the files 
For Each File in FileList 
    If InStr(File, "delete_junkfiles") = 0 Then 
     ' If "delete_junkfiles" is not find in the filename then do move file 
     File.Move "C:\Users\lislej\Desktop\test_move_to\" 
    End If 
Next 

업데이트 : 당신이 파일을 이름을 변경 한 후 파일 목록을 새로 고칠 필요가 있다고 생각

?

+0

고맙습니다. @PatricK, 코드 복사 및 붙여 넣기시 for 루프의 일부를 생략했습니다 .... 도움을 주셔서 감사합니다.하지만 문제가 조금 더 나아질 필요가 있다고 생각합니다. –

+0

내가 조금 더 나아지고있는 문제를 설명했다. 위의 @Oracle에 답하는 댓글의 일부이다. 다시 감사한다. –

+0

@JohnLisle 어떤 생각을하고 나면 이름을 변경 한 후에'FileList'를 다시 읽어 들여야한다고 생각한다. 그리고 당신은'File.Move'를 호출 할 수 있어야합니다. – PatricK

0

나는 모두가 기본적으로

'Move the files 
For Each File in FileList 
    if File.Name <> "delete_junkfiles.log" then 
     FileManagement.MoveFile File.Path, "C:\Users\lislej\Desktop\test_move_to\" 
    end if 
Next 

'Move the files 
For Each File in FileList 
FileManagement.MoveFile "C:\Users\lislej\Desktop\test_move\*.log", "C:\Users\lislej\Desktop\test_move_to\" 
Next 

을 변경하여 오류가 해결 될 것입니다 찾을 수 없음 delete_junkfiles.log 파일과 파일을 무시 생각, 무슨 일을하고 각 파일을 통해 반복하지만, 사용 된 와일드 카드는 MoveFile()에 있습니다. 첫 번째 반복이 모두 패턴과 일치하는 파일을 이동했기 때문에 파일을 찾을 수 없습니다. 두 번째 반복은 파일이 없으므로 오류가 발생했을 것입니다. 무시하고 싶은 파일을 피하기 위해 if을 추가했습니다.

+0

감사 지적한 전문가, 나는 나의 부호에 당신의 추가를 시도하고 나의 파일을 고쳐 줬다 문제, 감사한다, 그러나 아직도 모두를 개명하고 이동한다 그것을 이렇게 지적했다. –

+0

감사합니다. @ oracle 나는 내 코드에 당신의 추가를 시도했지만 문제가 발견되지 않았다. 고맙다.하지만 여전히 파일의 이름을 바꾸고 옮긴다. 기본적으로 FTPPub1_aaplog.ftp.log, FTPPub1_netdesklog.ftp.log, TPurge.log, TPurge_Others.log 및 delete_junkfiles.log라는 파일이있는 폴더가 있습니다. delete_junkfiles.log를 제외한 모든 파일은 이름을 바꾼 다음 이동 한 다음 원래 파일 이름으로 로그 파일을 다시 생성하면 delete_junk 파일은 변경되지 않아야합니다. 이 작업을 수행하는 쉬운 방법이 있다는 것을 알고 있지만 문제가 너무 가깝다고 생각합니다. –

관련 문제