Search the directories with modified date of today - batch-file

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

Related

findstr on multipule values

Please forgive me if this is glaring obvious but I have the behaviour of this for loop, which loops through certain named directories (US_Site4 etc.) and reads a config file in each of them to run a command separately for each:
setlocal enabledelayedexpansion
REM The Root to start searching from
SET "InstallSets=D:\InstallSets"
REM Folders to not search with in
SET "ExcludeStr=findstr /I /v ^"\Master Files \Training \Software^""
REM
SET "InstallSets=D:\InstallSets"
for /f "delims=*" %%F IN ('dir /s /b /on "!InstallSets!" ^| findstr /E /C:"Config.xml" ^| !ExcludeStr!') do (
(ECHO "%%F"| findstr /I "\US_Site4 \US_Site5" 1>NUL) && (
ECHO "%%F"...
)
)
And I would like to be able replicated in a more user configurable way for furure additions, i.e. settings a variable at the top of a batch file, e.g.:
setlocal enabledelayedexpansion
REM The Root to start searching from
SET "InstallSets=D:\InstallSets"
REM Folders to not search with in
SET "ExcludeStr=findstr /I /v ^"\Master Files \Training \Software^""
REM Folders to search in only
SET "DoOnlyStr=findstr /I ^"\US_Site4 \US_Site5^" 1>NUL"
for /f "delims=*" %%F IN ('dir /s /b /on "!InstallSets!" ^| findstr /E /C:"Config.xml" ^| !ExcludeStr!') do (
(ECHO "%%F"| %DoOnlyStr%) && (
ECHO "%%F"...
)
)
The first one brings back 2 results which are site4 and 5 as expected, however the second one brings back 4 (The 2 I wanted but duplicated). Why is this and how could get a "user friendly" configurable version like just setting the variable? I want to then later make a second file with findstr /I /V to do the opposite and do everything else BUT Site4 and 5.
Kind regards
Adam
It was the 1>NUL that was displaying only the 1 output and when that was in a variable it wasn't behaving as it should, so it was displaying 2 (as it seemed to when I removed it also), But is was a combination of the comment from #aschipfl with removing the " " on the set variables and how I structured it below. So my final solution that worked for me was:
setlocal enabledelayedexpansion
REM The Root to start searching from
SET "InstallSets=D:\InstallSets"
REM Folders to not search with in
SET ExcludeStr=findstr /I /v ^"\Master Files \Training \Software^"
REM Folders to search in only
SET DoOnlyStr=findstr /I ^"\US_Site4 \US_Site5^"
for /f "delims=*" %%F IN ('dir /s /b /on "!InstallSets!" ^| findstr /E /C:"Config.xml" ^| !ExcludeStr!') do (
(ECHO "%%F"| %DoOnlyStr% 1>NUL) && (
ECHO "%%F"...
)
)
Unsure why the !DoOnlyStr! didn't work though and it had to be %DoOnlyStr% as I have setlocal enabledelayedexpansion at the start

How to rename multiple folders that have a set number of files within that folder using batch script?

I am very new to programming so your help is much appreciated!
I have a top directory, containing subfolders which have files in them. I want to add the string "x" after the folder name e.g. foldername -> foldernamex if the subfolders contain less than 4 or greater than 4 files. How do I go about doing this?
Here is my attempt so far:
for %%e in ('dir ^| find "File(s)"') do (
set cnt=%%e echo File count = %cnt%
if File count <4 do ren "%%e" "%%~nxf_x"
)
This uses the dir command and the brief /b switch with the file-only switch /a-d and pipes the result through the find /c /v "" command to count the number of files.
findstr is used to find the exact numeral 4 and if it doesn't detect 4 then the script renames the folder and restarts the loop to handle nested subdirectories (because the folder structure changes when a folder is renamed).
It's not tested - so test it on a sample folder tree with copies of some folders first.
#echo off
:loop
for /f "delims=" %%a in ('dir /b /s /ad ^|findstr /v /i "x$" ') do (
dir "%%a" /b /a-d 2>nul |find /c /v "" |findstr "^4$" >nul || (ren "%%a" "%%~nxax" & goto :loop)
)
pause
Simpler code for processing only the folders inside the current folder.
#echo off
for /f "delims=" %%a in ('dir /b /s /ad ') do (
dir "%%a" /b /a-d 2>nul |find /c /v "" |findstr "^4$" >nul || ren "%%a" "%%~nxax"
)
pause

How to find all files that don't contain a string?

I've found the following code on this site:
#echo off
setlocal
set "rootFolder=c:\yourRootPath"
set "fileMask=*.txt"
set "outFile=missing.txt"
>"%outFile%" (
for /d %%D in ("%rootFolder%") for %%F in ("%%D\%fileMask%") do (
findstr /nbr "$O..*\.MIN%%" "%%F" | findstr /bl "1:" >nul || echo %%F
)
)
However, when I run the batch file, I receive the following error: "for was unexpected at this time."
My research says that this is normally caused by not using a double %%, but obviously this is not the case.
I guess it is something simple, but I can't work it out, any tips please?
This method should run much faster:
#echo off
setlocal
set "rootFolder=c:\yourRootPath"
set "fileMask=*.txt"
set "outFile=%~P0missing.txt"
cd "%rootFolder%"
findstr /S /M /B /R "$O..*\.MIN%%" "%fileMask%" > temp.tmp
( dir /S /A-D /B "%fileMask%" | findstr /V /G:temp.tmp ) > "%outFile%"
del temp.tmp
The missing.txt file is stored in the same folder of the Batch file, that must NOT be in the folder of the search files!

How to call a .txt file and find multiple strings of .txt file from a folder or directory using batch script

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.

Windows Command to identify folders with only one file

I need a command to run from the Windows CLI to identify any folders (or sub folders) that contain only one file. If the folder contains two files, it should not be included. In the end, I need to output this list to a text file. It should contain the full folder path.
Ex: OutputLog.txt
C:\fold1
C:\fold1\sub
C:\fold3
C:\fold4
This should work to identify folders with one file.
#echo off
for /d /r "d:\base\folder" %%a in (*) do (
dir /b /a-d "%%a" 2>nul |find /c /v "" |findstr "^1$" >nul && >>file.txt echo %%a
)
#echo off
setlocal EnableDelayedExpansion
(for /D /R %%a in (*) do (
set count=0
for %%b in ("%%a\*.*") do set /A count+=1
if !count! equ 1 echo %%a
)) > OutputLog.txt
#echo off
set "parentfolder=c:\test"
for /f "tokens=* delims=" %%F in ('dir /s /a:d /b "%parentfolder%"') do (
dir "%%F"|findstr /b "1 File(s)" >nul 2>&1 && echo %%F
)
This will list all subfolders with only one file in a parent folder .As it checks the string of the dir command output it should be altered if language settings/windows version provide different DIR command output.

Resources