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.
Related
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
)
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
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
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.
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
)