0

I'm trying to move files from all sub directories that do not have extension .bz2 in the end: enter image description here

It should move all extensions which aren't ending with (.bz2) I tried this:

for /R C:\AppServ\fastdl %f in (*) do @if not exist (*.bz2) move /Y *           C:\AppServ\fastdl\not_bzip >NUL

But this script is trying to move folders and is also failing like: enter image description here

And if I put %f instead of move /Y * it will move the .bz2 files.

Mike
  • 13
  • 2

2 Answers2

0

You probably want something like this:

For /R "C:\AppServ\fastdl" %f In (*) Do @If %~xf NEq .bz2 (@Move /Y "%~f" "C:\AppSrv\not_bzip">Nul)

Please note, I changed the move to directory, you will need to create it first yourself and make sure it is somewhere other than a subdirectory of the folder tree you are scanning.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • It is very dangerous to manipulate a directory (tree) inside of a `for` (`/R`) loop -- see this question: [At which point does `for` or `for /R` enumerate the directory (tree)?](http://stackoverflow.com/q/31975093) Using `for /F "delims=" %F in ('dir /B /S "C:\AppServ\fastdl\*"') do ( ... )` (similar to [Noodles' answer](http://stackoverflow.com/a/39761641)) ensures the directory tree is enumerated *before* it is manipulated. By the way, you should use quotes like `if "%~xf" NEQ ".bz2" ` to avoid trouble with files without an extension... – aschipfl Sep 29 '16 at 16:34
  • I think the word dangerous is a little over dramatic, they're moving files, not deleting them. I understand the fall downs with this method, but what really are the chances that the tree is being dynamically changed throughout this running script. Also similar can be said for the possibility of files with no extension, whilst there's a small chance it's unlikely especially when the OP provided a quick sample in their post. Even using the dir command, I'd still prefer to stipulate **-d** and **-s -h**, bypassing system & hidden files too. It's robust enough for general use, not for all use. – Compo Sep 29 '16 at 17:04
0
For /f "delims=" %A in ('dir /b /s "c:\windows\syswow64" ^| Findstr /v /I /c:".dll"') Do Echo %A

List all files except dll files.

  • Actually the search string should be `.dll$` to anchor the match to the end of the file names; otherwise, something like `this.is.not.a.dll.file.txt` would match erroneously... – aschipfl Sep 29 '16 at 16:30