【问题标题】:BATCH/CMD: Print each line of a .txt into a variableBATCH/CMD:将 .txt 的每一行打印到一个变量中
【发布时间】:2012-07-03 10:09:57
【问题描述】:

目标:将计算机中的每个 dll 文件都传递到 regsvr32.exe

已完成: 光盘\
::REM 将每个文件和目录 (/s) 导出到 c: 根目录中的一个文件中,名为 dir.txt,只有文件名 (/b)
目录 /s /b>>dir.txt
::REM 现在,这将包含所有扩展类型。我们只需要 dll,所以我们在 dll.txt 中找到它:
findstr ".dll$" dir.txt>>dll.txt

扭结:

现在,如果我想 regsvr32.exe “归档”现在在 dll.txt 中的每个文件,我需要以某种方式取出每行上单独的每个文件名。我想知道是否有第三方命令行工具可以将文件的每一行导出到变量中。这样,我可以:

===========

::REM 假设此工具有开关 /l 用于行号,/v:"" 用于使用变量,并在末尾使用“文件”:

set line=1  
:loop  
set dll=  
tool.exe /l %line% /v:"dll" "dll.txt"  
::REM We if defined here because if the line doesn't exist in dll.txt, the tool would return nothing to %dll%
if not defined %dll% exit  
::REM With the variable defined, we can continue  
regsvr32.exe %dll%  
set /a line=%line%+1  
goto loop  

========================

然后该工具将处理文件每一行的每个路径,直到它自动退出,因为不会有更多行。请注意,在循环之后我将 dll 设置为空,以便“如果未定义”每次都可以工作。

如果这种类型的第三方工具做不到,有没有办法用for?? 老实说,我从来没有学习过,也尝试过,但永远无法弄清楚。

非常感谢任何帮助!

对不起,如果这已经得到答复。

编辑/更新: 我已经发现了我将如何完成这项工作。

感谢:http://pyrocam.com/re-register-all-dlls-to-fix-no-such-interface-supported-error-in-windows-7-after-installing-ie7-standalone/
和:Read a txt line by line in a batch file

第一个链接显示手动将开头替换为 regsvr32.exe
第二个展示了在这种情况下如何使用 for {也感谢 craig65535 的帮助:)}

代码:

@echo off
color 1f
title Register .dll
echo.
echo Exporting file list . . .
echo.
cd /d c:
cd\
if exist dll.txt del dll.txt
if exist dir.txt del dir.txt
if exist dll.bat del dll.bat
echo Part 1 of 3 . . .
echo.
dir /s /b>>dir.txt
echo Part 2 of 3 . . .
echo.
findstr ".dll$" dir.txt>>dll.txt
del dir.txt
echo Part 3 of 3 . . .
echo.
for /f "delims=" %%i IN ('type dll.txt') do echo regsvr32.exe /s "%%i">>dll.bat
del dll.txt
echo Ready to begin regsvr32.exe . . .
echo.
pause
echo.
echo Beginning registration . . .
echo *This will take time, close any popups that occur
echo.
call dll.bat
echo.
echo Deleting registration file . . .
if exist dll.bat del dll.bat
echo.
echo DONE.
echo.
pause >nul

【问题讨论】:

    标签: command-line batch-file for-loop regsvr32 findstr


    【解决方案1】:

    你想要的命令是for /f

    for /f %%f in ('type dll.txt') do regsvr32.exe %%f
    

    这需要type dll.txt 的输出,并一次将一行放入%%f。然后,您可以使用 %%f 作为其他命令的参数。

    如果您想做的不仅仅是regsvr32.exe %%f,您可以编写另一个批处理文件并调用它:

    for /f %%f in ('type dll.txt') do call process.bat %%f
    

    process.bat 然后会收到%1 的文件名。

    【讨论】:

    • 正是我的想法。我只是有问题。您的类型想法可能是解决方案。感谢论坛的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 2013-10-09
    • 2022-11-30
    • 1970-01-01
    相关资源
    最近更新 更多