Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file : copy all file except those its name contain some substring

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

like image 683
paiseha Avatar asked Sep 15 '25 15:09

paiseha


2 Answers

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.

like image 113
Endoro Avatar answered Sep 17 '25 08:09

Endoro


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.

like image 35
Magoo Avatar answered Sep 17 '25 09:09

Magoo