我一直在尋找一個腳本,可以讓我在當前系統變量路徑的末尾添加文本。 我有什麼建議可以做到嗎? (spesificly,加入 「; C:\ Program Files文件(x86)的\的Java \ jre7 \ BIN \ java.exe的」 到現有文本)使用批處理文件添加一行到系統變量路徑
回答
echo Set objShell = CreateObject("WScript.Shell") > %TEMP%\sp.vbs
echo Set objSystemEnv = objShell.Environment("SYSTEM") >> %TEMP%\sp.vbs
echo objSystemEnv("Path") = objSystemEnv("Path") ^& ";C:\Program Files (x86)\Java\jre7\bin" >> %TEMP%\sp.vbs
cscript.exe /nologo %TEMP%\sp.vbs
del %TEMP%\sp.vbs
這CMD必須運行升高,否則的Cscript.exe將只寫「權限被拒絕」並且什麼都不會發生。
這不是一個批處理文件。這是一個批處理文件,創建一個腳本文件然後啓動它。雖然它可以工作,但它實際上不是對問題的回答,而是使用批處理文件編寫c#源文件,運行編譯器,然後啓動可執行文件。 –
@KenWhite,它是一個批處理文件,它完成了工作。 C#源代碼不會:1.從批處理文件中執行起來要困難得多2.在Vista之前,通常沒有csc.exe。 – Soonts
你錯過了我的觀點。這個問題是特別標記爲'windows批處理',並且你的答案不使用Windows批處理文件來修改'PATH'。它使用批處理文件來編寫和執行不同類型的腳本,就像我上面所說的那樣。這不是所問問題的答案。你說過「使用VBScript,但增加了從批處理文件而不是記事本中編寫腳本文件的開銷。」 (我沒有downvote你的答案,順便說一句,只是指出它沒有解決問題的問題。) –
如果你想你可以在Windows 7中使用SETX命令最簡單的辦法:
SETX PATH "%PATH%;C:\New Added Folder"
PS1:如果你想SETX命令執行的HKLM關鍵的變化使用-M參數。
PS2:更改是永久的,但您需要重新啓動用戶會話或控制檯。
如果你想要一個更專業的小東西,那麼你可以使用我的工具PATHS在Ruby中寫道,我爲Windows製作了一個可執行文件。
PATHS.EXE
http://exoshare.com/download.php?uid=1TKIOMR6
選項:
/? (或)-help |顯示此信息。
-l(或)-list |列出條目。
-c(或)-clean |清除路徑中的重複項和無效目錄。
-r(或)-reset |將路徑重置爲Windows默認值。
-a(或)-add |添加一個條目。
-d(or)-del |刪除一個條目。
-add -current |強制將條目添加到當前用戶路徑中。
-add -local |強制將條目添加到本地機器路徑中。
實例:
PATHS -l [+]索引的所有條目。
PATHS -a「C:\ Folder」 [+]將條目添加到本地路徑中。
PATHS -a當前「C:\ Folder」 [+]將條目添加到當前用戶路徑中。
PATHS -d「3」 [+]刪除索引列表的第3個條目。
PATHS -d「C:\ Folder」 [+]刪除條目。
PATHS.RB
require 'win32/registry'
require 'rainbow'
# PATHS v0.1
#
# By Elektro [email protected]
# Description:
# -----------
# This is a tool to manage the windows PATH enviroment.
exit if Object.const_defined?(:Ocra)
def logo()
print "
PATHS v0.1
By Elektro [email protected]
".foreground(:white)
end
def help()
print '
Options:
/? (or) -help | Show this info.
-l (or) -list | List the entries.
-c (or) -clean | Clean duplicates and invalid directories in the paths.
-r (or) -reset | Reset the paths to the Windows defaults.
-a (or) -add | Add a entry.
-d (or) -del | Delete a entry.
-add -current | Force adding a entry into the current user path.
-add -local | Force adding a entry into the local machine path.
Examples:
PATHS -l
[+] Indexes all the entries.
PATHS -a "C:\Folder"
[+] Adds a entry into the local path.
PATHS -a current "C:\Folder"
[+] Adds a entry into the current user path.
PATHS -d "3"
[+] Deletes the 3rd entry of the indexed list.
PATHS -d "C:\Folder"
[+] Deletes a entry.
'
Process.exit
end
def error(kind)
print "[+] ERROR"
if kind == "pos" then print "\n Index #{ARGV[1]} is out of range, only #{$pos} entries.\n" end
if kind == "notfound" then print "\n Directory \"#{ARGV[1]}\" not found on PATH.\n" end
Process.exit
end
def args()
if ARGV.empty? then get_paths("visible") end
if ARGV[0] == "/?" or ARGV[0] =~ /^-help$/i then help() end
if ARGV[0] =~ /^-l$/i or ARGV[0] =~ /^-list$/i then get_paths("visible") end
if ARGV[0] =~ /^-c$/i or ARGV[0] =~ /^-clean$/i then clean_path() end
if ARGV[0] =~ /^-d$/i or ARGV[0] =~ /^-del$/i then del_path() end
if ARGV[0] =~ /^-a$/i or ARGV[0] =~ /^-add$/i then add_path() end
if ARGV[0] =~ /^-r$/i or ARGV[0] =~ /^-reset$/i then reset_path() end
end
def get_paths(visibility)
$pos = 0
# HKCU path
if not visibility == "hidden" then puts "\n [+] Current User PATH:\n\n" end
Win32::Registry::HKEY_CURRENT_USER.open('Environment') do |reg|
for dir in reg['Path', Win32::Registry::REG_SZ].split(";").sort do
$pos = $pos+1
dir = dir.gsub(/^PATH=/, "")
instance_variable_set "@_#{$pos}", dir + "?CURRENT_USER"
if not File.directory? dir then invalid = "(Directory doesn't exist)".foreground(:red).bright else invalid ="" end
if not visibility == "hidden"
if $pos < 10 then puts " #{$pos.to_s} = #{dir} #{invalid}" else puts " #{$pos.to_s}= #{dir} #{invalid}"end
end
end
end
# HKLM path
if not visibility == "hidden" then puts "\n\n [+] Local Machine PATH:\n\n" end
Win32::Registry::HKEY_LOCAL_MACHINE.open('SYSTEM\CurrentControlSet\Control\Session Manager\Environment') do |reg|
for dir in reg['Path', Win32::Registry::REG_SZ].split(";").sort do
$pos = $pos+1
dir = dir.gsub(/^PATH=/, "")
instance_variable_set "@_#{$pos}", dir + "?LOCAL_MACHINE"
if not File.directory? dir then invalid = "(Directory doesn't exist)".foreground(:red).bright else invalid ="" end
if not visibility == "hidden"
if $pos < 10 then puts " #{$pos.to_s} = #{dir} #{invalid}" else puts " #{$pos.to_s}= #{dir} #{invalid}"end
end
end
end
if not visibility == "hidden" then Process.exit end
$max_pos = $pos
end
def add_path()
if ARGV[1] =~ /^-current$/ then key = "current" else key = "local" end
# HKCU path
if key == "current"
Win32::Registry::HKEY_CURRENT_USER.open('Environment', Win32::Registry::KEY_ALL_ACCESS) do |reg|
value = reg['Path']
reg.write('Path', Win32::Registry::REG_SZ, "#{value};#{ARGV.last}")
puts "[+] Entry added in User PATH: #{ARGV.last}"
end
end
# HKLM path
if key == "local"
Win32::Registry::HKEY_LOCAL_MACHINE.open('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', Win32::Registry::KEY_ALL_ACCESS) do |reg|
value = reg['Path']
reg.write('Path', Win32::Registry::REG_SZ, "#{value};#{ARGV.last}")
puts "[+] Entry added in Local PATH: #{ARGV.last}"
end
end
end
def del_path()
get_paths("hidden")
final_path = ""
found = 0
notfound = 0
if ARGV[1] =~ /^[1-9]+$/
choose = instance_variable_get "@_#{ARGV[1]}"
if ARGV[1].to_i > $max_pos.to_i then error("pos") end
# HKCU PATH index deletion
if choose["?CURRENT_USER"]
Win32::Registry::HKEY_CURRENT_USER.open('Environment', Win32::Registry::KEY_ALL_ACCESS) do |reg|
value = reg['Path']
for dir in reg['Path', Win32::Registry::REG_SZ].split(";").sort do
if not dir == choose.split("?").first then final_path << ";" + dir end
end
reg.write('Path', Win32::Registry::REG_SZ, final_path[1..-1])
end
puts "[+] Entry deleted in User PATH: #{choose.split("?").first}"
end
# HKLM PATH index deletion
if choose["?LOCAL_MACHINE"]
Win32::Registry::HKEY_LOCAL_MACHINE.open('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', Win32::Registry::KEY_ALL_ACCESS) do |reg|
value = reg['Path']
for dir in reg['Path', Win32::Registry::REG_SZ].split(";").sort do
if not dir == choose.split("?").first then final_path << ";" + dir end
end
reg.write('Path', Win32::Registry::REG_SZ, final_path[1..-1])
end
puts "[+] Entry deleted in Local PATH: #{choose.split("?").first}"
end
elsif
# HKCU PATH str deletion
Win32::Registry::HKEY_CURRENT_USER.open('Environment', Win32::Registry::KEY_ALL_ACCESS) do |reg|
value = reg['Path']
for dir in reg['Path', Win32::Registry::REG_SZ].split(";").sort do
if not dir =~ /^#{Regexp.escape(ARGV[1])}$/i then final_path << ";" + dir else found = "yes" end
end
reg.write('Path', Win32::Registry::REG_SZ, final_path[1..-1])
if found == "yes" then puts "[+] Entry deleted in User PATH: #{ARGV[1]}" else notfound = 1 end
end
# HKLM PATH str deletion
final_path = ""
found = ""
Win32::Registry::HKEY_LOCAL_MACHINE.open('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', Win32::Registry::KEY_ALL_ACCESS) do |reg|
value = reg['Path']
for dir in reg['Path', Win32::Registry::REG_SZ].split(";").sort do
if not dir =~ /^#{Regexp.escape(ARGV[1])}$/i then final_path << ";" + dir else found = "yes" end
end
reg.write('Path', Win32::Registry::REG_SZ, final_path[1..-1])
if found == "yes" then puts "[+] Entry deleted in Local PATH: #{ARGV[1]}" else notfound = notfound+1 end
if notfound == 2 then error("notfound") end
end
end
end
def reset_path()
Win32::Registry::HKEY_CURRENT_USER.open('Environment', Win32::Registry::KEY_ALL_ACCESS) do |reg| reg.write('Path', Win32::Registry::REG_SZ, 'C:\Windows;C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\syswow64') end
Win32::Registry::HKEY_LOCAL_MACHINE.open('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', Win32::Registry::KEY_ALL_ACCESS) do |reg| reg.write('Path', Win32::Registry::REG_SZ, 'C:\Windows;C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\syswow64') end
puts "[+] PATH restored to Windows defaults."
end
def clean_path()
puts "\n[+] Searching invalid or duplicated entries in the PATH...\n\n"
# HKCU PATH
final_path = ""
Win32::Registry::HKEY_CURRENT_USER.open('Environment', Win32::Registry::KEY_ALL_ACCESS) do |reg|
value = reg['Path']
for dir in reg['Path', Win32::Registry::REG_SZ].split(";").sort do
if File.directory? dir and not final_path[/#{Regexp.escape(dir)}$/i] then final_path << ";" + dir else puts "[+] Entry deleted in User PATH: #{dir}" end
end
reg.write('Path', Win32::Registry::REG_SZ, final_path[1..-1])
end
# HKLM PATH
final_path = ""
Win32::Registry::HKEY_LOCAL_MACHINE.open('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', Win32::Registry::KEY_ALL_ACCESS) do |reg|
value = reg['Path']
for dir in reg['Path', Win32::Registry::REG_SZ].split(";").sort do
if File.directory? dir and not final_path[/#{Regexp.escape(dir)}$/i] then final_path << ";" + dir else puts "[+] Entry deleted in Local PATH: #{dir}" end
end
reg.write('Path', Win32::Registry::REG_SZ, final_path[1..-1])
end
puts "\n[+] PATH is cleaned.\n\n"
end
logo()
args()
Process.exit
- 1. 從批處理文件追加到系統變量路徑
- 2. 批處理文件 - 用戶輸入設定的系統變量路徑
- 3. 未找到eclipse路徑變量中的批處理文件
- 4. 批處理 - 將文本文件中的路徑(文本)加載到變量中
- 5. 將變量添加到文件路徑
- 6. 變量For用於批處理文件中的路徑
- 7. 路徑在一個批處理文件
- 8. 調用批處理文件報告:「系統找不到指定的路徑」
- 9. 批處理文件使用系統變量從文本文件中讀取
- 10. 添加到批處理中的變量?
- 11. 在批處理文件中的文件路徑中使用變量
- 12. 在批處理文件中使用變量作爲文件路徑
- 13. 通過命令行添加系統路徑環境變量?
- 14. 在Windows下自動添加Python路徑到PATH系統變量
- 15. 設置環境變量路徑通過批處理文件
- 16. Windows CMD - 從批處理文件重置路徑變量?
- 17. 具有變量目錄路徑的批處理文件
- 18. 從Windows批處理文件設置系統環境變量?
- 19. 通過批處理文件爲系統設置環境變量
- 20. 如何將變量添加到批處理文件?
- 21. 將java變量添加到批處理文件
- 22. 批處理文件變量
- 23. 批處理文件變量
- 24. 使用路徑中的空格執行批處理文件
- 25. 使用Java在遠程系統上執行批處理文件
- 26. 使用變量作爲文件路徑將ImageIcon添加到JPanel
- 27. 添加變量文件路徑在Java
- 28. 批處理文件登錄系統
- 29. Windows批處理文件系統備份
- 30. 批處理文件 - VBScript運行時錯誤:找不到路徑
你可以這樣做,但只會持續到您的批處理文件完成。如果你想讓它成爲一個永久的變化,它將無法工作。 –