billious said:
But if you want to exclude a directory _AND ALL OF ITS SUBDIRECTORIES_
then
[04a] set pathname=%filename:~0,-1%
doubleJ said:
[01]@echo off
[02]setlocal enabledelayedexpansion
[03]for /r \\Audioserver\Audio2\BatchedMasters %%i in (.) do (
[04] set filename=%%i
[05] set pathname=!filename:~0,40!
[06] set exclusion=0
[07] if !pathname!==\\Audioserver\Audio2\BatchedMasters\Trac set
exclusion=1
[08] if !pathname!==\\Audioserver\Audio2\BatchedMasters\Jobs set
exclusion=1
[09] if !exclusion!==0 (
[10] set filename=!filename:~14,-2!
[11] set filename=!filename:\=_!
[12] RAR a \\audioserver\audio1\backup\audio2\!filename!.rar %%i\*.*
[13] )
[14])
[15]goto :eof
Notice a difference in pathname...
~0,-1 did not exclude directories, but ~0,42 did. It needed to exclude
things that started with !filename!.
In this example, I have to make sure that each exlusion line is 40
characters long. Is there an easy way to make that dynamic (Trac
should actually be Tracks, etc...)? I could set a variable for each
excluded directory, but that's lame.
One thing that I noticed, I cannot do...
if !filename:~0,42!==
It says that 42! is unexpected.
JJ
OK - a few things here.
First is a matter of protocol. End-posting (tagging comments to the END of a
message, not the top) is standard for usenet. It aids others to follow a
discussion without scrolling UP to the next point and DOWN to read that
point. Some people will automatically ignore threads that don't follow the
protocol - which reduces and delays replies.
Next:
the set varname=%varname2:~0,-1% was intended to trim the trailing "." from
the directoryname provided by FOR /R. This means that if you did not
terminate the directory name-to-match with a "\" then the IF would not match
!pathname! to directory-name-without-terminal-\
Since you haven't posted the failed code, only your modifications, it's not
possible to determine whether this was actually the case.
Nevertheless, the code I posted was bogus. It wouldn't work properly. Below
is a replacement.
Given a directory structure, requiring to
* exclude ...excludeme, but not its subdirectories
* exclude ...excludetree AND its subdirectories
C:\106x\xdtest\excludeme
C:\106x\xdtest\excludetree
C:\106x\xdtest\excludetree2
C:\106x\xdtest\includeme
C:\106x\xdtest\excludeme\andnotmeeither
C:\106x\xdtest\excludeme\butnotme
C:\106x\xdtest\excludetree\dumpme
C:\106x\xdtest\excludetree\dumpmetoo
C:\106x\xdtest\includeme\andme
C:\106x\xdtest\includeme\andmetoo
----- batch begins -------
[1]@echo off
[2]setlocal enabledelayedexpansion
[3]for /r c:\106x\xdtest %%i in (.) do (
[4]REM Note REM works in a do ( ... ) construct - :: does not
[5]REM Grab the directoryname to ydn
[6]set ydn=%%i
[7]
[8]REM initialise yxd - eXclude-Directory flag
[9]set yxd=N
[10]
[11]REM exclude c:\106x\excludeme ONLY - not its subdirectories
[12]if /i %%i==c:\106x\xdtest\excludeme\. set yxd=Y
[13]
[14]REM exclude c:\106x\xdtest\excludeTREE AND its subdirectories
[15]echo %%i|find /i "c:\106x\xdtest\excludeTREE\" >nul& if not errorlevel 1
set yxd=Y
[16]
[17]if !yxd!==N echo PROCESS %%i
[18]if not !yxd!==N echo BYPASS %%i
[19])
------ batch ends --------
Lines start [number] - any lines not starting [number] have been wrapped
and should be rejoined. The [number] that starts the line should be removed
which yields the following output:
PROCESS c:\106x\xdtest\.
BYPASS c:\106x\xdtest\excludeme\.
PROCESS c:\106x\xdtest\excludeme\andnotmeeither\.
PROCESS c:\106x\xdtest\excludeme\butnotme\.
BYPASS c:\106x\xdtest\excludetree\.
BYPASS c:\106x\xdtest\excludetree\dumpme\.
BYPASS c:\106x\xdtest\excludetree\dumpmetoo\.
PROCESS c:\106x\xdtest\excludetree2\.
PROCESS c:\106x\xdtest\includeme\.
PROCESS c:\106x\xdtest\includeme\andme\.
PROCESS c:\106x\xdtest\includeme\andmetoo\.
Noting that this procedure uses NEITHER filename NOR pathname - even if yDN
is established in [6]. You will probably need to use it to process the "\"
to "_" for your application - but you know how to do that...
* Note the terminal "\" on the directoryname on [15] This makes sure that
c:\106x\xdtest\excludetree is bypassed - but that
c:\106x\xdtest\excludetree2 is processed
* Note the terminal "\." on [12] which selects precisely THAT directory and
no other.
* Comments
The official comment-indicator is REM. Many users use the double-colon
structure as it's easier to type and less clumsy. HOWEVER, it fails in a
parenthesised-do.
* On style:
I use variable names that are three letters, starting with "y". It may be
cryptic, but it allows the contents of variables I have established by
simply issuing the command
SET Y
(which may be followed by a PAUSE command if convenient)
- no Microsoft-established variables begin "y" so I can easily isolate MY
variables.
* On IF !varname:~m,n!==...
does not work.
True. It's a quirk.
IF "!varname:~m,n!"=="..."
works though