3
我有一個便攜式開發工具,我想在其他PC上使用。我想設置一個文件關聯,以便點擊一個文件打開這個工具。然後,當我完成時,我想撤消或重置該PC上的文件關聯。臨時設置文件關聯
有沒有辦法做到這一點?可能來自批處理文件?
我有一個便攜式開發工具,我想在其他PC上使用。我想設置一個文件關聯,以便點擊一個文件打開這個工具。然後,當我完成時,我想撤消或重置該PC上的文件關聯。臨時設置文件關聯
有沒有辦法做到這一點?可能來自批處理文件?
那麼,你可以使用ftype
和assoc
命令來創建或刪除文件類型關聯:
ASSOC .foo=FooFile
FTYPE FooFile=X:\Foo\foo.exe %1 %*
您可以
FTYPE FooFile=
ASSOC .foo=
編輯後刪除它們:我有現在嘗試一些可以讓您將關聯重新設置回默認值的東西。我把它放在my Subversion repo。在現階段它會生成兩個新的批處理文件:set.cmd
和reset.cmd
;其中一個設置一個新的關聯,另一個反轉它。將set.cmd
滾入實際的批次應該不會太困難,但它會使測試成爲地獄,所以我會把它作爲練習。
代碼如下,它應該有足夠的評論,希望。
@echo off
setlocal enableextensions enabledelayedexpansion
rem Debug flag. Generates a little more output; un-set if undesired
set DEBUG=1
rem Parse arguments and help
if [%1]==[] goto Usage
if [%2]==[] goto Usage
rem Find out whether the association is taken
for /f "usebackq tokens=2* delims==" %%x in (`assoc .%1 2^>nul`) do set assoc_old=%%x
if defined DEBUG (
if defined assoc_old (echo Association already defined: [%assoc_old%]) else (echo Association not yet taken)
)
rem Find a new, unused association
rem Note that we assume that we find one, eventually. This isn't guaranteed, but we'll ignore that for the moment
rem Otherwise this loop might run forever
:loop
set assoc_new=My.%1.%RANDOM%
if defined DEBUG echo Trying new association (%assoc_new%)
assoc .%1 >nul 2>nul
if errorlevel 1 (
set assoc_new=
if defined DEBUG echo Didn't work out
) else (
if defined DEBUG echo Found one! \o/
)
if not defined assoc_new goto loop
if defined DEBUG echo Writing reset batch file
echo @echo off>reset.cmd
echo assoc .%1=%assoc_old%>>reset.cmd
echo ftype %assoc_new%=>>reset.cmd
if defined DEBUG echo Writing setting batch file
echo @echo off>set.cmd
echo assoc .%1=%assoc_new%>>set.cmd
echo ftype %assoc_new%=%2 %%1>>set.cmd
goto :eof
:Usage
echo.Usage
echo. %~nx0 type command
echo.
echo. type is the file type to override, such as docx or txt.
echo. No dot before it is necessary.
echo. command is the command to perform on that file.
echo. %%1 is automatically appended at the end.
echo. If the command includes spaces, surround it with quotes.
echo.
echo.Example
echo. %~nx0 txt notepad
exit /b 1
這確實做的關聯,但並沒有解決重置協會回以前的應用程序的問題,但我打勾你反正是你的解決方案運作良好,對我來說足夠。 – soupagain 2010-01-31 13:10:19
@soup:啊,對不起。我沒有看過你的問題,就好像有一個以前的應用程序關聯。可能首先查詢以前的關聯並動態地寫入另一個批處理文件以恢復它。我會試試它是否夠簡單;只要堅持:-) – Joey 2010-01-31 13:12:05
@soup:編輯完畢;有一個批處理文件來測試。看起來,這不作爲非管理員用戶,但;可悲的是,這大大降低了它的價值。 – Joey 2010-01-31 17:42:03