2009-06-24 2 views
6

Perl 스크립트를 사용하여 Windows 폴더 아이콘을 변경하는 방법이 있습니까?Perl 스크립트를 사용하여 Windows 폴더 아이콘을 변경하는 방법이 있습니까?

내 의도는 "xxx_documents"폴더의 일반 아이콘을 다른 아이콘으로 변경하는 것입니다. 전체 드라이브를 돌보는 방식으로 스크립트를 실행해야합니다.

드라이브에 많은 폴더가 있습니다. "documents"(예 : "xxx_documents"또는 단순히 "documents")라는 각 폴더를 검색하고 해당 아이콘을 "%SystemRoot%\system32\SHELL32.dll" 라이브러리의 아이콘으로 변경해야합니다.

Perl에서도 가능합니까? 이걸 도와 주신 모든 분들께 감사드립니다.

답변

8

정말 Perl로 할 수 있습니다. Windows는 각 폴더에 숨겨진 시스템 Dekstop.ini 파일을 사용하여 디렉토리 아이콘을 제어합니다. 내용은 다음과 같습니다.

[.ShellClassInfo] 
IconFile=%SystemRoot%\system32\SHELL32.dll 
IconIndex=41 

(다른 시스템에서는 가정합니다) 아이콘 41은 트리입니다. 위의 코드를 실행하면

#!/usr/bin/perl 
use strict; 
use warnings; 

use Win32API::File qw(createFile WriteFile fileLastError CloseHandle); 

my $file = createFile(
     'Desktop.ini', 
     { 
      Access  => 'w',  # Write access 
      Attributes => 'hs',  # Hidden system file 
      Create  => 'tc',  # Truncate/create 
     } 
) or die "Can't create Desktop.ini - " . fileLastError(); 

WriteFile(
     $file, 
     "[.ShellClassInfo]\r\n" . 
     "IconFile=%SystemRoot%\\system32\\SHELL32.dll\r\n" . 
     "IconIndex=41\r\n", 
     0, [], [] 
) or die "Can't write Desktop.ini - " . fileLastError(); 

CloseHandle($file) or die "Can't close Desktop.ini - " . fileLastError(); 

것은, 그것이 설정해야합니다 : Windows에서이 우리가 그것을 만들 Win32API::File으로 파고해야합니다 의미,이 파일이 명시 적으로 작동 할 수있는 시스템 파일로 설정 필요 현재의 디렉토리의 아이콘을 트리에 추가합니다. 탐색기가 변경 사항을 선택하기 전에 디렉토리 목록을 새로 고침해야 할 수 있습니다.

이제 아이콘을 변경하는 방법이 생겼으므로 이제는 전체 드라이브를 거쳐 패턴과 일치하는 모든 폴더를 바꿀 수 있습니다. 우리는 File::Find, 또는 그 대안 중 하나 (예를 들어, File::Find::Rule, 또는 File::Next)로 아주 쉽게이 작업을 수행 할 수 있습니다

#!/usr/bin/perl 
use strict; 
use warnings; 
use File::Find qw(find); 
use Win32API::File qw(createFile WriteFile fileLastError CloseHandle); 

my $topdir = $ARGV[0] or die "Usage: $0 path\n"; 

find(\&changeIcon, $topdir); 

sub changeIcon { 
    return if not /documents$/i; # Skip non-documents folders 
    return if not -d;    # Skip non-directories. 

    my $file = createFile(
     "$_\\Desktop.ini", 
     { 
       Access  => 'w',  # Write access 
       Attributes => 'hs',  # Hidden system file 
       Create  => 'tc',  # Truncate/create 
     } 
    ) or die "Can't create Desktop.ini - " . fileLastError(); 

    WriteFile(
     $file, 
     "[.ShellClassInfo]\r\n" . 
     "IconFile=%SystemRoot%\\system32\\SHELL32.dll\r\n" . 
     "IconIndex=41\r\n", 
     0, [], [] 
    ) or die "Can't write Desktop.ini - " . fileLastError(); 

    CloseHandle($file) or die "Can't close Desktop.ini - " . fileLastError(); 
} 

불행하게도, 난 그냥 디렉토리가 현재있는 경우 아이콘 이 변경됩니다 것을 발견했습니다 , 또는 한 번, 아이콘이 있었는지 ... 명백하게 Windows가 Desktop.ini 파일을 찾는 디렉토리 자체에 설정된 속성이 있지만, 그것이 내가 무엇인지 알아낼 수는 없습니다. 따라서 위의 해결책은 불완전합니다. 아이콘을 추가 할 디렉토리의 속성을 찾아 수정해야합니다.

+3

http://msdn.microsoft.com/en-us/library/cc144102.aspx에 따라 포함하는 폴더에 시스템 속성을 설정해야합니다. – ephemient

+0

http://www.google.com/search?btnI=&q=%22Customizing+Folders+with+Desktop.ini%22 –

0

새로 고침 아이콘을 얻으려면, 당신은 몇 가지 SHChangeNotify 부두 호출해야합니다 (C++ 예를 들어,하지만 당신은 아이디어를 얻을) :

int imageIndex = Shell_GetCachedImageIndexW(wPath, GetSyncFolderIconIndex(), 0); 
if (imageIndex != -1) 
{ 
    // If we don't do this, and we EVER change our icon, Explorer will likely keep 
    // using the old one that it's already got in the system cache. 
    SHChangeNotify(SHCNE_UPDATEIMAGE, SHCNF_DWORD | SHCNF_FLUSHNOWAIT, &imageIndex, NULL); 
} 
SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATHW | SHCNF_FLUSHNOWAIT, wPath, NULL); 
2

1.

[.ShellClassInfo] 
[email protected]%SystemRoot%\system32\shell32.dll,-21790 
[email protected]%SystemRoot%\system32\shell32.dll,-12689 
IconResource=%SystemRoot%\system32\imageres.dll,-108 
IconFile=%SystemRoot%\system32\shell32.dll 
IconIndex=-237 
[.ShellClassInfo] 
[email protected]%SystemRoot%\system32\shell32.dll,-21803 
[email protected]%SystemRoot%\system32\shell32.dll,-12689 
IconResource=%SystemRoot%\system32\imageres.dll,-3 
관련 문제