在多个.txt文件中查找字符串

时间:2022-04-11 19:27:27

I have a folder with many .txt files. I would like to find string "X" in all of these files then I would like to copy the found strings into .txt files into a different folder.

我有一个包含许多.txt文件的文件夹。我想在所有这些文件中找到字符串“X”,然后我想将找到的字符串复制到.txt文件到另一个文件夹中。

So far I have tried :

到目前为止,我尝试过:

@echo on
findstr /m "X" "%userprofile%\Desktop\New_Folder\New_Folder\*.txt"
if %errorlevel%==0 do (
for %%c in (*.txt) do (
type %%c >> "%UserProfile%\Desktop\New_Folder\%%~nc.txt"
pause

I do not understand the output %%~nc.txt part it's suppost to copy the changed .txt files to a new folder with the same name.

我不明白输出%% ~nc.txt部分它是支持将已更改的.txt文件复制到具有相同名称的新文件夹。

I would like to point out that string "X" is found in different places in the .txt file.

我想指出字符串“X”位于.txt文件的不同位置。

3 个解决方案

#1


1  

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "mystring=x"
FOR %%a IN ("%sourcedir%\*.txt") DO FINDSTR "%mystring%" "%%a">nul&IF NOT ERRORLEVEL 1 FINDSTR "%mystring%" "%%a">"%destdir%\%%~nxa"

GOTO :EOF

You would need to change the settings of sourcedir and destdir to suit your circumstances and set mystring appropriately, noting that you may have to adjust the findstr switches to accomodate case, literal and space-in-target-string.

你需要更改sourcedir和destdir的设置以适应你的情况并适当地设置mystring,注意你可能必须调整findstr开关以适应case,literal和space-in-target-string。

Naturally, you could code sourcedir etc. directly as literals, but doing it this way means that the relevant strings need only be changed in one place.

当然,您可以直接将sourcedir等编码为文字,但这样做意味着只需在一个地方更改相关字符串即可。

#2


2  

This batch file can did the trick (-_°)

这个批处理文件可以做到这一点(-_°)

So, just give a try : ScanfilesWordSearch_X.bat

所以,试一试:ScanfilesWordSearch_X.bat

@ECHO OFF
::******************************************************************************************
Title Scan a folder and store all files names in an array variables
SET "ROOT=%userprofile%\Desktop"
Set "NewFolder2Copy=%userprofile%\Desktop\NewCopyTxtFiles"
SET "EXT=txt"
SET "Count=0"
Set "LogFile=%~dp0%~n0.txt"
set "Word2Search=X"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder and its subfolders.
REM And Populate the array with existent files in this folder and its subfolders
For %%a in (%EXT%) Do ( 
    Call :Scanning "%Word2Search%" "*.%%a"
    FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\*.%%a"') DO (
        ( find /I "%Word2Search%" "%%f" >nul 2>&1 ) && (
            SET /a "Count+=1"
            set "list[!Count!]=%%~nxf"
            set "listpath[!Count!]=%%~dpFf"
        )
    ) || (
            ( Call :Scanning "%Word2Search%" "%%~nxf")
    )
)
::***************************************************************
:Display_Results
cls & color 0B
echo wscript.echo Len("%ROOT%"^) + 20 >"%tmp%\length.vbs"
for /f %%a in ('Cscript /nologo "%tmp%\length.vbs"') do ( set "cols=%%a")
If %cols% LSS 50 set /a cols=%cols% + 20
set /a lines=%Count% + 10
Mode con cols=%cols% lines=%lines%
ECHO  **********************************************************
ECHO  Folder:"%ROOT%"
ECHO  **********************************************************
If Exist "%LogFile%" Del "%LogFile%"
rem Display array elements and save results into the LogFile
for /L %%i in (1,1,%Count%) do (
    echo [%%i] : !list[%%i]!
    echo [%%i] : !list[%%i]! -- "!listpath[%%i]!" >> "%LogFile%"     
)

(   
    ECHO.
    ECHO Total of [%EXT%] files(s^) : %Count% file(s^) that contains the string "%Word2Search%"
)>> "%LogFile%"
ECHO(
ECHO Total of [%EXT%] files(s) : %Count% file(s)
echo(
echo    Type the number of file that you want to explore 
echo(
echo        To save those files just hit 'S' 
set /p "Input="
For /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
        Call :Explorer "!listpath[%%i]!"
    )
    IF /I "%INPUT%"=="S" (
        Call :CopyFiles
    )
)   
Goto:Display_Results
::**************************************************************
:Scanning <Word> <file>
mode con cols=75 lines=3
Cls & Color 0E
echo(
echo         Scanning for the string "%~1" on "%~2" ...
goto :eof
::*************************************************************
:Explorer <file>
explorer.exe /e,/select,"%~1"
Goto :EOF
::*************************************************************
:MakeCopy <Source> <Target>
If Not Exist "%~2\" MD "%~2\"
Copy /Y "%~1" "%~2\"
goto :eof
::*************************************************************
:CopyFiles
cls
mode con cols=80 lines=20
for /L %%i in (1,1,%Count%) do (
    echo Copying "!list[%%i]!" "%NewFolder2Copy%\"
    Call :MakeCopy  "!listpath[%%i]!" "%NewFolder2Copy%">nul 2>&1 
)
Call :Explorer "%NewFolder2Copy%\"
Goto:Display_Results
::*************************************************************

#3


0  

You are close, but checking the ErrorLevel of findstr does not make sense here as this reflects the overall result, that is, ErrorLevel is set to 0 in case any of the files contain the search string.

你很接近,但是检查findstr的ErrorLevel在这里没有意义,因为这反映了整体结果,即如果任何文件包含搜索字符串,则ErrorLevel设置为0。

I would parse the output of findstr /M using a for /F loop and copy the returned files in the body:

我将使用for / F循环解析findstr / M的输出并复制正文中返回的文件:

for /F "eol=| delims=" %%F in ('
    findstr /M /I /C:"X" "%USERPROFILE%\Desktop\New_Folder\New_Folder\*.txt"
') do (
    copy "%%F" "%USERPROFILE%\Desktop\New_Folder\"
)

This copies all those files which contain the literal search string (in a case-insensitive manner).

这将复制包含文字搜索字符串的所有文件(以不区分大小写的方式)。

#1


1  

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "mystring=x"
FOR %%a IN ("%sourcedir%\*.txt") DO FINDSTR "%mystring%" "%%a">nul&IF NOT ERRORLEVEL 1 FINDSTR "%mystring%" "%%a">"%destdir%\%%~nxa"

GOTO :EOF

You would need to change the settings of sourcedir and destdir to suit your circumstances and set mystring appropriately, noting that you may have to adjust the findstr switches to accomodate case, literal and space-in-target-string.

你需要更改sourcedir和destdir的设置以适应你的情况并适当地设置mystring,注意你可能必须调整findstr开关以适应case,literal和space-in-target-string。

Naturally, you could code sourcedir etc. directly as literals, but doing it this way means that the relevant strings need only be changed in one place.

当然,您可以直接将sourcedir等编码为文字,但这样做意味着只需在一个地方更改相关字符串即可。

#2


2  

This batch file can did the trick (-_°)

这个批处理文件可以做到这一点(-_°)

So, just give a try : ScanfilesWordSearch_X.bat

所以,试一试:ScanfilesWordSearch_X.bat

@ECHO OFF
::******************************************************************************************
Title Scan a folder and store all files names in an array variables
SET "ROOT=%userprofile%\Desktop"
Set "NewFolder2Copy=%userprofile%\Desktop\NewCopyTxtFiles"
SET "EXT=txt"
SET "Count=0"
Set "LogFile=%~dp0%~n0.txt"
set "Word2Search=X"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder and its subfolders.
REM And Populate the array with existent files in this folder and its subfolders
For %%a in (%EXT%) Do ( 
    Call :Scanning "%Word2Search%" "*.%%a"
    FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\*.%%a"') DO (
        ( find /I "%Word2Search%" "%%f" >nul 2>&1 ) && (
            SET /a "Count+=1"
            set "list[!Count!]=%%~nxf"
            set "listpath[!Count!]=%%~dpFf"
        )
    ) || (
            ( Call :Scanning "%Word2Search%" "%%~nxf")
    )
)
::***************************************************************
:Display_Results
cls & color 0B
echo wscript.echo Len("%ROOT%"^) + 20 >"%tmp%\length.vbs"
for /f %%a in ('Cscript /nologo "%tmp%\length.vbs"') do ( set "cols=%%a")
If %cols% LSS 50 set /a cols=%cols% + 20
set /a lines=%Count% + 10
Mode con cols=%cols% lines=%lines%
ECHO  **********************************************************
ECHO  Folder:"%ROOT%"
ECHO  **********************************************************
If Exist "%LogFile%" Del "%LogFile%"
rem Display array elements and save results into the LogFile
for /L %%i in (1,1,%Count%) do (
    echo [%%i] : !list[%%i]!
    echo [%%i] : !list[%%i]! -- "!listpath[%%i]!" >> "%LogFile%"     
)

(   
    ECHO.
    ECHO Total of [%EXT%] files(s^) : %Count% file(s^) that contains the string "%Word2Search%"
)>> "%LogFile%"
ECHO(
ECHO Total of [%EXT%] files(s) : %Count% file(s)
echo(
echo    Type the number of file that you want to explore 
echo(
echo        To save those files just hit 'S' 
set /p "Input="
For /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
        Call :Explorer "!listpath[%%i]!"
    )
    IF /I "%INPUT%"=="S" (
        Call :CopyFiles
    )
)   
Goto:Display_Results
::**************************************************************
:Scanning <Word> <file>
mode con cols=75 lines=3
Cls & Color 0E
echo(
echo         Scanning for the string "%~1" on "%~2" ...
goto :eof
::*************************************************************
:Explorer <file>
explorer.exe /e,/select,"%~1"
Goto :EOF
::*************************************************************
:MakeCopy <Source> <Target>
If Not Exist "%~2\" MD "%~2\"
Copy /Y "%~1" "%~2\"
goto :eof
::*************************************************************
:CopyFiles
cls
mode con cols=80 lines=20
for /L %%i in (1,1,%Count%) do (
    echo Copying "!list[%%i]!" "%NewFolder2Copy%\"
    Call :MakeCopy  "!listpath[%%i]!" "%NewFolder2Copy%">nul 2>&1 
)
Call :Explorer "%NewFolder2Copy%\"
Goto:Display_Results
::*************************************************************

#3


0  

You are close, but checking the ErrorLevel of findstr does not make sense here as this reflects the overall result, that is, ErrorLevel is set to 0 in case any of the files contain the search string.

你很接近,但是检查findstr的ErrorLevel在这里没有意义,因为这反映了整体结果,即如果任何文件包含搜索字符串,则ErrorLevel设置为0。

I would parse the output of findstr /M using a for /F loop and copy the returned files in the body:

我将使用for / F循环解析findstr / M的输出并复制正文中返回的文件:

for /F "eol=| delims=" %%F in ('
    findstr /M /I /C:"X" "%USERPROFILE%\Desktop\New_Folder\New_Folder\*.txt"
') do (
    copy "%%F" "%USERPROFILE%\Desktop\New_Folder\"
)

This copies all those files which contain the literal search string (in a case-insensitive manner).

这将复制包含文字搜索字符串的所有文件(以不区分大小写的方式)。