escaping " symbol used in findstr within a FOR statement - batch-file

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

Related

Batch output filename to variable

I am currently trying to use batch to grab a specific file name and then place it in a variable since the file name may change but the "client" or "mui" portion will not.
These two lines are what I tried first. using dir to search a path and /b to grab only the name I then filter with "client" or "mui" and that works for finding just the name. But trying to pipe that into a variable didn't work.
dir "\\server\path\here" /b | Find "client" | set Client
dir "\server\path\here" /b | Find "mui" | set MUI
So I tried my hand at these other items I found during some googling but these didn't work either.
FOR "tokens=*" %%a in ('DIR "\\server\path\here\"' /b) do (SET OUTPUT=%%a)
for /f "tokens=*" %%i in ('dir \\\server\path\here /b | Find client') do #echo %%i
for /f %%a in ('dir \\server\path\here /B | find "client"') do set FileCount=%%a
What am I missing or doing wrong here?
For those who come here later I ended up using
for /f "tokens=*" %%i in ('dir \\server\path\here /b ^| Find "client"') do (Set BaseClient=%%i)
FOR "tokens=*" %%a in ('DIR "\\server\path\here\"' /b) do (SET OUTPUT=%%a)
should work (if you look for the only or the last file)
for /f "tokens=*" %%i in ('dir \\server\path\here /b ^| Find "client"') do #echo %%i
you forgot to quote the find string (maybe dir /b /a-d ... and find /i "client" might be a good idea), and the | has to be escaped.
for /f %%a in ('dir \\server\path\here /B ^| find "client"') do set FileCount=%%a
Filecount? You might want dir /b /a-d ... ^| find /i /c "client" here.

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.

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

getting extra Thumbs.db file in destination location

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 ^)

Getting File not found error when running Batch command to delete empty folders and subfolders

For some reason I am getting an "File Not Found" Error when I run this DOS command to delete empty folders and subfolders. It looks correct as far as I can tell. Does anyone have any suggestions?
for /f "delims=" %%x in (dir /s /b /ad ^| sort /r') do rd "%%x" 2>NUL
You are missing a tick (') in front of dir. Try this:
for /f "delims=" %%x in ('dir /s /b /ad ^| sort /r') do rd "%%x" 2>NUL
This is cleaner
for /f "delims=" %%x in ('dir /b /ad') do echo.rd /s /q "%%x"
Remove the echo. ONLY if the results appear to be correct.

Resources