I have almost no experience in batch, but now I need a script to delete all files that contain certain characters in their names from a folder and its subfolders on Windows 64. I only did simple things in batch like
del "C:\TEST\TEST2\*.TXT"
But I have no idea how to perform the filtering I need.
Could someone help me to achieve this using batch?
EDIT more exactly, the question is "how to tell batch to include subfolders?"
The /s switch exists on quite a few commands (including del)
del /s "C:\TEST\TEST2\*.TXT"
The help for del /? says:
/S Delete specified files from all subdirectories.
Try this:
#echo off & setlocal
set "MySearchString=X"
for /f "delims=" %%i in ('dir /b /s /a-d ^| findstr /i "%MySearchString%"') do echo del "%%~i"
Set the variable MySearchString to the character or string to search and remove the echo command, if the output is OK.
You can also specify the MySearchString for the file name only:
#echo off & setlocal
set "MySearchString=T"
for /r %%a in (*) do for /f "delims=" %%i in ('echo("%%~na" ^| findstr /i "%MySearchString%"') do echo del "%%~fa"
Related
I need a batch file that counts certain file types in sub directories. In this case it's .txt and .xls files. At the end of the file it reports this back.
#echo off
REM get script directory
set scriptdir=%~dp0
REM change directory to script directory
cd /d %scriptdir%
setlocal
set txtcount=0
set xlscount=0
for %%x in ('dir *.txt /s ) do set /a txtcount+=1
for %%x in ('dir *.xls /s ) do set /a xlscount+=1
echo %txtcount% text files
echo %xlscount% .xls files
endlocal
pause
My batch file doesn't report back the correct count of files. I thought it might be continually counting but I've set the count variables to local so I'm not sure what's up.
There's three problems with your script:
You want to iterate over command output, so you need FOR /F
You're not using the "bare" /B format, i.e. DIR outputs more than just filenames
You're missing the closing single quote twice
Replace your loops with this:
FOR /F %%X IN ('DIR /S /B *.txt') DO SET /A "txtcount+=1"
FOR /F %%X IN ('DIR /S /B *.xls') DO SET /A "xlscount+=1"
You are very close, but you are missing the switch /B of the dir command, so the output contains a header and a footer, which are both included in the count. Therefore, change dir *.txt /s to dir *.txt /s /b (same for *.xls) and the count is going to be correct.
Besides that, there are typos: the closing ' is missing -- for %%x in ('dir *.txt /s') do .... In addition, the /F switch at for is missing, which is needed to capture the output of a command. (I assume these things are present in the true script as you would receive errors otherwise.)
Alternative Approach
To count the number of files, you could also do the following:
for /F "delims=" %%X in ('dir /B /S "*.txt" ^| find /C /V ""') do set "COUNT=%%X"
find /V "" searches for non-empty lines, hence all lines returned by dir /B /S are considered as matches; the /C switch simply lets find return the total amount of matching lines.
How can I delete files that do not end with the pattern .PDF_*.pdf ?
AA00A6E2.PDF
AA00A6E3.PDF
AA00A6E3.PDF_01.pdf
AA00A6E3.PDF_02.pdf
AA00A6E3.PDF_03.pdf
I have been trying all sorts of variations around the following syntax:
FOR %%F IN (%varFolderSource%\*.*) DO IF NOT "%%~xF" == "*_*" DEL /F /S "%%F"
But I can't seem to crack it.
%varFolderSource% is a folder path: C:\Temp etc.
I am running this in a Windows 7 batch file.
For /f "delims=" %A in ('dir /b /a-d ^|findstr /i /v /e /r "\.PDF_[a-z0-9]*\.pdf"') do echo %A
Type
for /?
findstr /?
In a batch file use %%A rather than %A at command prompt.
I want to create a 0 byte file names dblank in a specific directory C:\Users\myUser\*.data\.
echo. 2>"C:\Users\myUser\*.data\dblank.txt"
The * sign in the above command refers to any letters or numbers. I do not know. How can I refer to any letters or numbers in my batch code?
Maybe this:
setlocal enableextensions
for /D %%i in (C:\Users\myUsers\*.data) do copy nul "%%~i\dblank.txt"
endlocal
You can omit setlocal/endlocal if command extensions are already enabled (cmd /E:on).
This works on every existing *.data folder, if any.
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data') do echo. 2>"%%f\dblank.txt"
EDIT
Filter results:
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data^|findstr /r "\\[0-9a-zA-Z]*\.data$"') do (
echo. 2>"%%f\dblank.txt"
)
In a Windows cmd batch script named my.bat, I want to execute a command for all the files in the current directory except for the batch file my.bat.
I use below command in my.bat currently to run the command only for *.txt *.ppt, but really as new files of different extensions might be added to the folder and hence execution of this command by excluding one type of file extension (in my case *.bat) would be more readable/helpful.
FOR /F "delims=|" %%i IN ('dir /b *.txt *.ppt') DO echo %%i
Question is: How do I exclude that file alone with that particular file extension and make the for command execute on all files of all extensions except the excluded one?
FOR /F "tokens=* delims=|" %%i IN ('dir /b *.*') do if not %%~xi==.bat echo %%i
I have added tokens=* in as well otherwise you won't get full filenames if they have spaces.
To echo without the dot
setlocal enabledelayedexpansion
FOR /F "tokens=* delims=|" %%i IN ('dir /b *.*') do (
set e=%%~xi
set e=!e:.=!
echo !e!
)
This is providing that the file doesn't have any other dots, otherwise it will remove them too. This is a bit more sturdy than just removing the 4th character from the end though, as not all files have a 3 character extension.
You could pass the output of the dir /b command through findstr, like so:
FOR /F "delims=|" %%i IN ('dir /b ^| findstr /v /r "^my.bat$"') DO echo %%i
The /v option to findstr prints anything that doesn't match the parameter. The match is based on a regular expression that matches only lines that contain my.bat and nothing else.
I want to delete all the files in the current directory which do not contain the string "sample" in their name.
for instance,
test_final_1.exe
test_initial_1.exe
test_sample_1.exe
test_sample_2.exe
I want to delete all the files other than the ones containing sample in their name.
for %i in (*.*) do if not %i == "*sample*" del /f /q %i
Is the use of wild card character in the if condition allowed?
Does, (*.*) represent the current directory?
Thanks.
Easiest to use FIND or FINDSTR with /V option to look for names that don't contain a string, and /I option for case insenstive search. Switch to FOR /F and pipe results of DIR to FIND.
for /f "eol=: delims=" %F in ('dir /b /a-d * ^| find /v /i "sample"') do del "%F"
change %F to %%F if used in a batch file.
The answer from Aacini worked for me. I needed a bat file to parse the directory tree finding all files with xyz file extension and not containing badvalue anywhere in the path. The solution was:
setlocal enableDelayedExpansion
for /r %%f in (*.xyz) do (
set "str1=%%f"
if "!str1!" == "!str1:badvalue=!" (
echo Found file with xyz extension and without badvalue in path
)
)
setlocal EnableDelayedExpansion
for %i in (*.*) do (set "name=%i" & if "!name!" == "!name:sample=!" del /f /q %i)