用于比较文本文件和文件夹内容的批处理脚本

时间:2014-01-04 02:04:48

标签: batch-file

我有这个批次来检查导出安装的程序列表及其卸载程序:

@ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
MKDIR "%userprofile%\desktop\CloudUninstall"
CD "%userprofile%\desktop\CloudUninstall"
regedit /e regexport.txt "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall"
find "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\" regexport.txt >> reg1.txt
set /a var=2
GOTO LOOPD

:LOOPD
FOR /F "skip=%var% tokens=* delims==" %%A in (reg1.txt) do set trans=%%A & GOTO WALKIT
PAUSE

:WALKIT
REM This transits the variable from the for /f loop into the current function
set current=%trans%
REM This then takes the REGEDIT string formatting and reformats it to standard text for new function
regedit /e %var%.txt "%current:~1,-2%"
find "DisplayName" %var%.txt >> %var%_a.txt
find "UninstallString" %var%.txt >> %var%_a.txt
set /a var=var+1
GOTO LOOPD

我需要的是一种将文本文件与程序名称与这组文件进行比较的方法。然后,如果文本文件中的名称出现在该文件列表中,请将卸载程序导出到单独的文本文件中。

1 个答案:

答案 0 :(得分:0)

@echo off

    rem Configure environment
    setlocal enableextensions disabledelayedexpansion

    rem Configure our "blacklist" file
    set "blackList=%temp%\blacklist.txt"

    rem To test, generate the blacklist file
    (
        echo Adobe Flash Player
        echo Dell Touchpad
        echo Nokia Suite
    ) > "%blacklist%" 

    echo.
    type "%blacklist%"
    echo.

    rem From where to retrieve the information
    set "key=HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall"

    rem Variables used for temporary storage of values
    set "displayName="
    set "uninstallString="

    rem Query the registry for the desired values and store values in variables. Each time a Uninstall key is found
    rem output data. The complete list is being sent to a temporary file

    (
        for /f "tokens=1,2,*" %%a in ('reg query "%key%" /s ^| findstr /i /l /c:"%key%" /c:"DisplayName" /c:"UninstallString"') do (
            if /i "%%a"=="DisplayName" (
                set "displayName=%%c"
            ) else if /i "%%a"=="UninstallString" (
                set "uninstallString=%%c"
            ) else (
                if defined displayName if defined uninstallString (
                    setlocal enabledelayedexpansion
                    if not "!uninstallString:~0,1!!uninstallString:~0,1!"=="""" (
                        set "uninstallString="!uninstallString!"
                        set "uninstallString=!uninstallString:.exe=.exe" !" 
                    )
                    echo(!displayName!^|!uninstallString!
                    endlocal
                )
                set "displayName="
                set "uninstallString="
            )
        )
        if defined displayName if defined uninstallString (
            setlocal enabledelayedexpansion
            echo(!displayName!=!uninstallString!
            endlocal
        )
    ) > "%temp%\ProgramList"

    rem Compare the generated program list against black list
    echo.
    type "%temp%\ProgramList" | findstr /i /r /g:"%blacklist%"
    echo.

    rem Extract uninstallstring from list to another file
    (for /f "tokens=1,* delims=^|" %%u in (
        'findstr /i /r /g:"%blacklist%" ^< "%temp%\ProgramList"'
    ) do (
        set "c=%%v"

        echo(%%v
    ))>"%temp%\UninstallList"

    echo.
    type "%temp%\UninstallList"
    echo.

    endlocal
    exit /b
相关问题