2017-08-06 40 views
1

我已經批量創建了一個程序,要求輸入密碼才能繼續執行程序的下一部分。我有一個我創建的包含密碼的文件。在程序中,我會調用該文本文件的內容並將其設置爲「密碼」變量...唯一的問題是,我收到錯誤消息:'The File is being Used在另一個過程'在另一個進程中使用的文件錯誤在批處理

我已經做了一些研究,並指出有時一些代碼行不是必需的,並導致發生此錯誤。難道這就是

這是我已經找到了一個錯誤我的代碼的一部分:

for /F "delims=" %%i in (Password.txt) do set content=%%i echo %content% set password123=%content% 
powershell -Command $pword = read-host "Enter password" -AsSecureString ;^$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ;^[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt 
set /p password=<.tmp.txt & del .tmp.txt 
if %password123%=%password% goto :correct 
if not %password123%=%password% goto :incorrect 

感謝您的時間提前!

回答

0

繞過創建包含密碼的臨時文件。解析powershell輸出例如如下:

for /F "delims=" %%G in (' 
powershell -NoProfile -Command "$pword = read-host 'Enter password' -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)" 
') do (
    set "password=%%G" 
) 

而且讀整個if /?;使用適當的比較運算符==。使用雙引號的密碼

if "%password123%"=="%password%" (goto :correct) else goto :incorrect 

要在密碼正確對待也可以雙引號(S)逃避可能cmd -poisonous字符,如|<>&,適用delayed expansion如下:

SETLOCAL EnableDelayedExpansion 
if "!password123!"=="!password!" ( 
    ENDLOCAL 
    goto :correct 
) else ( 
    ENDLOCAL 
    goto :incorrect 
) 
相關問題