2013-03-09 3 views
0

프로그래밍상의 잘못을 용서하십시오. 이것이 천재가 존재하는 이유입니다!디렉토리의 목록에서 단일 파일의 이름을 바꿉니다.

Sched 작업을 통해 30 분마다 단일 파일의 이름을 변경하고 싶습니다.

파일 목록 :

있는 test1.txt test2.txt가 test3.txt 요법 .. 속으로

: 있는 test.txt test2.txt가 text3.txt 요법 ..

test.txt는 프로그램에서 삭제됩니다. 따라서 30 분 시간에 test2.txt를 test.txt로 이름을 바꾸고 모든 파일이 처리 될 때까지 계속합니다.

감사합니다. 발견 된 파일은 Rename different files to one file name, one at a time이지만 파일 만 복사합니다.

답변

2

기본 이름이있는 파일이 있는지 확인하고, 기본 이름에 가장 작은 번호가 붙은 파일 이름을 바꿀 수 있습니다. 파일 이름에

Const basename = "test" 
Const srcFolder = "..." 
Const extension = "txt" 

Set fso = CreateObject("Scripting.FileSystemObject") 

dstFile = fso.BuildPath(srcFolder, basename & "." & extension) 

If fso.FileExists(dstFile) Then WScript.Quit 0 'nothing to do 

For Each f In fso.GetFolder(srcFolder).Files 
    If LCase(fso.GetExtensionName(f.Name)) = extension Then 
    If LCase(Left(f.Name, Len(basename))) = basename Then 
     num = Mid(fso.GetBaseName(f.Name), Len(basename)+1) 
     If Len(num) > 0 Then 
     num = CInt(num) 
     If IsEmpty(minnum) Or minnum > num Then minnum = num 
     End If 
    End If 
    End If 
Next 

If Not IsEmpty(minnum) Then 
    srcFile = fso.BuildPath(srcFolder, basename & minnum & "." & extension) 
    fso.MoveFile srcFile, dstFile 
End If 

검사를하고, 숫자는 정규 표현식에 대해 테스트를 통해 조금 단순화 할 수있다 : 이런 식으로 뭔가를 시도 프롬프트 응답

Set re = New RegExp 
re.Pattern = "^" & basename & "(\d+)\." & extension & "$" 
re.IgnoreCase = True 

For Each f In fso.GetFolder(srcFolder).Files 
    Set m = re.Execute(f.Name) 
    If m.Count > 0 Then 
    num = CInt(m(0).SubMatches(0)) 
    If IsEmpty(minnum) Or minnum > num Then minnum = num 
    End If 
Next 
+0

감사합니다! 이 줄에 오류가있는 것 같습니다 num = Mid (fso.GetBaseName (f.Name), Len (basename) +1)) – user2151337

+0

사과. 명령문의 예상 종료. Line 14 Char 58 – user2151337

+0

Brilliant! 치료를합니다. 당신의 도움에 많은 감사드립니다! – user2151337

관련 문제