用 JavaScript 遷移目錄

來源:互聯網
上載者:User
javascript

這兩天用虛擬機器安裝系統,準備用虛擬機器把開發環境和資料庫分離,想法如下:
      1.開發環境為Host
      2.Guest 為資料庫伺服器,每一個伺服器都是一個獨立的虛擬機器
       資料庫包括,Oracle 9i、SQL Server 2005、MySQL

   但是裝系統,還要設定環境變數,以減少系統硬碟的佔用和增加效能,這就需要遷移系統硬碟的一些目錄了,比如IE臨時目錄,臨時檔案夾,Applocation Data;此外,我們還需要把重要的檔案夾移動到其他分區,以避免在系統硬碟發生事故或者想要恢複的時候,重要資料(MyDocument,收藏夾,程式配置等)不受影響。
   手動修改環境變數和註冊表值太麻煩了,因為我每次裝系統都要修改那麼一次,這次實在受夠了,心裡一發狠。好,我寫個指令碼把你搞定!
   BAT是不能設定系統內容變數的,此外可用的就有 VBScript 和 JScript 了;VBScript 的好處是有對話方塊,JScript沒有(alert等只能在網頁中使用),而JScript 的代碼條理清晰一些,並且功能強大,可以使用Regex等功能。
   於是乎,寫了下面的指令碼,各位大蝦請看代碼:

  1//********************************************************************
  2// Copymiddle 2006 Zealic,All middle keeped.
  3//********************************************************************
  4//** 環境變數名
  5//** 設定環境變數名,這些值影響環境變數的名字,建議不要修改
  6
  7var VN_PATH        = "PATH";
  8var VN_PROFILE        = "PROFILE";
  9var VN_PROFILE_USER    = "PROFILE_USER";
 10var VN_VOLATILE_PROFILE    = "VOLATILE_PROFILE";
 11var VN_TEMP        = "TEMP";
 12
 13
 14//********************************************************************
 15//** 設定
 16
 17var m_Prefix        = "GUEST_";
 18var m_UserName        = "Zealic";
 19var m_Profile        = "D:\\Profile";
 20var m_VoltProfile    = "F:\\VolatileProfile";
 21var m_UserPath        = "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727;"
 22                + "D:\\Java\\JRE\\Currently\\bin";
 23
 24//調用函數以設定
 25SetEnvironment(m_Prefix,m_UserName,m_Profile,m_VoltProfile,m_UserPath);
 26
 27
 28//********************************************************************
 29//** 函數定義
 30
 31// 設定環境變數
 32//     prefix         : 環境變數名的首碼
 33//     userName    : 使用者名稱
 34//     profile        : 重要檔案目錄
 35//     voltProfile    : 非重要檔案目錄
 36//     userPath    : 使用者 Path,設定該值以進行快捷運行程式
 37function SetEnvironment(prefix,userName,profile,voltProfile,userPath)
 38{
 39    //開始設定
 40    var currentName;
 41    //===========================
 42    // 設定系統重要目錄
 43    currentName = prefix + VN_PROFILE;
 44   
 45    SetSystemValue(currentName, profile);
 46
 47    // 設定設定使用者重要目錄
 48    currentName = prefix + VN_PROFILE_USER;
 49    SetSystemValue(currentName, "%" + prefix + VN_PROFILE + "%\\" + userName);
 50
 51    // 設定設定系統非重要目錄
 52    currentName = prefix + VN_VOLATILE_PROFILE;
 53    SetSystemValue(currentName, voltProfile);
 54
 55    // 設定設定使用者非重要目錄
 56    currentName = prefix + "VOLATILE_PROFILE_USER";
 57    SetSystemValue(currentName, "%" + prefix + VN_VOLATILE_PROFILE + "%" + "\\" + userName);
 58
 59    // 設定臨時目錄
 60    currentName = prefix + "TEMP";
 61    SetSystemValue(currentName, "%" + prefix + VN_VOLATILE_PROFILE + "%" + "\\Temporary");
 62
 63    //設定TEMP變數
 64    var temp = "%" + prefix + VN_TEMP + "%";
 65    SetUserValue("TMP", temp);
 66    SetUserValue("TEMP", temp);
 67    SetSystemValue("TMP", temp);
 68    SetSystemValue("TEMP", temp);
 69
 70    // 設定 Path 和 自訂 Path 串連
 71    var currentName = prefix + VN_PATH;
 72    SetSystemValue(currentName, userPath);
 73    // 檢測是否已經存在Path,如果不存在則設定
 74    var regValue = new RegExp("%" + prefix + VN_PATH + "%","i");
 75    if(!regValue.test(GetSystemValue("Path")))
 76    {
 77        SetSystemValue("Path",GetSystemValue("Path") + ";%" + prefix + VN_PATH + "%");
 78    }
 79}
 80
 81// 調試用函數
 82function Debug(msg)
 83{
 84    wsh = new ActiveXObject("WScript.Shell");
 85    wsh.Popup(msg);
 86}
 87
 88// 擷取使用者環境變數
 89function GetUserValue(name,value)
 90{
 91    wsh = new ActiveXObject("WScript.Shell");
 92    return wsh.Environment("user").Item(name);
 93}
 94// 設定使用者環境變數
 95function SetUserValue(name,value)
 96{
 97    wsh = new ActiveXObject("WScript.Shell");
 98    wsh.Environment("user").Item(name) = value;
 99}
100// 刪除使用者環境變數
101function RemoveUserValue(name)
102{
103    wsh = new ActiveXObject("WScript.Shell");
104    wsh.Environment("user").Remove(name) = value;
105}
106
107// 擷取系統內容變數
108function GetSystemValue(name,value)
109{
110    wsh = new ActiveXObject("WScript.Shell");
111    return wsh.Environment("system").Item(name);
112}
113// 設定系統內容變數
114function SetSystemValue(name,value)
115{
116    wsh = new ActiveXObject("WScript.Shell");
117    wsh.Environment("system").Item(name) = value;
118}
119// 刪除系統內容變數
120function RemoveSystemValue(name)
121{
122    wsh = new ActiveXObject("WScript.Shell");
123    wsh.Environment("system").Remove(name) = value;
124}
125
126
127
128

   其中 Profile 為有價值的資料和檔案 ,VolatileProfile 用過而無價值的資料和檔案。這樣,我們就可以清楚的知道,Profile 要保留,VolatileProfile 則可以經常清理。

   之後我還需要把 Desktop,My Document,Favorites檔案夾遷移到 Profile 目錄中,然後就開始安裝資料庫。
   不過嘛,今天太晚了,明天再說~~~
   明天用用 JScript 操作註冊表以遷移系統的一些目錄到 Profile 和 VolatileProfile 目錄。
http://www.cnblogs.com/zealic/archive/2006/11/07/552433.html



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.