2012-12-12 25 views
5

Tôi đang cố gắng tạo tệp lô để đọc tệp khóa .ldb của Microsoft Access. Tệp khóa chứa danh sách tên máy tính và tên người dùng. Tôi muốn trích xuất các tên máy tính và cuối cùng chạy chúng với một lệnh bên ngoài.Cần trợ giúp viết tệp lô để đọc tệp .ldb khóa MS có dấu phân cách bằng không

Định dạng của tập tin thực thi là một hàng duy nhất với (1) một tên máy tính (2) một nhân vật NULL (Hex 00) (3) khoảng 20 không gian (4) tên người dùng (5) ký tự NULL (6) khoảng 20 dấu cách lặp lại.

Ví dụ trong Notepad ++ với (NUL) đại diện Hex 00:

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

Tôi đã thử một số phương pháp sử dụng FOR để đọc các tập tin nhưng không thể vượt qua các tên máy tính đầu tiên.

 
setlocal EnableDelayedExpansion 
set file=database.ldb 

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

Đối với hầu hết cơ sở dữ liệu Access của tôi, tên người dùng trong tệp là Admin. Tôi đã có thể sử dụng FIND để cho tôi biết số lần xuất hiện của "Quản trị" nằm trong tệp (cộng 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!)  
    ) 
) 

Lặp lại qua các dòng tìm thấy không hoạt động, có thể vì chỉ có một dòng trong tệp (không trả về vận chuyển).

Tôi muốn tìm một giải pháp có thể hoạt động với cài đặt chuẩn của Windows XP.


Sau khi nhận được câu trả lời được chấp nhận, tôi kết hợp vào tệp hàng loạt mà tôi đăng bên dưới. Tôi đã đặt tên tệp là ShowUsersInLDB.bat và đặt nó trong thư mục Gửi Tới của tôi.

@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 

Trả lời

4

CMD.EXE thường không phát độc đáo với byte NUL. Nhưng có một vài lệnh bên ngoài có thể xử lý các byte NUL.

Bạn cũng phải lo lắng về độ dài của "dòng". CMD.EXE không thích các dòng dài hơn 8191 byte.

Tôi nghĩ đặt cược tốt nhất của bạn là THÊM vì nó chuyển đổi NUL thành các dòng mới.

Sau đây phải lặp lại tên máy tính của bạn.

@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

Tôi cũng đang gặp sự cố khi xử lý các ký tự đại diện. Sử dụng nhiều hơn đã giúp. Cảm ơn vì tiền hỗ trợ! –

Các vấn đề liên quan