开发者

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

开发者 https://www.devze.com 2023-01-23 18:16 出处:网络
Fro开发者_运维问答m a DOS command I want to move all files that do not match a file name pattern.

Fro开发者_运维问答m 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 .\..


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.


Robocopy is a possibility

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

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


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 *.*  


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.


In some cases it can be made more simple. For example, I had to copy recursively a bunch of directories but excluding all images (png and bmp files), so I simply created an excludeList.txt file containing:

.png
.bmp

and run

 xcopy /S /I <source> <dest> /exclude:c:\excludeList.txt 

It will match any file or directory containing .png, but not necessarily ending by .png. (I did not investigate if smart use of wildcards or regular expressions are possible). It does not handle your particular example (for which you have already a good answer) but it solved my problem, and this page is what I found when I googled in search of a solution :)


Not ideal, but moving all files to the destination and moving the files back to the source is a fast way with actual move operation (no copies). Of course this assumes there are no files in destination matching the wildcard.

move source\*.* destination\ && move destination\aaa*.* source\

0

精彩评论

暂无评论...
验证码 换一张
取 消