getting extra Thumbs.db file in destination location - batch-file

While i am executing the following statements:
for /f "Tokens=* " %%I in (' dir /b/a-d "%src_dir%\*.*"') do (
echo %%I>> %save_file%
)
i am getting Thumbs.db evrytime in my destination folder %save_file%..
Please suggest , how to exclude that .DB file.
Thanks

dir /b /a-d "%src_dir%\*" | findstr /v /e /i /l /c:"\thumbs.db" > %save_file%
Filter the lines that do not match (/v), at the end of the line (/e), ignoring case (/i) the literal (/l) \thumbs.db and send all the output to the indicated file.
edited to adapt to comments
( for /f "delims=" %%a in ('
dir /b /a-d "%src_dir%\*" ^| findstr /v /e /i /l /c:"\thumbs.db"
') do echo %%a
) > %save_file%
The only "problematic" part is the need to escape the pipe character (the reason for the ^)

Related

Echo only files with certain extension using DIR and FINDSTR

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.

Strict string matching locate file batch - Case sensitive

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

escaping " symbol used in findstr within a FOR statement

Yeah, I've tried the most popular available solutions(1)(2)
They didn't help much; just restated what I already knew.
This works:
#echo on
set var=APPENDTEXT
for /f "delims=" %%a in ('dir *.* /b /a-d') do ren "%%a" "%%~na%var%%%~xa"
pause
but then I try to refine it a bit like so
#echo on
set var=APPENDTEXT
for /f "delims=" %%a in ('dir *.* /b /a-d | findstr /v /i "\.bat$" ') do ren "%%a" "%%~na%var%%%~xa"
pause
so that I do not end up renaming the batch file itself. But then everything got messed up.
I've tried several approaches for escaping, none working quite like I want them to.
Additional Information: From what I gather, escaping " inside findstr is a problem when it is itself inside something else. I've
tried escaping with "" and with /" and with ^" to no avail. Am I doing
something wrong in these approaches?
('dir . /b /a-d | findstr /v /i "".bat$"" ')
('dir . /b /a-d | findstr /v /i \".bat$\" ')
('dir . /b /a-d | findstr /v /i ^".bat$^" ')
What is the correct way to escape it?
*
What I want it to do ?
Simply put,
When I run this.bat file inside a folder, I want all the files inside
it to be renamed with a APPENDTEXT (except the bat file itself) Example:
a.dat --> aAPPENDTEXT.dat pleasework.txt --> pleaseworkAPPENDTEXT.txt
You have escaped the findstr statement correctly, but the pipe | symbol still needs to be escaped. | findstr → ^| findstr
#echo on
set var=APPENDTEXT
for /f "delims=" %%a in ('dir *.* /b /a-d ^| findstr /v /i "\.bat$" ') do ren "%%a" "%%~na%var%%%~xa"
pause

Want to exclude specific files with dir findstr

I want to find all *.csv files within a folder-structure except 2 files
This is my code
for /f "tokens=*" %%i IN ('dir /b /s *.csv | findstr /v /i "\combinedold.csv" | findstr /v /i "\combined.csv"')
The 2 files are "combined.csv" and "combinedold.csv".
The basic command should be something like
dir /s /b /a-d *.csv | findstr /v /i /e /c:"\\combinedold.csv" /c:"\\combined.csv"
Now, to include it inside a for /f command, it is necessary to escape non quoted special characters (the pipe in this case), so it becomes
for /f "delims=" %%i in ('
dir /s /b /a-d *.csv ^| findstr /v /i /e /c:"\\combinedold.csv" /c:"\\combined.csv"
') do echo %%i

Batch: find all files with exclude part of the name

I need to find all * .avi files (even in subfolders). But I want to exclude files with specific part of the name (* demux.avi). Something like this:
FOR /R %%g IN (*.avi) DO (
if "%%g"=="*demux.avi" ( - THIS PART IS WRONG
echo "%%g" excluded
) else (
echo "%%g"
)
)
I expect results:
file1.avi
file1 demux.avi excluded
file2.avi
**
Try this:
FOR /F %%g IN ('dir /b /s "*.avi"^|find /i /v "demux"') DO ( whatever )
We are basically saying look for all avi files in this folder and subfolders and pipe them to find.
Use finds /V switch that finds all lines NOT containing "demux" and run your command on them.
Like this :
#echo off&cls
for /f "delims=" %%a in ('dir /b/a-d *.avi') do (echo %%a | find "demux" && echo %%a [Excluded] || echo %%a [OK])
In place of echo %%a [Excluded] or echo %%a [OK] you can do what you want (DEL, REN,MOVE,...)
Edit :
To just have the name of the file when your using /S option of DIR
#echo off&cls
for /f "delims=" %%a in ('dir /s/b/a-d *.avi') do (echo %%~na | find "demux" && echo %%~na [Excluded] || echo %%~na [OK])

Resources