Move all files except some (file template) from the DOS command

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

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

for %i in (*) do if not %i == aaa* move %i .\.. 
+7
dos batch-file
source share
5 answers

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

  dir /b /ad "source"|findstr /b "aaa" >"%temp%\aaafiles.tmp" xcopy "source" "destination\" /exclude:%temp%\aaafiles.tmp /y 

The first line executes the DIR (directory) list of the source folder, listing the files in bare format ( / b ), ignoring the directory names ( / ad ). The output is sent to the FINDSTR command.

FINDSTR looks at each file name and compares its beginning ( / b ) with the string "aaa".

If the beginning of the file name matches the string "aaa", the file name is redirected (written) to the aaafiles.tmp file in the TEMP directory.

/ b is vital because you do not want to exclude files such as theaaafile.txt.

The XCOPY command copies files from the source folder to the destination folder, except for the files listed in the aaafiles.tmp file.

The request to overwrite existing files ( / y ) is disabled.

source and destination must be replaced with your own names.

+13
source share

One way to do this is to create a list of files to move in a temporary file. Then use the file with the for command. Create a list using findstr .

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

The first command gets a list of all files (without directories) in the current directory, and then passes the list to findstr , which excludes ( /v ) the names of the files that match the pattern, and puts it in the @movelist file in the temp directory. The second team simply accepts these results, so you can do what you will with them (move).

Probably the best way to do this in one command without a temporary file, I just don't know how to write it. I am not sure how to call the dir command from the for command. AFAIK only uses program files that exist, not built-in commands.

+4
source share

Robocopy is an opportunity

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

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

+4
source share

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

Move (copy and delete) all files except those starting with "aaa" from the current directory to "dest". May also indicate the full source path.

 attrib +a *.* attrib -a aaa*.* xcopy /a [or /m] *.* dest del /aa /q *.* 
+3
source share

In some cases this can be made simpler. For example, I had to copy many directories recursively, but excluding all images (png and bmp files), so I just 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 in .png . (I have not investigated whether the use of wildcards or regular expressions is possible intelligently). It does not handle your specific example (for which you already have a good answer), but it solved my problem, and this page is what I found when I searched googled in search of a solution :)

+2
source share

All Articles