Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move all files except some (file pattern) from a DOS command

Tags:

batch-file

dos

From a DOS command I want to move all files that do not match a file name pattern. Something like this:

For example I want to move all files that do not start with "aaa"

for %i in (*) do if not %i == aaa* move %i .\..
like image 313
eze1981 Avatar asked Nov 04 '10 15:11

eze1981


4 Answers

Robocopy is a possibility

robocopy source destination *.* /mov /XF aaa*.*

for options see here http://technet.microsoft.com/en-us/library/cc733145.aspx

like image 158
Gregory Adam Avatar answered Nov 19 '22 02:11

Gregory Adam


XCOPY is designed to work with 'exclude' lists... See below:

   dir /b /a-d "source"|findstr /b "aaa" >"%temp%\aaafiles.tmp"

   xcopy "source" "destination\" /exclude:%temp%\aaafiles.tmp /y

The first line performs a DIR (directory) listing of the source folder, listing files in bare format (/b) ignoring directory names (/a-d). The output is piped into the FINDSTR command.

FINDSTR looks at each filename and compares it's beginning (/b) with the string "aaa".

If the start of a filename matches the string "aaa", the filename is redirected (written) to the file aaafiles.tmp in the users TEMP directory.

The /b is vital because you don't want to exclude files such as theaaafile.txt.

The XCOPY command copies files from the source folder to the destination folder except files that are listed in aaafiles.tmp.

Prompting to overwrite existing files (/y) is turned off.

source and destination will need to be replaced your own foldernames.

like image 43
Paul Tomasi Avatar answered Nov 19 '22 00:11

Paul Tomasi


If you don't mind fiddling with the archive bit, you can use it to selectively copy and delete files based on a file mask.

Move (copy and delete) all files except those beginning w/"aaa" from current directory to "dest". May also specify full source path.

attrib +a *.*
attrib -a aaa*.*
xcopy /a [or /m] *.* dest
del /aa /q *.*  
like image 34
user1054300 Avatar answered Nov 19 '22 00:11

user1054300


One way you can do it is create a list of the files to move in a temporary file. Then use the file in with the for command. Generate the list using findstr.

> dir/b/a-d | findstr /v aaa* > "%temp%\@movelist"
> for /f %f in (%temp%\@movelist) do move %f ...

The first command gets a list of all files (with no directories) in the current directory and then pipes the list to findstr which excludes (/v) filenames that match the pattern and puts it in the file @movelist in the temp directory. The second command just takes those results so you may do what you will with them (move it).

There's probably a better way to do it in a single command without the temporary file, I just don't know how to write it. I'm not sure how to call the dir command from within the for command. AFAIK it only takes program files that exist, not builtin commands.

like image 43
Jeff Mercado Avatar answered Nov 19 '22 02:11

Jeff Mercado