0

I was searching a lot of times how to copy all the files of different extensions from a folder and all its subfolders to a new folder without copying the folders itself using Batch File. I mean ONLY FILES.

I have a very massive(nested) directory structure I mean folder inside folders. I tried Robocopy but it didn't work. Then I found a code having XCOPY. I also tried C# but it takes too much time to complete.

If you have any other suggestion please recommend here.

1
  • What OS, what files? You should think of this as two steps: identify the files to move, then move them. This way means the method matters less, and you have more control over what is selected. Also, there are questions here going back a long time about how to move files of one type, so your research isn't complete. Jun 5, 2020 at 20:01

2 Answers 2

0

I would go with PowerShell if you're on anything resembling a modern Windows sytem. The simplest version of what you're asking:

$source = 'C:\Users\source'
$dest   = 'C:\Users\destination'
$filter = '*.txt'

Get-ChildItem $Source $filter -recurse | Copy-Item -Destination $dest

To filter items by date, you pipe the results of Get-ChildItem to Where-Object (aliases: Whereor?) before piping to Copy-Item.

$Cutoff = [DateTime]'3/1/20'
Get-ChildItem $Source $filter -recurse |
   Where CreationTime -gt $CutOff |
      Copy-Item -Destination $dest

The date-related properties of an IO.FileSystem object are:

  • CreationTime
  • LastAccessTime
  • LastWriteTime

You can use any of the Comparison Operators.

For a recurring operation, you'll probably want to calculate the cutoff from the current date:

  • Since Midnight: (Get-Date).Date
  • Last 24 hours: (Get-Date).AddDays(-1)
  • Midnight, day before (Get-Date).AddDays(-1).Date
5
  • its working thanks, i can't upvote bcz i have only 1 rep. but what if we want to copy files from a certain date ? as i did in my answer below...
    – afnan-a
    Jun 7, 2020 at 7:09
  • by creationTime
    – afnan-a
    Jun 8, 2020 at 4:23
  • Yes its working, thanks... is there any way to run this code after every 24 hours?just like we run batch file in windows schedular
    – afnan-a
    Jun 8, 2020 at 5:16
  • Hven't worked with that personally, but yes, have seen posts about running PowerShell tasks through Task Scheduler. First step is saving the code in a .ps1 file, I know that! :D Jun 8, 2020 at 5:27
  • i tried just like this and it worked Get-ChildItem 'C:\Users\afnan\Desktop\source' '*.txt' -recurse | Where CreationTime -gt '06/08/20' | Copy-Item -Destination 'C:\Users\afnan\Desktop\destination'
    – afnan-a
    Jun 8, 2020 at 5:29
0

Then I found a solution, and I think its very easy and fast then any other method.

@ECHO OFF

SET "source=C:\Users\source"
SET "dest=C:\Users\destination"
SET "FileList=*.txt"

SET "dupCnt=1"

FOR /R "%source%" %%A IN (%FileList%) DO (
    IF NOT EXIST "%dest%\%%~NXA" (
        XCOPY /F /Y "%%~FA" "%dest%\" && IF EXIST "%%~FA" /Q /F "%%~FA"
    ) ELSE (
        CALL :DupeRoutine "%%~FA" "%%~NA" "%%~XA"
        )
    )
GOTO :EOF

:DupeRoutine
IF EXIST "%dest%\%~2_(%dupCnt%)%~3" (
    SET /A dupCnt=%dupCnt%+1
    CALL :DupeRoutine "%~1" "%~2" "%~3"
) ELSE (
    IF NOT EXIST "%dest%\%~2_(%dupCnt%)%~3" ECHO F | XCOPY /Y /F "%~1" "%dest%\%~2_(%dupCnt%)%~3" /Q /F "%~1"
    SET "dupCnt=1" 
    )
GOTO :EOF

Here is another thing, if you want to copy files after a certain date then put date inside this query like this:

On line 11

XCOPY /D /D:06-05-2020 /F /Y "%%~FA" "%dest%\" && IF EXIST "%%~FA" /Q /F "%%~FA"

On line 23

IF NOT EXIST "%dest%\%~2_(%dupCnt%)%~3" ECHO F | XCOPY /D /D:06-05-2020 /Y /F "%~1" "%dest%\%~2_(%dupCnt%)%~3" /Q /F "%~1"

The date is in this format MM-DD-YYYY, also you can add other files extensions in FileList=

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.