Want to exclude specific files with dir findstr - batch-file

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

Related

How to copy folders with Batch file which does not start with an underscore?

I would like to copy all files and folders which don't start with an underscore in its file/directory name using a batch file. How can I achieve that?
From cmdline:
For /f %i in ('dir /b ^| findstr /v /b "_"') do echo %i
In a batch file:
For /f %%i in ('dir /b ^| findstr /v /b "_"') do echo %%i
Findstr /v excludes specific items specified in the double quotes.
To do it through all subfolders add /s
For /f %%i in ('dir /s /b ^| findstr \[^_][^\\]*$') do echo %%i

.bat to search for directory with certain name and set into a copy path

I'm trying to search for a folder (c:\test) in a certain directory that has the word "current" in it. And then i would like to copy from a folder inside it (c:\test\current\first).
Any help would be much appreciated. I have done my research but so far I have only managed to do xcopy, but not the first 2. Sorry I'm relatively new to this.
I am not sure of the structure, but seems you want to do:
from Batch file:
For /f "delims=" %%i in ('dir /S /B /AD "N:\8\Installation Release" ^| findstr /i "web" ^| findstr /i "current"') do echo %%i
From Cmdline (Console):
For /f "delims=" %i in ('dir /S /B /AD "N:\8\Installation Release" ^| findstr /i "web" ^| findstr /i "current"') do echo %i
So as per your last comment, this is more or less what you would need to perform the xcopy:
For /f "delims=" %%i in ('dir /S /B /AD "N:\8\Installation Release" ^| findstr /i "web" ^| findstr /i "current"') do (
xcopy "%%i"* /D /C /Q /R /Y /I /S "D:\Abc" & goto :eof
)

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.

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

C opy file3.txt to f older down level three, file4.txt to four

I have a directory like this
C:\folder11\
C:\folder21\
C:\folder11\folder12\
C:\folder11\folder12\folder13\
C:\folder21\folder22\folder23\
C:\folder31\folder32\folder33\
C:\folder11\folder12\folder13\folder14\
C:\folder11\folder12\folder13\folder14\folder15\
I have file1.txt, file2.txt, file3.txt, file4.txt, file5.txt,...
I want to have this.
C:\folder11\file1.txt
C:\folder21\file1.txt
C:\folder11\folder12\file2.txt
C:\folder11\folder12\folder13\file3.txt
C:\folder21\folder22\folder23\file3.txt
C:\folder31\folder32\folder33\file3.txt
C:\folder11\folder12\folder13\folder14\file4.txt
C:\folder11\folder12\folder13\folder14\folder15\file5.txt
Could you Please make for file3.txt ,and file4.txt,... copied to each specific folders in that current directory?
Thank You Very Much.......
Try:
COPY file path
Like this but for all of them
COPY "file5.txt" "C:\folder11\folder12\folder13\folder14\folder15\"
This assumes a root directory is the base level. Each line has an echo copy command which will just show you on the console what it will copy. Remove the echo from each line to perform the file copy.
#echo off
pushd "c:\"
for /f "delims=" %%a in ('dir /ad /b /s ^| findstr \\.*\\ ^| findstr /v \\.*\\.*\\ ') do echo copy "file1.txt" "%%~fa"
for /f "delims=" %%a in ('dir /ad /b /s ^| findstr \\.*\\.*\\ ^| findstr /v \\.*\\.*\\.*\\ ') do echo copy "file2.txt" "%%~fa"
for /f "delims=" %%a in ('dir /ad /b /s ^| findstr \\.*\\.*\\.*\\ ^| findstr /v \\.*\\.*\\.*\\.*\\ ') do echo copy "file3.txt" "%%~fa"
for /f "delims=" %%a in ('dir /ad /b /s ^| findstr \\.*\\.*\\.*\\.*\\ ^| findstr /v \\.*\\.*\\.*\\.*\\.*\\ ') do echo copy "file4.txt" "%%~fa"
for /f "delims=" %%a in ('dir /ad /b /s ^| findstr \\.*\\.*\\.*\\.*\\.*\\ ^| findstr /v \\.*\\.*\\.*\\.*\\.*\\.*\\') do echo copy "file5.txt" "%%~fa"
pause
popd

Resources