I have a folder with subfolders include txt and pdf files. There is a Pdf file for each txt file which has nearly same name.
For example; for each ABC_R10.txt --> there is a ABC).pdf file.
In Windows 10, with a batch file,
I want to search specific string in a .txt file with FINDSTR command, and copy files, which contain my string, into current folder. I achieved proper code until this point.
CLS
#ECHO OFF
ECHO FIND BUKUM
findstr /m /s /i /p /c:"BUKUM" *.txt > logfile.xls
for /f "delims=" %%a in ('findstr /m /s /i /p /c:"BUKUM" *.txt') do ^
copy "%%a" "%cd%"
if errorlevel 1 echo nothing found.
PAUSE
CLS
EXIT
But I want to find file name of exact match but get pdf file with similar name, not txt file.
I have to get ABC of ABC_R10.txt and add ).pdf string and get ABC).pdf
Substring of _R occurs each .txt file.
How can I achieve it?
Based solely on your now edited question:
#For /F "Delims=_" %%A In ('FindStr /SPMIC:"BUKUM" *.txt') Do #Copy /Y "%%A).pdf">Nul
EDIT: there is nothing to say to #Compo's modification,
just when using the ) unquoted inside a code block it has to be escpaped ^)
#ECHO OFF
CLS
ECHO FIND BUKUM
for /f "delims=_" %%A in (
'findstr /msip /c:"BUKUM" *.txt'
) do if exist "%%A).pdf" (
copy "%%A).pdf" "%cd%"
Echo copied %%A^).pdf to %cd%
) else echo not found "%%A).pdf"
PAUSE
Related
Im making a small script but i reached a head scratch
I want the batch file to look for example.exe or Folder name in all drives then cd to it and create a txt file inside it? is that even possible :P?
cd /d D:
dir example.exe /s /p
lets say its found and the dir is D:/Example.exe
so i want the batch to do this,
if example.exe is found cd to it directory then
REM. >> "D:/logs.txt
is that possible? –
what do i put after "if exist" for the batch file to automatically switch to the found file directory
#echo on
cd /d D:
dir example.exe /s /p if exist (whether its in D:/Folder/folder or D:/Folder go to directory)
echo >test.txt
pause
Finally Solution by #Stephan best working answer
#echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('wmic logicaldisk where "size>0" get caption^|find ":"') do (
for /f "delims=" %%A in ('dir /s /b %%a:\example.exe 2^>nul') do (
ECHO break>"%%~dpAtest.txt"
)
)
To catch the output of a command, use for.
dir /p does not make sense here (it's to pause if the output is longer than the screen). You want /b (bare format; filename only / drive/path/filename when used with /s).
%%~dpA gives drive:\path\ only.
break>filename to create an empty file (REM. does also work, but I prefer break)
#echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('wmic logicaldisk where "size>0" get caption^|find ":"') do (
for /f "delims=" %%A in ('dir /s /b %%a:\example.exe 2^>nul') do (
ECHO break>"%%~dpAtest.txt"
)
)
this puts an empty test.txt to every folder in every available drive where there is an example.exe. If there already should be a test.txt, it gets overwritten by an empty file.
NOTE: this only echoes the break command to the screen (for security reason). If the output is like you want it, remove the ECHO.
Updated answer, since I got your question after this update:
In order to process all the files you found:
FOR /F "delims=#" %%f IN ('dir example.exe /s /b ') DO (
echo example.exe was found at path %%~pf
)
Now you can modify your ECHO statement:
ECHO some text > C:\logs.txt
If you want to append to the file instead of replacing, you have to use two brackets:
ECHO some text >> C:\logs.txt
You can enter a path after > . You don't need to CD before.
You can also use the variable for CD:
cd %%~pf
If you want some other part of %%f, you can do FOR /? on your command line or see the reference
To put simply I am a temp IT for (Blank) motor company. I am trying to build a batch file that will search Progresslog.txt for a string. this string will be a user name like awilson. Thing is there is 1 progresslog.txt for each computer I have backed up under the folder names HMC(insert s/n) I need to search each progresslog for the string but can't figure out what I'm doing. This is what I have so far.
for /d /r ".\" %%a in (*) do if /i "%%~nxa"=="progresslog.txt" set "folderpath=%%a" & echo "%folderpath%"
for /f %%f in ('dir /b %folderpath%') do echo %%f & findstr /m "%Input%" progresslog.txt
if %errorlevel%==0 (
echo Found String!
) else goto A
It is a lot easier
findstr /s /l /m /c:"awilson" progresslog.txt
Just search in subdirectories (/s) the literal indicated (/l /c:"...") in files named progresslog.txt and output only the name of files found (/m)
I am new here. I found the way to find only one string from a directory.
findstr /S /M /C:"string" /C:folder *.txt
I can get success for only one string. But my wish is to find a solution, where i will write my wanted multiple strings in a file and will call that file by command and write the directory or folder name, where these information can be found.
i found some information from this forum but i could not succeeded.
#echo off
set RESULT_FILE="result.txt"
set /p "var1=Enter the String to Find: "
pushd %~p0
type NUL > %RESULT_FILE%.tmp
for /f "delims=" %%a in ('dir /B /S *.txt') do (
for /f "tokens=3 delims=:" %%c in ('find /i /c "%var1%" "%%a"') do (
for /f "tokens=*" %%f in ('find /i "%var1%" "%%a"') do if %%c neq 0 echo %%f
)
) >> "%RESULT_FILE%".tmp
move %RESULT_FILE%.tmp %RESULT_FILE% >nul 2>&1
:: Open the file
"%RESULT_FILE%"
popd
this code is also not working for me... after run, i get a blank result.txt file
I hope you already experienced the same problem and can help me to get rid of that problem.
If you have any questions, please let me know. I will be happy to answer.
Thanks in advance.
findstr /r /s /m "string1 string2 string3" *.txt
Space seperates search terms.
I need help creating a batch script that will add the filename to the first line of every .csv within all subfolders. The batch file will be in the mainfolder. The same folder will contain several subfolders, each containing a large amount of .csv files. I need the files to stay within their respective folders. My goal is to add this to another working script I have that merges all files within a subfolder into a single .csv. This will provide a way to navigate through the merged file. I found some similar posts, but they weren't quite what I was looking for. This is the closest thing I could find but the delimiter stuff is throwing me off Batch file to Write Filename to first line
File Structure Example:
C:\MainFolder\batch_script.bat
C:\MainFolder\SubFolder1\csv1.csv
C:\MainFolder\SubFolder1\csv2.csv
C:\MainFolder\SubFolder2\csv3.csv
C:\MainFolder\SubFolder2\csv4.csv
File Contents before running script
csv1.csv:
This is the content of csv1.csv
csv2.csv:
This is the content of csv2.csv
csv3.csv:
This is the content of csv3.csv
csv4.csv:
This is the content of csv4.csv
File Contents after running script:
csv1.csv:
csv1.csv
This is the content of csv1.csv
csv2.csv:
csv2.csv
This is the content of csv2.csv
csv3.csv:
csv3.csv
This is the content of csv3.csv
csv4.csv:
csv4.csv
This is the content of csv4.csv
P.S.
I also found this code on another website but I'm not sure how I would make it loop through the different subfolders
FOR /F "usebackq tokens=1" %%i IN (`DIR /B`) DO (
ECHO %%i >> %1
TYPE %%i >> %1
)
#echo off
setlocal enableextensions disabledelayedexpansion
for /r %%a in (*.csv) do (
set /p "firstLine=" < "%%~fa"
setlocal enabledelayedexpansion
for /f "delims=" %%b in (".!firstLine!") do endlocal & if not "%%~b"==".%%~nxa" (
( echo %%~nxa
type "%%~fa"
) > "%%~fa.new"
move /y "%%~fa.new" "%%~fa" >nul
)
)
What it does is generate a new file with the name of the csv as the first line, appends the csv to the new file and moves the new file over the csv. In the process, it tests if the content of the first line is the same as the name of the csv to avoid reinclusion of the filename on each run of the batch file.
edited to adapt to comments - As the process will not be run more than once for set of csv files, the full code can be reduced to
#echo off
setlocal enableextensions disabledelayedexpansion
for /r %%a in (*.csv) do (
( echo %%~nxa
type "%%~fa"
) > "%%~fa.new"
move /y "%%~fa.new" "%%~fa" >nul
)
Try this:
for /r %%a in (*.csv) do (
copy /y "%%~a" "%temp%\%%~na"
Echo %%~na > "%%~a"
for /f "usebackq" %%b in ("%temp%\%%~na") do Echo %%b >> "%%~a"
del "%temp%\%%~na"
)
And that should do what you want it to. I would test it out first if you could.
This should create an all.csv in every folder in the tree under the C:\MainFolder and add the filename before the contents of each CSV file.
The filename will contain the full path but that can easily be changed if needed.
#echo off
for /d /r "C:\MainFolder\" %%a in (*) do (
pushd "%%a"
(for %%b in (*.csv) do (
echo %%b
type "%%b"
)
)>all.tmp
ren all.tmp all.csv
popd
)
pause
I have a need for a batch file or utility that would be able to find any "un-compressed archive folders" that are no longer needed and can now be deleted because the original archive file is still present.
The key is that the "un-compressed folder" and the "original archive file" always have the same name except for the file extension of the archive file. I do not want to automatically delete anything; I just want to create a list of folders that I can manually check out. So the sequence would be a 4 step process:
1) Search for all archive files using wildcards such as *.zip, *.rar, *.iso
2) Create a list of all of the filenames that are found - minus the file extensions
3) Use the list created in step two to search for any folders with those names
4) Create a text file with any folders found in step three.
I tried modifying a batch file that I found in these posts but it didn't work and I would like to start from scratch. Any help would be greatly appreciated.
Thanks
Ok, I'll do this step by step:
Step 1:
set dir="C:\...[path to root directory]"
where /r %dir% *.zip *.iso *.rar >> log.txt
Note the where utility should be on your computer if using windows 7.
Step 2:
ren log.txt log.tmp
for /f "delims=." %%a in (log.tmp) do (Echo %%a >> log.txt)
del log.tmp
The above code will not handle files names with periods in it
Step 3:
for /f "tokens=*" %%a in (log.txt) do (
where /r %dir% %%a* >> files.txt
)
Not 100% sure if above will work, tell me if it doesn't.
Step 4:
Rem This code will handle file paths to directories
Ren files.txt files.tmp
for /f "tokens=*" %%a in (files.tmp) do (
Echo %%~pa >> files.txt
)
del files.tmp
Rem The below code will ged rid of repeated direcotries
ren files.txt files.tmp
Echo. > files.txt
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (files.tmp) do (
set var=1
for /f "tokens=*" %%b in (files.txt) do (
if "%%~a" equ "%%~b" set var=0
)
if !var!==1 Echo %%a >> files.txt
)
del files.tmp
And I'm rather confident that should work. Of course I haven't tested this, but run all of this with #Echo on and a pause command between each sect (or as seperate batch files) so that if an eror does occur I can try helping you.
Hope this was helpful, Mona.
#echo off
setlocal enableextensions
set "rootDir=d:\_data_"
set "fileList=%~dp0\%~n0.files.list"
set "folderList=%~dp0\%~n0.folders.list"
rem generate list of compressed files names
break > "%fileList%"
for /F "tokens=*" %%f in ('where /r "%rootDir%" *.zip *.rar *.iso *.7z 2^>nul') do (
>> "%fileList%" echo %%~nf
)
rem check compressed file list against directory list
break > "%folderList%"
for /F "tokens=*" %%f in ('dir "%rootDir%" /s /b /ad ^| findstr /e /g:"%fileList%" ') do (
>> "%folderList%" echo %%f
)
type "%folderList%"
endlocal