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
Related
Just another simple question and probably duplicate of Windows batch script to search for specific files to delete
But I just can't figure it out.
In a batch file, I have the following:
for /f "delims=" %%a in ('dir /a-d /s /b *.tmp ^| findstr /e /r c:"*\.tmp" ') do echo "%%a"
In a folder on which I run the script are the files test.tmp and test.tmpl. I want the script to echo just test.tmp, but it doesn't echo anything. Any help is appreciated.
because of /r, * is interpreted as "zero or more of previous char". What is the previous char? You don't need * here. Just findstr /e ".tmp". Another /i may be a good idea.
break>test.tmp
break>test.tmpx
break>test.xtmp
for /f "delims=" %%a in ('dir /a-d /s /b *.tmp^| findstr /ie ".tmp"') do echo "%%a"
Try this instead:
for /f "delims=" %%a in ('dir /a-d /s /b *.tmp ^| findstr /e ".tmp" ') do echo "%%a"
Do not use /R as it uses search strings as regular expressions.
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!
for /f "tokens=* delims=" %%a in ('dir "recordings.txt" /s /b') do (
echo %%a
)
Is this the correct format to look on the Z: drive for files in the recordings.txt?
Tried powershell don't have permissions on the server
Contents of the file just have the file name / extension
3030009948_3030009912_df1389947f0fb80d62832122.sasf
The Directory structure of Z: is as follows
MM\dd\hh\mm\
recordings.txt is on the Desktop of my userprofile
I also need the paths of the found files
This should read recordings.txt on your desktop and create recordings-results.txt in the same place with the full path to every file inside it.
#echo off
dir /b /s /a-d "z:\" >"%temp%\results.tmp"
del "%userprofile%\desktop\recordings-results.txt" 2>nul
for /f "usebackq delims=" %%a in ("%userprofile%\desktop\recordings.txt") do (
echo finding "%%a"
findstr /i /c:"%%a" "%temp%\results.tmp" >>"%userprofile%\desktop\recordings-results.txt"
)
del "%temp%\results.tmp"
pause
This should be even faster:
#echo off
dir /b /s /a-d "z:\" >"%temp%\results.tmp"
findstr /i /g:"%userprofile%\desktop\recordings.txt" "%temp%\results.tmp" >"%userprofile%\desktop\recordings-results.txt"
del "%temp%\results.tmp"
pause
Dir is to look into a directory if you want to get a value from a file use :
for /f "tokens=*" %%a in ('type "Z:\recordings.txt"') do (
echo %%a
)
I am new to batch scripting. I am to write a batch script which will search for a sub directory and then compare the extension of the files present inside the directory.
For example, for the following structure:
D:\Batch_script\unknown_dir_name\destiantion1\dummy.txt
My script should first find the directory destiantion1 and then if it exist it should look for dummy.txt file.
Is it possible? can anyone please suggest me a way to do this.
Thanks
for /f "tokens=*" %%f in (
'dir /ad /s /b "d:\Batch_script\destiantion1"'
) do if exist "%%~f\dummy.txt" echo %%~f
For each directory named destiantion1 under d:\batch_script, if it contains a dummy.txt file, echo to console
EDITED - to adapt to comments
for /f "tokens=*" %%f in (
'dir /ad /s /b "d:\Batch_script\destiantion1"'
) do if exist "%%~f\dummy.txt" (
dir /a-d /b "%%~f\dummy.txt" 2>nul | find "dummy.txt" > nul && echo %%~f
)
for and dir commands do not differentiate upper and lower case at search, so, use a filter. If the string is found, no errorlevel is set and the code after && is executed.
Other alternative is to process the complete list of files searching for the adecuated folder/file
for /f "delims=" %%f in (
'dir /a-d /s /b "d:\batch_script\dummy.txt" ^| findstr /l /e /c:"\destiantion1\dummy.txt"'
) do echo %%~dpf
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.