Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

move files if not .jpg

Posted on 2006-11-22
11
Medium Priority
?
322 Views
Last Modified: 2013-11-18
Hi there,

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

Erik
0
Comment
Question by:fmew
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 8

Expert Comment

by:drydenhogg
ID: 17994838
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
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 17994895
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
 
LVL 6

Expert Comment

by:dave_moats
ID: 17996072
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:Raymun
ID: 17996709
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
 
LVL 4

Expert Comment

by:Raymun
ID: 17996755
and it's not case sensitive i.e. it will exclude jpg, JPG, Jpg, JpG, etc etc
0
 
LVL 4

Expert Comment

by:Raymun
ID: 17997128
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
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 17997149
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
 
LVL 4

Expert Comment

by:chhokra_expert
ID: 17997501
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
 

Author Comment

by:fmew
ID: 17998127
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
 
LVL 4

Accepted Solution

by:
Raymun earned 1000 total points
ID: 18000043
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
 

Author Comment

by:fmew
ID: 18007970
Hi Raymun,

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

cu
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Simple Linear Regression

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question