用 JavaScript 迁移目录

  这两天用虚拟机安装系统,准备用虚拟机把开发环境和数据库分离,想法如下:

  1.开发环境为Host

  2.Guest 为数据库服务器,每一个服务器都是一个独立的虚拟机

  数据库包括,Oracle 9i、SQL Server 2005、MySQL

  但是装系统,还要设置环境变量,以减少系统盘的占用和增加性能,这就需要迁移系统盘的一些目录了,比如IE临时目录,临时文件夹,Applocation Data;此外,我们还需要把重要的文件夹移动到其他分区,以避免在系统盘发生事故或者想要恢复的时候,重要数据(MyDocument,收藏夹,程序配置等)不受影响。

  手动修改环境变量和注册表值太麻烦了,因为我每次装系统都要修改那么一次,这次实在受够了,心里一发狠。好,我写个脚本把你搞定!

  BAT是不能设置系统环境变量的,此外可用的就有 VBScript 和 JScript 了;VBScript 的好处是有对话框,JScript没有(alert等只能在网页中使用),而JScript 的代码条理清晰一些,并且功能强大,可以使用正则表达式等功能。

  于是乎,写了下面的脚本,各位大虾请看代码:

  

复制代码 代码如下:
//********************************************************************

  // Copymiddle 2006 Zealic,All middle keeped.

  //********************************************************************

  //** 环境变量名

  //** 设置环境变量名,这些值影响环境变量的名字,建议不要修改

  var VN_PATH        = "PATH";

  var VN_PROFILE        = "PROFILE";

  var VN_PROFILE_USER    = "PROFILE_USER";

  var VN_VOLATILE_PROFILE    = "VOLATILE_PROFILE";

  var VN_TEMP        = "TEMP";

  //********************************************************************

  //** 设置

  var m_Prefix        = "GUEST_";

  var m_UserName        = "Zealic";

  var m_Profile        = "D:\\Profile";

  var m_VoltProfile    = "F:\\VolatileProfile";

  var m_UserPath        = "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727;"

  + "D:\\Java\\JRE\\Currently\\bin";

  //调用函数以设置

  SetEnvironment(m_Prefix,m_UserName,m_Profile,m_VoltProfile,m_UserPath);

  //********************************************************************

  //** 函数定义

  // 设置环境变量

  //     prefix         : 环境变量名的前缀

  //     userName    : 用户名

  //     profile        : 重要文件目录

  //     voltProfile    : 非重要文件目录

  //     userPath    : 用户 Path,设置该值以进行快捷运行程序

  function SetEnvironment(prefix,userName,profile,voltProfile,userPath)

  {

  //开始设置

  var currentName;

  //===========================

  // 设置系统重要目录

  currentName = prefix + VN_PROFILE;

  SetSystemValue(currentName, profile);

  // 设置设置用户重要目录

  currentName = prefix + VN_PROFILE_USER;

  SetSystemValue(currentName, "%" + prefix + VN_PROFILE + "%\\" + userName);

  // 设置设置系统非重要目录

  currentName = prefix + VN_VOLATILE_PROFILE;

  SetSystemValue(currentName, voltProfile);

  // 设置设置用户非重要目录

  currentName = prefix + "VOLATILE_PROFILE_USER";

  SetSystemValue(currentName, "%" + prefix + VN_VOLATILE_PROFILE + "%" + "\\" + userName);

  // 设置临时目录

  currentName = prefix + "TEMP";

  SetSystemValue(currentName, "%" + prefix + VN_VOLATILE_PROFILE + "%" + "\\Temporary");

  //设置TEMP变量

  var temp = "%" + prefix + VN_TEMP + "%";

  SetUserValue("TMP", temp);

  SetUserValue("TEMP", temp);

  SetSystemValue("TMP", temp);

  SetSystemValue("TEMP", temp);

  // 设置 Path 和 自定义 Path 连接

  var currentName = prefix + VN_PATH;

  SetSystemValue(currentName, userPath);

  // 检测是否已经存在Path,如果不存在则设置

  var regValue = new RegExp("%" + prefix + VN_PATH + "%","i");

  if(!regValue.test(GetSystemValue("Path")))

  {

  SetSystemValue("Path",GetSystemValue("Path") + ";%" + prefix + VN_PATH + "%");

  }

  }

  // 调试用函数

  function Debug(msg)

  {

  wsh = new ActiveXObject("WScript.Shell");

  wsh.Popup(msg);

  }

  // 获取用户环境变量

  function GetUserValue(name,value)

  {

  wsh = new ActiveXObject("WScript.Shell");

  return wsh.Environment("user").Item(name);

  }

  // 设置用户环境变量

  function SetUserValue(name,value)

  {

  wsh = new ActiveXObject("WScript.Shell");

  wsh.Environment("user").Item(name) = value;

  }

  // 删除用户环境变量

  function RemoveUserValue(name)

  {

  wsh = new ActiveXObject("WScript.Shell");

  wsh.Environment("user").Remove(name) = value;

  }

  // 获取系统环境变量

  function GetSystemValue(name,value)

  {

  wsh = new ActiveXObject("WScript.Shell");

  return wsh.Environment("system").Item(name);

  }

  // 设置系统环境变量

  function SetSystemValue(name,value)

  {

  wsh = new ActiveXObject("WScript.Shell");

  wsh.Environment("system").Item(name) = value;

  }

  // 删除系统环境变量

  function RemoveSystemValue(name)

  {

  wsh = new ActiveXObject("WScript.Shell");

  wsh.Environment("system").Remove(name) = value;

  } 

  其中 Profile 为有价值的数据和文件 ,VolatileProfile 用过而无价值的数据和文件。这样,我们就可以清楚的知道,Profile 要保留,VolatileProfile 则可以经常清理。

  之后我还需要把 Desktop,My Document,Favorites文件夹迁移到 Profile 目录中,然后就开始安装数据库。

  不过嘛,今天太晚了,明天再说~~~

  明天用用 JScript 操作注册表以迁移系统的一些目录到 Profile 和 VolatileProfile 目录。

  http://www.cnblogs.com/zealic/archive/2006/11/07/552433.html