2012-12-12 58 views
5

我想創建一個批處理文件來讀取Microsoft Access .ldb鎖定文件。鎖定文件包含計算機名稱和用戶名稱的列表。我想提取計算機名稱並最終針對外部命令運行它們。需要幫助編寫一個批處理文件來讀取MS Access .ldb鎖定文件與NULL分隔符

批處理文件的格式是一個單行 (1)計算機名 (2)一個NULL字符(十六進制00) (3)大約20位 (4)的用戶名 (5 )NULL字符 (6)重複大約20個空格 。在記事本中

實例++與代表十六進制00 (NUL)

 
COMPUTER0123(NUL)      Admin(NUL)      COMPUTER0507(NUL)      Admin(NUL) 

我試着用FOR讀取文件的幾種方法,但不能讓過去的第一個計算機名稱。

 
setlocal EnableDelayedExpansion 
set file=database.ldb 

for /F %%a in ('type %file%') do (
    echo %%a 
    ) 

對於大多數我的Access數據庫,文件中的用戶名是Admin。我已經可以使用FIND來告訴我在文件中加了多少「Admin」(加1)。

for /f "delims=" %%n in ('find /c /v "Admin" %file%') do set "len=%%n" 
set "len=!len:*:=!" 
echo %len% (minus 1) computer names to process 
<%file% (
    for /l %%l in (1 1 !len!) do ( 
    set "line=" 
    set /p "line=" 
    echo(!line!)  
    ) 
) 

通過發現線迭代不起作用,可能是因爲有僅是文件(沒有回車)在一行。

我想找到一個解決方案,可以使用Windows XP的標準安裝。


接收公認的答案後,我合併到這一個批處理文件,我下面張貼。我將該文件命名爲ShowUsersInLDB.bat,並將其放在我的SendTo文件夾中。

@echo off 
::=================================================================== 
:: Put this in your SendTo folder and it will let you right-click 
:: on an Access .ldb/.laccdb lock file and tell you the computer 
:: names that have opened the database. 
:: 
:: After the computer names are shown, this will prompt you to 
:: search for the user names associated with each computer. This 
:: depends upon finding a 3rd party file named NetUsers.exe in 
:: the user profile folder. Feel free to change the path if you 
:: want to store the file in another location. 
:: 
:: NetUsers.exe can be downloaded from here: http://www.optimumx.com/downloads.html#NetUsers 
:: 
:: Notes: 
:: 1) Keep in mind that sometimes after people leave the database 
:: the lock file still shows their computer name. Don't jump 
:: to conclusions. 
:: 2) NetUsers.exe seems to report all users who have logged on 
:: to the computer and not logged off, including services. 
:: If you aren't familiar with your user names or your users are 
:: sharing remote desktops/Citrix/Terminal Services, you may have 
:: to guess who might have created the lock entry. 
:: 
:: Installation: 
:: You may find a batch file named Install_UsersInLDB.bat that will 
:: copy this file to the SendTo folder and the NetUsers.exe file to 
:: the user profile (or a place you define). 
:: 
:: Ben Sacherich - March 2014 
:: Please let me know if you have any ideas for improvements. 
::=================================================================== 

setlocal 
set file="%1" 

:: Make sure the file has a compatible extension. 
if "%~x1"==".ldb" goto :ExtensionIsValid 
if "%~x1"==".laccdb" goto :ExtensionIsValid 

echo. 
echo "%~n1%~x1" is not the correct file type. 
echo. 
pause 
goto :End 

:ExtensionIsValid 
echo The Access "%~n1%~x1" file contains 
echo the following computer names: 
echo. 
set "compNameLine=1" 
for /f %%A in ('more "%file%"') do (
    if defined compNameLine (
    echo %%A 
    set "compNameLine=" 
) else set "compNameLine=1" 
) 

echo. 
echo Are you ready to look up the user names on each computer? 
pause 

set "compNameLine=1" 
for /f %%A in ('more "%file%"') do (
    if defined compNameLine (
    ::echo %%A 
    "%userprofile%\netusers" \\%%A 
    set "compNameLine=" 
) else set "compNameLine=1" 
) 

echo. 
echo -- Validation finished at %time% 
pause 
:End 
exit 

回答

4

CMD.EXE通常不能很好地播放NUL字節。但是有幾個外部命令可以處理NUL字節。

您還必須擔心「行」的長度。 CMD.EXE不喜歡長度超過8191字節的行。

我認爲你最好的選擇是更多,因爲它將NUL轉換爲新的行。

以下內容應回顯您的計算機名稱。

@echo off 
setlocal 
set "file=database.ldb" 
set "compNameLine=1" 
for /f %%A in ('more "%file%"') do (
    if defined compNameLine (
    echo %%A 
    set "compNameLine=" 
) else set "compNameLine=1" 
) 
+0

我還面臨一個處理nul字符的問題。使用更多幫助。謝謝你的提示! –