BATCH/CMD: Print each line of a .txt into a variable

▼魔方 西西 提交于 2020-01-16 12:43:52

问题


Goal: Have every dll file in a computer passed into regsvr32.exe

Accomplished: cd\
::REM Exports every file and directory (/s) into a file in root of c: called dir.txt with only file names (/b)
dir /s /b>>dir.txt
::REM Now, this will contain every extension type. We only want dll's, so we findstr it into dll.txt:
findstr ".dll$" dir.txt>>dll.txt

The Kink:

Now, if I want to regsvr32.exe "file" every file that is now in dll.txt, I somehow need to get out every individual filename that is individually on each line. I was wondering if there is a third party command line tool that can export each line of the file into a variable. This way, I could:

==========

::REM assume this tool had switches /l for the line number, and /v:"" for variable to use, and used "file" at the end:

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  

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

Then the tool would process each path of each line of the file until it exits automatically, because there would be no more lines. Notice right after loop I set dll to nothing so that 'if not defined' will work each time.

If this type of third-party tool cannot be done, is there a way to do that with for?? I honestly never learned for, and tried to but could never figure it out.

Any help is greatly appreciated!

Sorry if this has already been answered.

EDIT/UPDATE: I have discovered how I will make this work.

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

The first link shows manually replacing the beginning with regsvr32.exe
The second shows how to use for in this case {also thanks to craig65535 FOR his help :)}

Code:

@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

回答1:


The command you want is for /f.

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

That takes the output of type dll.txt and puts one line at a time into %%f. You can then use %%f as an argument for some other command.

If you want to do more than regsvr32.exe %%f, you can write another batch file and call that:

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

process.bat would then receive the filename as %1.



来源:https://stackoverflow.com/questions/11303096/batch-cmd-print-each-line-of-a-txt-into-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!