Copy file according to date using console

4

I have a list of .rar files in a network unit which are generated automatically one per day.

What I'm trying to do is generate a .bat that goes to that network unit and copy the most recent file according to the date of modification or creation with the work better, it should only be the most recent file.

I've tried the following:

@echo off
echo ============INICIANDO PROCESO DE COPIADO=============
echo ......=ACEDIENDO A LA CARPETA DE LOS ARCHIVOS=.......
W:
echo .....................................................
echo ==============EMPEZANDO COPIA DE ARCHIVO=============
set fecha=%date%
xcopy "W:\UnidadDeRed\*.rar" /d:%fecha% "C:\DestinoArchivo\*.rar"

echo .............=COPIA DE ARCHIVO TERMINADA=............
echo =====================================================
pause

In the code I am declaring fecha and I give it a value using the operator date so I get the current date, then I use the varible fecha in the modifier /d: of the instruction xcopy the modifier serves to copy files changed during or after the specified date.

Like this one, I get the following error:

The date format of my team is that and the file format is also like this:

If I remove the modifier, copy all the files, which I do not want, but then:
Why that error? How is that switch used?
Is it because the files have time? Some other way?

Thank you in advance

    
asked by WilsonGtZ 09.02.2017 в 22:13
source

4 answers

1

The parameters are not in correct order you should follow this order:

xcopy source [destination] [/a] [/b] [/c] [/d [:date]] [/e] [/f] [/g] [/h]
[/i] [/j] [/k] [/l] [/m] [/n] [/o] [/p] [/q] [/r] [/s] [/t] [/u] [/v] [/w]
[/x] [/y] [/-y] [/z] 

As you can see, first go to the origin and destination and then the flags, I put some:

  • / a to copy a file

  • / i to avoid the prompt if it is a file or directory

  • / d for the date

I tested Windows 10 and it worked

With this code:

@echo off
echo ============INICIANDO PROCESO DE COPIADO=============
echo ......=ACEDIENDO A LA CARPETA DE LOS ARCHIVOS=.......
W:
echo .....................................................
echo ==============EMPEZANDO COPIA DE ARCHIVO=============
set fecha=%date%
xcopy "W:\UnidadDeRed\*.rar" "C:\DestinoArchivo\*.rar" /a /i /d:%fecha%

echo .............=COPIA DE ARCHIVO TERMINADA=............
echo =====================================================

pause
    
answered by 10.02.2017 в 00:31
1

I used to do something similar and not manage to make their proposals work, but modifying their contributions a bit, I made it work. Then I include the code that I tried and it worked:

@echo off
echo ==============EMPEZANDO COPIA DE ARCHIVO=============

set fecha=%date%

for /F "tokens=1-5 delims=/" %%a in ("%date%") do set "fecha=%%b-%%a-%%c"

xcopy "*.txt" "C:\Pruebas\" /y /i /d:%fecha%

echo .............=COPIA DE ARCHIVO TERMINADA=............
echo =====================================================

This code already includes the instruction with the date as needed by the xcopy command.

I hope you serve them!

    
answered by 09.05.2018 в 00:57
0

If you look at the help of the xcopy command, you will find that the date format you have to pass does not correspond to the format of the files or the system dl. The format is /D:m-d-y , in that order and with dashes, no periods.

You have the option to process the value of the date to adapt it to the format of xcopy

for /f "tokens=1-3 delims=." %%a in ("%date%") do set "fecha=%%b-%%a-%%c"

Or you can use robocopy indicating that you only want files with a maximum age of one day

robocopy "W:\UnidadDeRed" "C:\DestinoArchivo" *.rar /maxage:1
    
answered by 16.02.2017 в 08:13
0

Instead of doing that, you can get the name of the most recent file and pass it to xcopy as a parameter. This should work for you:

@echo off
echo ============INICIANDO PROCESO DE COPIADO=============
echo ......=ACCEDIENDO A LA CARPETA DE LOS ARCHIVOS=......
echo .....................................................
echo ==============EMPEZANDO COPIA DE ARCHIVO=============
echo.
for /f "delims=" %%a in ('dir W:\UnidadDeRed\*.rar /b /a-d /od') do set ultimo=%%a
xcopy "W:\UnidadDeRed\%ultimo%" "C:\DestinoArchivo"
echo.
echo .............=COPIA DE ARCHIVO TERMINADA=............
echo =====================================================
pause

Note that if the file exists, it will ask for confirmation to overwrite. If you leave the parameter /D without any date, it will only copy if the date is more recent.

Option 2:

If in the source folder you only add these backups and you simply replicate them in the destination, you could use something as simple as:

xcopy "W:\UnidadDeRed\*.rar" /D "C:\DestinoArchivo"

With this you would achieve your goal, because it will not copy any file that already exists and has the same date.

PS: Note that I corrected the error in the second echo . ;)

    
answered by 01.08.2017 в 19:06