简体   繁体   中英

find string in file and copy the folders

I have a text file as below, i need to detect/find/extract the file names inside "<" and ">", and then search for that filename in another folder and copy it to a new folder. and these with any occurrence.

#include <SD.h>                      // Se inclye la libreria SD
#include <SPI.h>              // Se incluye la libreria para la comunicacion SPI
#include <TMRpcm.h>           //  Se incluye la libreria para reproducir .WAV
#include <HCSR04.h>

#define SD_ChipSelectPin 4  // Selecciona la tarjeta SD. Puede ser cualquier pin que NO se esté         utilizando ya. 
TMRpcm Audio;   // Se crea un objeto para usar las funciones de la libreria TMRpcm
UltraSonicDistanceSensor distanceSensor(7, 8);  // Initialize sensor that uses digital pins 13 and 12.

bool boton_A = 0; //Variable que guarda el estado del pulsador de cambiar de cancion
bool boton_B = 0; //Variable que guarda el estado del pulsador de pausar
int song = 0; //Se inician en 0 para que cuando se incremente la cancion al principio se reproduzca la cancion 1
int last_song = 0; //Almacena el número de la ultima cancion
int pos;        // posicion detectada

in that case i need the bat to copy SD.h SPI.h TMRpcm.h and HCSR04.h to a new folder. i tried a lot but without success. I don't put my previous attempts because they're very basic stuff. And in some moment I realized I need help.

Perhaps this is the sort of thing you're looking for:

@For /F "Tokens=2 Delims=<>" %%G In (
    '^""%__AppDir__%findstr.exe" /IRC:"^#include[\ ]*<.*\.h>" "S:\omewhere\A Text File.txt"^"'
)Do @"%__AppDir__%xcopy exe" "X:\Another Folder\%%G" "Y:\A New Folder\" /Y>NUL 2>&1

Please modify the locations/file name/directory names as necessary.

I will not be expanding upon anything in the above, as you did not show any indication that you had made any attempt at your task before asking here. If you want to know how a particular command works, open a Command Prompt, ( cmd.exe ), window and enter the command name followed by the standard help option /? , ie for /? , findstr /? , and xcopy /?

You can do something like that , just modify the variables to yours :

@echo off
Title find string in file and copy to a new folder using batch script
Rem You should modify the variable InputFile to yours 
set "InputFile=Test.txt"
Set "OutPutFile=Output.txt"
Call :Extract "%InputFile%" "%OutPutFile%"
Rem You should modify the variable Source_Folder to yours 
Set "Source_Folder=E:\StackOverflow\"
Rem You should modify the variable Target_Folder to yours 
Set "Target_Folder=E:\TargetFolder\"

If Not Exist "%Target_Folder%" MD "%Target_Folder%"

@for /f "delims=" %%a in ('Type "%OutPutFile%"') do (
    @for /f "delims=" %%b in ('Dir /b /s "%Source_Folder%" ^|find /I "%%a"') do (
        echo "%%~fb"
        Copy /Y "%%~fb" "%Target_Folder%"
    )
)
pause
Exit
::-----------------------------------------------------
:Extract <InputFile> <OutPutFile>
(
echo Data = WScript.StdIn.ReadAll
echo Data = Extract(Data,"\<(\S+)\>"^)
echo WScript.StdOut.WriteLine Data
echo Function Extract(Data,Pattern^)
echo    Dim oRE,oMatches,Match,M,i
echo    set oRE = New RegExp
echo    oRE.IgnoreCase = True
echo    oRE.Global = True
echo    oRE.Pattern = Pattern
echo    set oMatches = oRE.Execute(Data^)
echo    If not isEmpty(oMatches^) then
echo        For i = 0 To oMatches.Count - 1
echo            M = M ^& oMatches(i^).SubMatches(0^) ^& vbcrlf 
echo        Next 
echo    End if
echo    Extract = M
echo End Function
)>"%tmp%\%~n0.vbs"
cscript //nologo "%tmp%\%~n0.vbs" < "%~1" > "%~2"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
exit /b
::-----------------------------------------------------


1) This code will try to find your files in C:\\Program Files (x86)\\Arduino\\libraries , and, if found, it will search for the next file


@echo off && setlocal EnableDelayedExpansion

title <nul && title .\%~nx0 
cd/d "%systemdrive%" && cd\ 

set "_output_dir=G:\SUPER_USER\Q60142344" 
set "_input_file=G:\SUPER_USER\Q60142344\Q60142344.txt"

for %%s in (findstr,find,where
)do set "_%%~s=%%~s" && call set "_%%~s=%__APPDIR__%!_%%~s!.exe"

cd /d "C:\Program Files (x86)\Arduino\libraries" 
for /f "tokens=2 delims=<>" %%i in ('type "!_input_file!"^|!_findstr! "<  >"
')do for /f "tokens=* delims= " %%# in ('2^>nul !_where! /r . "%%~i"^|!_find! /i "%%~i"
')do if not "%%~#"=="!_output_dir!\%%i" >nul copy /y /v "%%~#" "!_output_dir!" && echo/File "%%~#" Copied to "!_output_dir!"))

endlocal && goto :EOF

  • Outputs:
File "C:\Program Files (x86)\Arduino\libraries\SD.h" Copied to "G:\SO_en\Q60142344"))
File "C:\Program Files (x86)\Arduino\libraries\SPI.h" Copied to "G:\SO_en\Q60142344"))
File "C:\Program Files (x86)\Arduino\libraries\HCSR04.h" Copied to "G:\SO_en\Q60142344"))

  • Just edit by adding the pertinent value in the above variable to match the necessary actions
