Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch For loop exclude file name that contains

Tags:

batch-file

I have a simple batch file that will loop through all *Test.htm files and copy them. Some of the files will contain a string that I don't want to copy.

FOR /R "C:\" %%g IN (*Test.htm) DO  (
echo %%g
)

What I want in pseudo code:

@echo off
FOR /R "C:\" %%g IN (*Test.htm) DO ( 
   if %%g contains "Exclude" do nothing
else 
   copy...
)
like image 439
twifosp Avatar asked Apr 24 '13 13:04

twifosp


People also ask

What does %% do in batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %% g'in batch file?

%%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'.

What does %1 mean in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

What is %~ n0 in batch file?

%0 is the name of the batch file. %~n0 Expands %0 to a file Name without file extension.


1 Answers

For filename:

@echo off

FOR /R "C:\" %%g IN (*Test.htm) DO ( 
   (Echo "%%g" | FIND /I "Exclude" 1>NUL) || (
       Copy "%%g"...
   )
)

For File content:

@echo off

FOR /R "C:\" %%g IN (*Test.htm) DO ( 
   (Type "%%g" | FIND /I "Exclude" 1>NUL) || (
       Copy "%%g"...
   )
)
like image 198
ElektroStudios Avatar answered Sep 29 '22 06:09

ElektroStudios