2012-12-12 17 views
5

Próbuję utworzyć plik wsadowy do odczytu pliku blokady programu Microsoft Access .ldb. Plik blokady zawiera listę nazw komputerów i nazw użytkowników. Chcę wyodrębnić nazwy komputerów i ostatecznie uruchomić je na polecenie zewnętrzne.Potrzebujesz pomocy w pisaniu pliku wsadowego do odczytu pliku MS Access .ldb lock z ogranicznikami pustymi

Format pliku wsadowym jest jednorzędowe z (1) nazwa komputera (2) charakter puste (Hex 00) (3) około 20 obowiązuje (4), nazwa użytkownika (5) znak NULL (6) około 20 spacji powtórzenie.

przykład w Notepad ++ z (NUL) reprezentujących Hex 00:

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

Próbowałem kilka metod z wykorzystaniem FOR do odczytu pliku, ale nie można ominąć pierwszej nazwy komputera.

 
setlocal EnableDelayedExpansion 
set file=database.ldb 

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

Dla dla większości moich baz danych Access, nazwa użytkownika w pliku jest Admin. Udało mi się użyć numeru FIND, aby poinformować mnie o liczbie wystąpień "Admin" w pliku (plus 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!)  
    ) 
) 

Iterowanie przez znalezione linie nie działa, prawdopodobnie dlatego, że w pliku znajduje się tylko jeden wiersz (nie powraca karetka).

chciałbym znaleźć rozwiązanie, które będzie działać w standardowej instalacji systemu Windows XP.


Po otrzymaniu zaakceptowanej odpowiedzi połączyłem ją w plik wsadowy, który zamieszczam poniżej. Nazwałem plik ShowUsersInLDB.bat i umieściłem go w folderze 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 

Odpowiedz

4

CMD.EXE generalnie nie gra ładnie z bajtami NUL. Ale istnieje kilka zewnętrznych poleceń, które poradzą sobie z bajtami NUL.

Trzeba też martwić o długości „linii”. CMD.EXE nie lubi linii dłuższych niż 8191 bajtów.

myślę najlepiej jest bardziej ponieważ przekształca nuls na nowe linie.

Następujące elementy powinny wyświetlać nazwy komputerów.

@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

Wystąpił również problem z obsługą znaków nul. Użycie więcej pomogło. Dzięki za wskazówkę! –

Powiązane problemy