2012-12-12 12 views
5

Sto cercando di creare un file batch per leggere un file di blocco .ldb di Microsoft Access. Il file di blocco contiene un elenco di nomi di computer e nomi utente. Voglio estrarre i nomi dei computer e infine eseguirli con un comando esterno.Serve aiuto per scrivere un file batch per leggere un file di blocco .ldb con MS Access con delimitatori null.

Il formato del file batch è una singola riga con (1) un nome computer (2) un carattere NULL (Hex 00) (3) circa 20 spazi (4) il nome utente (5) un carattere NULL (6) circa 20 spazi ripetuto.

Esempio in Notepad ++ con (NUL) rappresenta Hex 00:

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

Ho provato diversi metodi che utilizzano FOR per leggere il file, ma non può andare oltre il primo nome del computer.

 
setlocal EnableDelayedExpansion 
set file=database.ldb 

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

Per la maggior parte dei miei database di Access, il nome utente nel file è Admin. Sono stato in grado di usare FIND per dirmi quante occorrenze di "Admin" ci sono nel file (più 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!)  
    ) 
) 

Iterazione attraverso le linee trovati non funziona, probabilmente perché c'è solo una riga nel file (senza ritorni a capo).

Mi piacerebbe trovare una soluzione che funzioni con un'installazione standard di Windows XP.


Dopo aver ricevuto una risposta accettata, l'ho combinata in un file batch che sto postando di seguito. Ho chiamato il file ShowUsersInLDB.bat e l'ho inserito nella mia cartella 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 

risposta

4

CMD.EXE generalmente non funziona correttamente con i byte NUL. Ma ci sono alcuni comandi esterni che possono gestire i byte NUL.

Devi anche preoccuparti della lunghezza della "linea". CMD.EXE non ama le righe più lunghe di 8191 byte.

Penso che la tua migliore scommessa sia PIÙ poiché converte i NUL in nuove linee.

Quanto segue dovrebbe fare eco ai nomi dei computer.

@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

Ero di fronte a un problema di gestione di caratteri nul troppo. Usando più aiutato. Grazie per il consiglio! –

Problemi correlati