DOS Batch FOR Loop to delete Files not Containing a String - for-loop

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)

Related

Displaying content of multiple .txt files with the same filename in all subdirectories

I have multiple files with the same filename under various subdirectories, for example:
c:\kjhsd\client.txt
c:\ekjjs\client.txt
c:\oiwnk\client.txt
I do not know the middle part of the path represented by random letters above, but the filename is always consistent.
I need a way to use this command: type client.txt (or) more client.txt but to display the content of all text files with the filename "client.txt" under any directory at the same time.
So if:
c:\kjhsd\client.txt contained: hello
c:\ekjjs\client.txt contained: helloworld
c:\oiwnk\client.txt contained: helloagain
After running the single command, I would see:
hello
helloworld
helloagain
Thanks in advance :)
from cmd.exe:
#for /r c:\ %a in ("client.txt") do #type "%~a"2>nul
From a Batch-File:
#for /r c:\ %%a in ("client.txt") do #type "%%~a"2>nul
Edit: as per the newline requirement:
#echo off
Pushd "c:\somedir"
for /f %%a in ('dir /b /s /a-d ^| find /i "client.txt"') do (
type "%%~a"2>nul
echo(
)
Popd
or if you want to see which file had what text:
#echo off
Pushd "c:\somedir"
for /f %%a in ('dir /b /s /a-d ^| find /i "client.txt"') do (
echo content: "%%~a"
type "%%~a"2>nul
echo(
)
Popd
The easiest approach is to use a for /D loop to find the different directories, and to let find /V "" return the contents of the files, because then a line-break is automatically appended to every file in case there is none in the file itself (opposed to type).
In Command Prompt do this:
#for /D %I in ("D:\some\root\*") do #(< "%~I\client.txt" find /V "") 2> nul
In a batch file do this:
#for /D %%I in ("D:\some\root\*") do #(< "%%~I\client.txt" find /V "") 2> nul
The 2> nul portion suppresses eerror messages if a directory does not contain a file called client.txt.

How to create file in partially known folder name using batch file

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

How to copy files. iterating whitespace directories - batch

Hello i want to create a batch file that copies the files from the current directory to another.
for /F "usebackq" %%b IN (`DIR /B /S ""`) DO #(
XCOPY %%b %1
)
So Far so good.
MY problem are the whitespaces in directories.
So when the name of a directory is /Dir whitespaces end/
it does not copy it. "File not found - Dir"
Start bat file with destination
CopyFiles.bat "I:\testFolder*.*"
How can i work around this feature?
Try this:
for /f "delims=" %%b in ('dir /b /s ') do xcopy "%%~b" "%1"
Important is 1) set "delims=" and 2) enclose for loop and other variables in double quotes.

batch script delete file containining certain chars

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"

Exclusion of files of certain extension

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.

Resources