3

first of all im beginner. i want to create batch file to search through specific folder (including all it subfolder) and copy all file inside it except those which filename contain some specific string,this is what i have so far

set now=fish        
set logDirectory="C:\Users\paiseha\Desktop\bb\"      
for /r %logDirectory% %%i IN (*%now%*.*) do (          
rem copy process goes here            
)       

let say i have 3 file in it

  C:\Users\fareast\Desktop\bb\one.txt  
  C:\Users\fareast\Desktop\bb\twofishtwo.txt  
  C:\Users\fareast\Desktop\bb\three.txt  

so i want to copy file one.txt and three.txt only, but instead it copy only the second one,i know its because of *%now%*.* so how can i invert it so that it does the other way around, help me pls, thanks in advance

paiseha
  • 169
  • 1
  • 3
  • 10

2 Answers2

5

try:

@ECHO OFF &setlocal
set "now=fish"        
set "logDirectory=C:\Users\paiseha\Desktop\bb"      
for /f "delims=" %%a in ('dir /a-d/b/s "%logDirectory%"^|findstr /riv "^.*[\\][^\\]*%now%[^\\]*$"') do (          
  rem copy process goes here            
)

EDIT: The \ character is represented as [\\] instead of \\ because of a quirk on how Vista FINDSTR regex escapes \. Vista requires \\\\, but XP and Win 7 use \\. The only representation that works on all platforms is [\\]. See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.

Community
  • 1
  • 1
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Consider a path containing `%now%`... Would work with the directory/exclude-string combo specified, but not in the general case :( soz... – Magoo Jul 11 '13 at 06:43
  • thanks for the reply, the code u give loop thru "C:\Users\fareast\Desktop\" not "C:\Users\paiseha\Desktop\bb\", why is that? – paiseha Jul 11 '13 at 07:04
2
for /f "delims=" %%a in ('dir /a-d/s/b "%logDirectory%" ') do echo %%~nxa|findstr /i /L "%now%" >nul&if errorlevel 1 ECHO COPY "%%a"

should work for you.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • why does it loop thru parent directory as well? i meant instead of loop through "C:\Users\paiseha\Desktop\bb\" as i set it loop through directory "C:\Users\paiseha\Desktop\" – paiseha Jul 11 '13 at 07:06
  • @paiseha : same edit as @Endoro required - quote moved in `set log...` to enclose `var=string` instead of `var="quoted string"`. The first assigns the value without trailing spaces (if present) whereas the second will assign a quoted-value AND any strat trailing spaces on the line to `var`. – Magoo Jul 11 '13 at 07:13
  • +1, But the FINDSTR search should probably use the `/L` literal option in addition to the `/I` option. Without `/L` the solution will fail if `now` is something like `.abc` because the `.` will match any character. – dbenham Jul 11 '13 at 11:16
  • Another option is to use `FIND /I` instead of `FINDSTR /IL`. Also it is shorter to use `|| copy "%%a"` instead of `& if errorlevel 1 copy "%%a"`... – dbenham Jul 11 '13 at 11:49
  • @dbenham Yeah - fair enough on the /L switch - edited-in. As for `||` vs `&&` - I never use them, but only because I can never remember which is which is what... – Magoo Jul 11 '13 at 14:32