1

I need to copy only .jpg and not .jpg.jpg from a particular folder.

When I do a copy c:\data\*.jpg D:\backup\ both .jpg and .jpg.jpg named files are copied over and I'm not sure how to omit the double extension named files from the copy operation.

Note: I'm not allowed to rename or delete those .jpg.jpg files

2 Answers2

1

You can use a for loop to iterate files in the directory and use variable substitutions to check that each iterated "file name part only without it's own extension" does not contain another extension (.jpg). Files that do not contain an additional extension within their file name will be copied with the xcopy command using conditional if logic accordingly.

Batch Script

@ECHO ON

SET "srcPath=c:\data"
SET "copyPath=D:\backup"

for %%a in ("%srcPath%\*.jpg") do (
    for %%b in ("%%~dpna") do if [%%~xb]==[] XCOPY /F /Y "%%~a" "%copyPath%\"
    )
PAUSE
EXIT

Further Resources

  • For
  • Variable Substitutions (FOR /?)

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    
  • If

  • XCOPY
0

Using powershell instead of command prompt:

Copy-Item C:\data*.jpg -Destination D:\backup -Exclude "*.jpg.jpg"

This is of course assuming that you have a lot of files named data[something].jpg in C: and not a directory tree with images in it.

If you want a recursive copy of jpgs in C:\Data\ without files ending in .jpg.jpg:

$source = 'C:\data'
$dest = 'D:\backup'
$exclude = @('*.jpg.jpg')
Get-ChildItem $source -Recurse -Exclude $exclude -Filter *.jpg | Copy-Item -Destination {Join-Path $dest $_.FullName.Substring($source.length)}

(shamefully stolen and modified from this answer on SO)

Baldrickk
  • 570
  • 1
  • 3
  • 11