move files if not .jpg

Hi there,

With a .cmd batchfile I want to move files from one dir to another if they dont have the extension jpg

Erik
fmewAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
RaymunConnect With a Mentor Commented:
Good question. I originally thought it was just your system specs but he same thing happened to me (on a Core 2 duo w/ 2GB ram) but at approx. 24000 files. I don't know what's causing it. Anyhow, I refined the code. The previous one didn't do any error checking like checking for two or four-letter extensions and it incorrectly excluded files with the specified extension anywhere in the extension, such as .jjjjjjpg. The following does:

@echo off
setlocal enabledelayedexpansion

set DirOriginal=%cd%
set Tmp=%DirOriginal%\tmp

:: --------- modify these ------------
set DirContainingFiles=Data
set DirToMoveTo=Moved
set ExcludesFile=%DirOriginal%\excludes.txt
:: -----------------------------------

cd %DirContainingFiles%

if not exist !DirToMoveTo! md !DirToMoveTo!

for /f "tokens=1,2 delims=." %%a in ('dir /b') do (
      if not %%b.==. (
            echo %%b>%Tmp%
            findstr /i /x /g:%ExcludesFile% %Tmp% > nul
            if not !errorlevel!==0 move /y %%a.%%b %DirToMoveTo%\%%a.%%b
      )
)

cd %DirOriginal%
del %Tmp%

endlocal

In the excludes file you can list all the extensions to exclude, one per line e.g.

jpg
gif
bmp
ext
...and so on
0
 
drydenhoggCommented:
The normal XCOPY command can do this but not in a very neat way,

XCOPY c:\yoursource\*.* d:\yourdestination /EXCLUDE:C:\yourexcludedir\yourexcludefile.txt

the excludefile.txt is a txt file containing one line.

.JPG


Only thing to be wary of is that If any part of the absolute path for each source file matches a line in the exclude file, the file is not copied, so if the files were in a directory called "something.jpg" this wouldnt work.
0
 
Lee SavidgeCommented:
Hi,

To move a file and exclude a wildcard isn't possible with standard windows batch language. There is probably a way to do it using a FOR loop with a certain file types list but I don't have the syntax to hand. To move them in a hacky way then something like this will work in a batch file:

@echo off
attrib +h c:\images\*.jpg
xcopy c:\images\*.* c:\images2\ > NUL
del c:\images\*.* /a-H /Q > nul
attrib -h c:\images\*.jpg


This batch file will hide all jpg's in a certain dir (in my example I used an images dir on my PC). I then copy using xcopy as by default xcopy doesn't copy hidden files. You have to tell it to copy hidden ones if you want that. Then I delete them from the source directory and exclude hidden files from the delete command. Both the copy and delete have their outputs suppressed by redirectly the output to NUL. Then you alter the attributes to unhide the hidden jpgs. Hacky and not very neat but it meets your requirements.

Regards,

Lee

0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
dave_moatsCommented:
An alternative is to look at the results of the dir /b command and then only move the files that do not have a jpg or JPG file extension (This is case sensitive).

[Start Code]

@echo off

:: set the source and target directories
:: replace these two variable values with your directory paths
:: if your paths have spaces in them, I would use the short name instead of using quotes
:: e.g. c:\program files could be c:\progra~1
set srcDir=C:\begin\replacethis\
set trgDir=C:\end\replacethis\

:: look in the source directory and only move the files we care about
for /f "tokens=1,2 delims=." %%j in ('dir %srcDir% /b') do (
   if not "%%k"=="jpg" (
      if not "%%k"=="JPG" (
         xcopy %srcDir%%%j.%%k  %trgDir%
      )
   )
)

[End Code]

HTH
Dave
0
 
RaymunCommented:
Hello fmew,

Try this:

@echo off
setlocal enabledelayedexpansion

cd OriginalDir

set ExcludeExt=jpg
set OtherDir=folder

if not exist !OtherDir! mkdir !OtherDir!

for %%a in (*.*) do (
      set name=%%a
      echo !name:~2!>..\tmp

      findstr /I /C:!ExcludeExt! ..\tmp > NUL

      if !errorlevel!==1 move /y %%a !OtherDir!\%%a
)

del ..\tmp

cd ..

endlocal
0
 
RaymunCommented:
and it's not case sensitive i.e. it will exclude jpg, JPG, Jpg, JpG, etc etc
0
 
RaymunCommented:
Or maybe you'll like this one better. You can specify multiple exclude extensions:

@echo off
setlocal enabledelayedexpansion

set DirOriginal=%cd%
set DirContainingFiles=Data
set DirToMoveTo=Moved
set ExcludeExt=[jpg][gif]

cd !DirContainingFiles!

if not exist !DirToMoveTo! mkdir !DirToMoveTo!

for %%a in (*.*) do (
      set name=%%a
      echo !name:~2!>!DirOriginal!\tmp

      findstr /i /r !ExcludeExt! !DirOriginal!\tmp > nul

      if not !errorlevel!==0 move /y %%a !DirToMoveTo!\%%a
)

cd !DirOriginal!
del tmp

endlocal
0
 
Lee SavidgeCommented:
Hi,

Raymun, that's pretty cool. I knew there was a way of doing this with a FOR loop. Hats off to you!

Regards,

Lee
0
 
chhokra_expertCommented:
i know you want a DOS batchfile, but i have to say how ridiculously easy this'd be under bash :) (or if you have cygwin)

this one line command should do it for you:

find . \! -iname "*.jpg" -type f -exec cp '{}' target_dir \;

where target_dir is the directory you want to place the files in (works best if target_dir is not a sub-dir of current dir).

e.g. you want to move all non jpg files from folder1 to folder2

ls -laF
# ./folder1/
# ./folder2/

cd folder1
find  . \! -iname "*.jpg" -type f -exec cp '{}' ../folder2 \;

explanation of command: find all files that don't end in .jpg (-iname) makes it case-insensitive, ensure that it is a file and not a dir (-type f) and for each such file, execute the command "cp '{}' ../folder2" where '{}' is replaced by the name of the file found. this works for subdirs of folder1 as well.
0
 
fmewAuthor Commented:
Hi Everybody,

Thx for all your comments. Iám busy, so it will not be till friday for me to look at it.
Its the thing that Raymun does Iam looking for.

Hi chhokra_expert,
I dont know bash :) (or cygwin, but this looks good to.

Hi Raymun,
Looks good again. I will look at it. You do this:  ExcludeExt=[jpg][gif][etc]
Is it possible to do something like IncludeExt=[jpg] and then move every file that is not included?
I did the other code from you, that works fine, but I had a dir with 90000 files. I put 4000 files in one dir (you know, folder0 folder1 etc). After 4 folders (=something less then 16000 files) the code just stops. I can live with that, but do you know what causes this limitation?


See you
Erik
0
 
fmewAuthor Commented:
Hi Raymun,

This is fun with you. Your good.
I'll raise you to 500

cu
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.