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

早过忘川 提交于 2020-01-01 04:50:09

问题


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

回答1:


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.




回答2:


Robocopy is a possibility

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

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




回答3:


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



回答4:


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.




回答5:


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 :)




回答6:


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\



来源:https://stackoverflow.com/questions/4098444/move-all-files-except-some-file-pattern-from-a-dos-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!