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!
Related
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
I have a piece of code which runs through each line in a find.txt file and tries locate it. If it does not exist it will populate a output.txt file. Thing is, if a file is called "Egg.mp3" and in my find.txt has "egg.mp3" it counts that as if it found it? Now correct.. It did but i need something thats strict! Case sensitive even so that "Egg.mp3" is not the same as "egg.mp3" therefore to drop "egg.mp3" into my output.txt.
Does anyone have a solution to this? I searched around and found nothing that may help.
Batch code:
for /f "usebackq delims=" %%i in ("E:\find.txt") do IF EXIST "C:\Users\PC\Desktop\Lib\%%i" (echo "File Exists") ELSE (echo "C:\Users\PC\Desktop\Lib\%%i">> "C:\Users\PC\Desktop\output.txt")
pause
Windows does not differentiate case when dealing with file or folder names. So "egg.mp3" and "Egg.mp3" really are equivalent.
But if you still want to include file names that differ only in case, then you can do the following:
#echo off
set "folder=C:\Users\PC\Desktop\Lib"
set "output=C:\Users\PC\Desktop\output.txt"
pushd "%folder%"
>"%output%" (
for /f "usebackq delims=" %%F in ("e:\find.txt") do dir /b /a-d "%%F" 2>nul | findstr /xc:"%%F" >&2 || echo %folder%\%%F
)
popd
The following would be a lot faster (assuming you don't really need the path info in the output), but this nasty FINDSTR bug prevents the following from working properly - DO NOT USE!
#echo off
dir /b /a-d "C:\Users\PC\Desktop\Lib" >"e:\temp.txt"
findstr /LXVG:"e:\temp.txt" "e:\find.txt" >"C:\Users\PC\Desktop\output.txt"
del "e:\temp.txt"
If you have JREPL.BAT, then you can do the following instead:
#echo off
dir /b /a-d "C:\Users\PC\Desktop\Lib" >"e:\temp.txt"
call jrepl "e:\temp.txt" "" /b /e /r 0:FILE /f "e:\find.txt" /o "C:\Users\PC\Desktop\output.txt"
del "e:\temp.txt"
If you really need the path info in your output, then you can do the following:
#echo off
dir /b /a-d "C:\Users\PC\Desktop\Lib" >"e:\temp.txt"
jrepl "e:\temp.txt" "" /b /e /r 0:FILE /f "e:\find.txt" | jrepl "^" "C:\Users\PC\Desktop\Lib\" /o "C:\Users\PC\Desktop\output.txt"
del "e:\temp.txt"
Based off of a comment in this solution, this should do what you want:
#echo off
for /f "usebackq delims=" %%i in ("find.txt") do (
echo Checking for %%i...
dir /b /a-d "%%i"|find "%%i" >nul
if %errorlevel% == 0 (
echo "File Exists"
) ELSE (
echo "Not found"
)
)
Example of the base command in action:
D:\batch>dir /b /a-d "egg.mp3"|find "egg.mp3"
D:\batch>dir /b /a-d "Egg.mp3"|find "Egg.mp3"
Egg.mp3
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 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.
I want to create a windows batch file which lists all the empty sub-directories present under the user specified root directory.
Can anybody help regarding the same?
#echo off
for /d /r %1 %%A in (.) do (
dir /a /b "%%~fA" 2>nul | findstr "^" >nul || echo %%~fA
)
The above solution ignores Hidden folders. I've also been told that using both /D and /R options with FOR is bugged, though I've never had a problem with it.
#echo off
dir /a /b %1 2>nul | findstr "^" >nul || echo %%~fA
for /f "eol=: delims=" %%A in ('dir /s /ad /b %1') do (
dir /a /b "%%~fA" 2>nul | findstr "^" >nul || echo %%~fA
)
The 2nd solution that avoids FOR /D /R will include Hidden folders. But I believe it can fail if folder names contain Unicode.