How to copy files with only 1 extension and not double in command prompt

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

user935892

Posted 2018-08-23T09:21:24.400

Reputation: 61

1Rather than employ sophisticated sync products, it might be simpler to copy all the .jpg files and then delete the .jpg.jpg ones. – harrymc – 2018-08-23T09:27:28.103

Answers

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

Posted 2018-08-23T09:21:24.400

Reputation: 521

2

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

Pimp Juice IT

Posted 2018-08-23T09:21:24.400

Reputation: 29 425