2012-12-12 2 views
5

Microsoft Accessの.ldbロックファイルを読み取るためのバッチファイルを作成しようとしています。ロックファイルには、コンピュータ名とユーザー名の一覧が含まれています。私はコンピュータ名を抽出し、最終的に外部コマンドに対してそれらを実行したい。null区切り文字を使用してMS Access .ldbロックファイルを読み込むためのバッチファイルの作成に役立つ必要があります

バッチファイルの形式は と単一の行である(1)コンピュータ名 (2)NULL文字(16進数00) (3)約20スペース (4)ユーザ名 (5 )NULL文字 (6)約20スペース を繰り返す。六角00を表す(NUL)とメモ帳で

例++:

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

私は、ファイルを読むためにFORを使用して、いくつかの方法を試してみたが、最初のコンピュータ名を乗り越えることができません。私のAccessデータベースのほとんどのために

 
setlocal EnableDelayedExpansion 
set file=database.ldb 

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

、ファイル内のユーザー名は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!)  
    ) 
) 

ファイル内に1行しかないため(キャリッジリターンなし)、おそらく見つかった行を反復処理できません。

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を新しい行に変換するので、あなたの最善の策はMOREだと思います。

以下は、コンピュータ名をエコーし​​ます。

@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

ナル文字も問題に直面していました。より多くの助けを借りて。ヒントをありがとう! –

関連する問題