set "_output_dir=G:\\SO_en\\Q60142344" && set "_input_file=G:\\SO_en\\Q60142344\\Q60142344.txt" set "_output_dir=A:\\the\\path\\to\\folder\\target" && set "_input_file=B:\\the\\path\\to\\file\\input_file.h"




2) This code will try to find your files in C: , and, if found, it will search for the next file

In addition, it will avoid finding in the destination folder, if it exists in the destination folder, it will also ignore.

  • Just edit by adding the pertinent value in the above variable to match the necessary actions
set "_output_dir=G:\\SO_en\\Q60142344" && set "_input_file=G:\\SO_en\\Q60142344\\Q60142344.txt" set "_output_dir=A:\\the\\path\\to\\folder\\target" && set "_input_file=B:\\the\\path\\to\\file\\input_file.h"



@echo off && setlocal EnableDelayedExpansion

title <nul && title .\%~nx0 
cd/d "%systemdrive%" && cd\ 

set "_output_dir=G:\SO_en\Q60142344" 
set "_input_file=G:\SO_en\Q60142344\Q60142344.txt"

for %%s in (findstr,find,where
)do set "_%%~s=%%~s" && call set "_%%~s=%__APPDIR__%!_%%~s!.exe"

for /f "tokens=2 delims=<>" %%i in ('type "!_input_file!"^|!_findstr! "<  >"
')do if not exist "!_output_dir!\%%~i" (for /f "tokens=* delims= " %%# in ('
2^>nul !_where! /r %systemdrive%\ "%%~i"^|!_find! /i "%%~i"')do if not "%%~#"=="!_output_dir!\%%i" (
>nul copy /v "%%~#" "!_output_dir!" && cd/d "%~dp0" && echo/File "%%~#" Copied to "!_output_dir!" ))

endlocal && goto :EOF 

  • Outputs:
File "C:\Users\ecker\AppData\Local\Temp\SD.h" Copied to "G:\SO_en\Q60142344"
File "C:\Users\ecker\Downloads\SPI.h" Copied to "G:\SO_en\Q60142344"
File "C:\Users\ecker\Downloads\HCSR04.h" Copied to "G:\SO_en\Q60142344"



3) This code will try to find your files on all of your drives on the computer and, if found on a drive, it will search for the next file, without checking the remaining drives for the same file already found.

In addition, it will avoid finding in the destination folder, if it exists in the destination folder, it will also ignore.

  • Just edit by adding the pertinent value in the above variable to match the necessary actions
set "_output_dir=A:\\the\\path\\to\\folder\\target" set "_input_file=B:\\the\\path\\to\\file\\input_file.h"


@echo off && setlocal EnableDelayedExpansion

title <nul && title .\%~nx0
set "_output_dir=G:\SO_en\Q60142344"
set "_input_file=G:\SO_en\Q60142344\Q60142344.h"
set "_wmic=%__APPDIR__%wbem\wmic.exe logicaldisk get caption"
for %%s in (findstr,find,where)do set "_%%~s=%%~s" && call set "_%%~s=%__APPDIR__%!_%%~s!.exe"

for /f "tokens=2 delims=<>" %%i in ('type "!_input_file!"^|!_findstr! "< >"
')do for /f "tokens=1" %%D in ('!_wmic! ^|!_find! ":"')do cd /d "%%~D\" && cd\ & if not exist "!_output_dir!\%%~i" (
echo/Looking for file %%i in %%~D\... & for /f "tokens=*delims= " %%# in ('2^>nul !_where! /r \ "%%~i"^|!_find! /i "%%i"
')do if not "%%~#"=="!_output_dir!\%%i" >nul copy /y "%%~#" "!_output_dir!" && cd/d "%~dp0" && echo/File "%%~#" Copied to "!_output_dir!")

endlocal && goto :EOF


  • Outputs:
Looking for file SD.h in C:\...
File "C:\Users\ecker\AppData\Local\Temp\SD.h" Copied to "G:\SO_en\Q60142344"
Looking for file SPI.h in C:\...
File "C:\Users\ecker\Downloads\SPI.h" Copied to "G:\SO_en\Q60142344"
Looking for file TMRpcm.h in C:\...
Looking for file TMRpcm.h in G:\...
File "G:\TMRpcm.h" Copied to "G:\SO_en\Q60142344"
Looking for file HCSR04.h in C:\...
File "C:\Users\ecker\Downloads\HCSR04.h" Copied to "G:\SO_en\Q60142344"

Obs.: To copy to one of the folders below, understand that it is necessary to run the batch as an administrator::


 %ProgramFiles% == C:\\Program Files (x86) %ProgramFiles(x86)% == C:\\Program Files (x86) %ProgramW6432% == C:\\Program Files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM