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)
Related
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
I have just started to learn how to code in batch file. I would appreciate help with the following requirement for my batch file.
I have do some research and found this link (Is there a file in a directory with a modified date of today - Batch File) somehow similar to what i want.
Are there files in a directory with a modified date of today
If yes (may have more than one file with the modified date of today)
Copy the files into the folder
else
echo no file found
else
NO file for today
I try the solution in the link, but that solution only managed to return one file. For instance, there are two files namely test_20150114 and testString_20150114, the solution will only copy the file testString_20150114 to the folder. I want to copy files test_20150114 and testString_20150114 into the folder. How can i achieve it?
I try using two for loop to achieve but somehow fail.
Here the code i grab from another website with quite similar requirement as mine,
for /f "tokens=2" %%I in ("%date%") do set today=%%I
for /f "tokens=5" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "test"') do (
for /f "tokens=4*" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "%today%"') do (
rem record success for later
set found=1
rem search file %%I for "test" (case-insensitive).
find /i "string" "%%I">NUL
rem Was last command successful?
if %ERRORLEVEL%==0 (
echo test Files Found for today
If %%H GTR 0 (
echo Found %%I file is greater than 0kb
)
) else (
echo test string NOT found
)
)
)
for /f "tokens=5" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "testString"')do(
for /f "tokens=4*" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "%today%"') do (
rem record success for later
set found=1
rem search file %%I for "testString" (case-insensitive).
find /i "string" "%%I">NUL
rem Was last command successful?
if %ERRORLEVEL%==0 (
echo testString Files Found for today
If %%H GTR 0 (
echo Found %%I file is greater than 0kb
)
) else (
echo test string NOT found
)
)
)
*EDIT: I manage to get this solution from the help of Serenity
forfiles /s /m *.* /d 0 /c "cmd /c
if #fsize==0 Echo #file is 0Kb || Copy #file D:\Test
Add on questions: After managed to find the files with date of today, i want to copy the content of the file to the new file. (Test.txt content will be copy to Result.txt)
forfiles /d 0
list files modified today.
Forfiles /d 0 && Echo copy etc || Echo File Not Found
Although ForFiles can execute it's own commands on found files and prints it's own message File Not Found.
forfiles /d 0 /p c:\windows /m *.ini /s /c "cmd /c copy ^"#path^" c:\somewhereelse\"
Edit: Added cmd /c to command line
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'm kind of struggling with the following search.
I need to look for files which are misplaced in wrong folders. I have the following testing structure (in reality it is hudreds of folders):
C:\test2\John\John_phone.txt
C:\test2\Mary\John_address.txt
C:\test2\Mary\Mary_address.txt
C:\test2\Mary\Mary_phone.txt
C:\test2\Mary\Peter_address.txt
The files John_address.txt and Peter_address.txt are misplaced in Mary's folder. I want to check Mary's folder for any misplaced files and list them in a separate log file. So for the example above the log would contain the names (paths) of the two misplaced files, The deciding identifier is the person's name. I have this piece of code:
#echo off
cls
set /p name="Specify the name: "
::forfiles /p "%CD%\%name%" /s /m *.* /c "cmd /c echo #path">>log.txt
forfiles /p "%CD%\%name%" /s /m "| findstr /i /v "\*%name%*"" /c "cmd /c echo #path">>log.txt
pause
The commented line for forfiles works (lists all files in the folder), so I have an issue with findstr: ERROR: Files of type "| findstr /i /v *Mary*" not found.
The /v switch with findstr should find all files that do not contain the specified name, but obviously I'm doing something wrong while using it as input for forfiles.
I don't want to use the dir command since it lists additional information and I need to integrate the output into larger log file (I need to get only the path of the misplaced file).
Any help is appreciated.
Thanks.
Edit: Would it be easier to write the code if the correct file name would be fixed like this?
All_data_%NAME%_new.txt
The stuff before and after the name would be fixed and this format of the file name would be the only correct option, so anything else would have to be reported.
For a simple solution
#echo off
setlocal enableextensions disabledelayedexpansion
set "rootFolder=c:\test2"
( for /d %%z in ("%rootFolder%\*"
) do for %%y in ("%%~fz\*"
) do for /f "tokens=1 delims=_" %%a in ("%%~ny"
) do if /i not "%%~nxz"=="%%a" echo %%~fy
) > log.txt
This will iterate the files under each of the folders testing if their name starts with the same string that the folder name.
edited the original proposed solution does not work as intended, so, removed
edited - as the name of the file has not a fixed format, and the name of the folder can be in any place, this can be used
#echo off
setlocal enableextensions disabledelayedexpansion
set "rootFolder=c:\test2"
rem create a temporary file to hold the patterns to match against the list
rem of files to determine if it is correctly placed
set "patterns=%temp%\%~nx0.%random%.tmp"
> "%patterns%" (
for /d %%a in ("%rootFolder%\*") do echo(\\%%~nxa\\[^^\\]*%%~nxa[^^\\]*
)
rem Get the list of files, and filter to only get those that do not match
rem the generated patterns
dir /s /b /a-d "%rootFolder%\*" ^
| findstr /r /i /c:"%rootFolder:\=\\%\\[^\\]*\\." ^
| findstr /r /i /e /v /g:"%patterns%" ^
> log.txt
rem Patterns file is no longer needed
del "%patterns%" >nul 2>nul
this should help provided that comparison is case sensitive
for /f "delims=.'_\ tokens=2,3,*" %%a in ('forfiles /s /m *.txt /c "cmd /c echo #relpath"') do if NOT "%%a"=="%%b" #echo "%%a\%%b_%%c
So I have this code for sorting files within a complex of subdirectories within a main directory:
#ECHO OFF
SETLOCAL
SET relroot=g:\Pictures\Uda 18
(SET relroot=g:\Pictures\Uda 18)
SET "relroot=g:\Pictures\Uda 18"
SET "destdir=g:\Pictures\Uda 18\Sets"
:again
(SET artist=)
SET /p artist="Artist? "
IF NOT DEFINED artist GOTO :eof
MD "%destdir%\%artist%" 2>nul
FOR /f "delims=" %%i IN (
' dir /s /b /a-d "%relroot%\*%artist%*" '
) DO (
>>undo.txt ECHO %%i^|%destdir%\%artist%\%%~nxi
MOVE "%%i" "%destdir%\%artist%\%%~nxi" >nul)
)
GOTO again
It takes an input, searches the directory 'Uda 18' and everything within it, and moves all files with the input in their names to a folder named after the input under the directory 'Uda 18/Sets'. However, careless testing has caused me to draw files from 'Uda 18/Sets', the names of which mean they can't be reorganised using the batch. To avoid this, I need to exclude 'Uda 18/Sets' from where files are taken from, but I can't find a way. So as the titles asks; is it possible to skip a select subdirectory, and if so, how?
personally, I would prefer creatÃng a destination-directory BESIDES the source-directory, not within:
source: g:\pictures\uda 18\xxx
destination: g:\pictures\sorted\uda 18\xxx
This way you do not only avoid that problem, you will also allways have a clear and consistent directory-structure
I too would separate my destination folder from the folders to be processed.
Moving it out and moving it back later is an option though.
move "g:\Pictures\Uda 18\Sets" "g:\Pictures"
do your other commands here
move "g:\Pictures\Sets" "g:\Pictures\Uda 18"
FOR /f "delims=" %%i IN (
' dir /s /b /a-d "%relroot%\*%artist%*" ^|findstr /v /i /b /c:"%destdir%\\"'
) DO ( ECHO MOVE "%%i" "%destdir%\%artist%\%%~nxi")
Should list the MOVE to be performed /v EXCLUDING any directory /b beginning /i case-insensitive /c: the entire string including spaces ... where \ escapes \ so therefore excluding "g:\Pictures\Uda 18\Sets" and any of its subdirectories (where the full filename would begin g:\Pictures\Uda 18\Sets\"
simply remove the /v to select ONLY the matching files in "g:\Pictures\Uda 18\Sets" and its subdirectories.
Try this to draw the file names except those from the "sets" folder:
dir /b /s /a-d "uda 18" | find /v /i "sets"
... and with your folders:
echo "%destdir%\%artist%" | find /v /i "sets" >nul && goto:createFolder || goto:eof