2016-09-16 51 views
-1

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

到目前为止,我曾尝试:

@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 

我不明白输出%%~nc.txt部分是suppost的改变.txt文件复制到一个新的文件夹名称相同。

我想指出字符串"X".txt文件的不同位置找到。

+0

我不知道'%%〜vc'是什么;键入'for /?'到命令提示窗口并阅读帮助;没有'〜v'修饰符(也没有'for %% v'循环)。无论如何,你的代码是不完整的,因为有不平衡的括号......那么把你的需求中的几个字符作为文本输入到这里而不是将它们提供为图片呢? – aschipfl

+0

@aschipfl我喜欢创意。感谢'for /?'我会采取一个squiz。 – MALKAVIAN

+0

就我而言,图像显示的是非大写的** x **,而不是大写的** X **。此外,我会考虑** x **作为一个字符,如果它仅出现在与空白相邻的位置(string1 ** x ** string3),则只会成为一个字符串。试图制定有价值的解决方案时,这种信息至关重要。 – Compo

回答

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 

您将需要改变设置为sourcedirdestdir以适合您的情况并适当设置mystring,注意您可能需要调整findstr开关以适应案件,文字和空间在目标字符串。

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

0

你接近了,但是检查findstrErrorLevel没有意义在这里,因为这反映了总的结果,即,ErrorLevel设置为0的情况下,文件的任何包含搜索字符串。

我会用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\" 
) 

这会将所有那些包含的文字搜索字符串(在不区分大小写的方式)文件。

+0

[编辑](http://stackoverflow.com/revisions/39534388/2):增加了'/ I'开关来做不区分大小写的搜索;添加'/ C'开关将搜索字符串视为字面值; – aschipfl

+0

只是为了说明这是OP询问的问题,“我想在所有这些文件中找到字符串”X“,然后我想将找到的字符串复制到.txt文件中,并将其放入不同的文件夹中。”图片要求将它们复制“只有字符串x”。我假设该问题要求使用匹配字符串的文件名进行复制,但这样做目标文件只包含匹配字符串*(s)*。 – Compo

+0

@Compo,我同意,这是不是很清楚需要什么;无论如何,我正在复制包含搜索字符串的所有文件... – aschipfl

2

此批处理文件可以做的伎俩(-_°)

所以,只要给一个尝试: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 
::************************************************************* 
相关问题