2011-01-12 3 views
1

밑줄이있는 파일 이름에 공백이있는 Windows 서브 디렉토리의 파일 이름을 바꾸는 스크립트를 제안 할 수 있습니까? 예를 들어 파일 이름이Windows 서브 디렉토리의 파일 이름을 바꾸는 스크립트

abc xyz.pdf 

그것은

abc_xyz.pdf 
+0

서브 디렉토리의 서브 디렉토리로 되풀이해야합니까? – Casey

+0

디렉토리에 재귀가되다 – iceman

+0

하위 디렉토리 이름에 공백이 있으면 어떻게됩니까? 있는 그대로 둡니다. 또는 이름을 바꿉니 까? 이름을 바꾸면 PARENT 디렉토리의 이름을 변경해서는 안된다고 가정합니다. 예 : 'C : \ my docs \ pdfs'로 시작하고'C : \ my docs \ pdfs \ sci fi \ star wars1.pdf' 파일을 가지고 있다면,'C : \ my docs \ pdfs \ sci fi \ star_wars1.pdf','C : \ my docs \ pdfs \ sci_fi \ star_wars1.pdf' 또는'C : \ my_docs \ pdfs \ sci_fi \ star_wars1.pdf' 파일이 있습니까? 나는 # 1을 추측하고있다. – DVK

답변

1

펄 : 재귀 찾기 및 파일에 actioning에 대한 사용 File::Find.

다음을주의해야합니다. 밑줄이있는 DIRECTORIES의 이름을 바꾸지 마십시오. 따라서 File::Basename.

use File::Find; 
use File::Basename; 
use File::Spec; 
use strict; 

find ({ 'wanted' => \&renamefile }, 'X:\my\sub\dir'); 

sub renamefile { 
    my $file = $_; 
    return unless (-f $file); # Don't rename directories! 
    my $dirname = dirname($file); # file's directory, so we rename only the file itself. 
    my $file_name = basename($file); # File name fore renaming. 
    my $new_file_name = $file_name; 
    $new_file_name =~ s/ /_/g; # replace all spaces with underscores 
    rename($file, File::Spec->catfile($dirname, $new_file_name)) 
     or die $!; # Error handling - what if we couldn't rename? 
} 
+1

질문에 대한 언급에서 알 수 있듯이 (1) 시작 디렉토리의 하위 디렉토리에 공백이 있고 (2) 시작 디렉토리에 공백이 있으면 어떻게 될지 명확하지 않습니다. 위의 코드에서 대답은 "아무것도"아니라고 가정합니다. 하위 디렉토리의 이름을 바꾸고 싶지만 시작 디렉토리는 바꾸지 않으려면 좀 더 복잡해집니다. – DVK

+0

이렇게하면됩니다. 나는 그것도 필요로한다. – Hongtao

0

다음 배치 파일을 테스트되지해야하지만, 작업을해야하는 경우.

@echo off 
for %%i in (*) do call :rename "%%~ni" 
goto :EOF 

:rename 
set filename=%1 
set newname=%filename: =_% 
rename "filename" "newname" 
0

다음은 VBScript 스크립트입니다. 이 폴더 C에있는 파일의 이름을 변경합니다 : 여전히 PERL 그래서이의 자상했다 학습 테스트

 
    Dim fso, f, f1, fc, s 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set f = fso.GetFolder("C:\Test") 
    Set fc = f.Files 
    For Each f1 in fc 
     f1.move f1.ParentFolder & "\" & replace(f1.Name, " ", "_") 
    Next 
+0

'f1.move' 줄에 구문 오류가있을 수 있다고 생각합니다. – Casey

+0

구문 오류가 없습니다. 디렉터리를 통해 실행되지 않습니다. VBScript에는 renamefile 메서드가 없습니다. 이동은 그것을위한 방법입니다. – BZ1

0

임 \ ...

opendir (curDir, "."); 

@filesWithSpaces = grep(/.*\s.*\..*/, readdir (curDir)); 

foreach $oneFile (@filesWithSpaces){ 
    $newName = $oneFile; 
    $newName =~ s/\ /_/g; 
    print "RENAMING: $oneFile -> $newName \n"; 
    rename($oneFile, $newName); 

}

내 초기 테스트에 잘 보인다. 그 재귀 아니지만.

+0

여기에는 DIRECTORY 이름의 공백을 바꾸는 것과 같은 몇 가지 오류가 있습니다. – DVK

0

다음은 작동하는 VBScript입니다. 그러나 지정된 디렉토리의 하위 디렉토리로 재귀하지는 않습니다.

Dim fso 
Dim folder 
Dim stringToFind 
Dim replacement 

' Check arguments 
If Wscript.Arguments.Count <> 3 Then 
    ' Usage 
    Wscript.echo "Usage: rename.vbs folder string_to_find replacement" 
    WScript.Quit 
End If 

Set fso = CreateObject("Scripting.FileSystemObject") 

Set folder = fso.GetFolder(WScript.Arguments(0)) 

If Err.Number <> 0 Then 
    WScript.Echo "Folder " & WScript.Arguments(0) & " does not exist." 
    WScript.Quit 
End If 

stringToFind = WScript.Arguments(1) 
replacement = WScript.Arguments(2) 

For Each file in folder.Files 
    fso.MoveFile file.ParentFolder & "\" & file.Name, file.ParentFolder & "\" & Replace(file.Name, stringToFind, replacement) 
Next 

Set folder = Nothing 
Set fso = Nothing 
관련 문제