Page 1 of 1

How to copy files, but not all of them

Posted: 03 Oct 2013 00:22
by hendrikbez
Thanks to Foxidrive for helping me with this code.

I just want to know if this is possible with a bath file.

I have 12 txt files that I want to copy (This is working) renaming them in new folder (this is working)
But not all of them change every day, so how can I only let the files that I change today, be copied to new folder, and
the others that have not changed and have yesterday date. not copied to folder.

Here are some of the code

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" rem & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"

CD "D:\Users\hbezuidenhout\Documents\WindowsGP\Restore" 2>nul
echo copying file
copy "D:\Users\hbezuidenhout\Documents\Windows GP Restore.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Restore\Windows GP Restore %DD% %MM% %YYYY%.txt"  >nul
echo done

CD "D:\Users\hbezuidenhout\Documents\WindowsGP\Clients" 2>nul
echo copying file
copy "D:\Users\hbezuidenhout\Documents\Windows GP Clients.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Clients\Windows GP Clients %DD% %MM% %YYYY%.txt"  >nul
echo done

CD "D:\Users\hbezuidenhout\Documents\WindowsGP\Backup" 2>nul
echo copying file
copy "D:\Users\hbezuidenhout\Documents\Windows GP Backup.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Backup\Windows GP Backup %DD% %MM% %YYYY%.txt"  >nul
echo done

CD "D:\Users\hbezuidenhout\Documents\WindowsGP\Tapes" 2>nul
echo copying file
copy "D:\Users\hbezuidenhout\Documents\Windows GP Tapes.txt.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Tapes\Windows GP Tapes %DD% %MM% %YYYY%.txt"  >nul
echo done


Re: How to copy files, but not all of them

Posted: 03 Oct 2013 00:33
by foxidrive
You incorrectly changed the MD to CD.

There is no need to CD to the folder because the full path is specified in the copy command.

Another issue above is the last filename has a double extension.

Re: How to copy files, but not all of them

Posted: 03 Oct 2013 00:44
by hendrikbez
You incorrectly changed the MD to CD.
There is no need to CD to the folder because the full path is specified in the copy command.

Ok I did change it from MD to CD, because the folder did already exist, but I will then take all the lines with cd out. I did think it should be in.

Another issue above is the last filename has a double extension.


Yes I did see that after I have send this out,

Is it possible that I ask in the first post.

Thank you for your great help

Re: How to copy files, but not all of them

Posted: 03 Oct 2013 00:50
by foxidrive
This uses a subroutine to check the last modified date and if it IS the same as today then it will copy the file.

Each line starting with call :check_copy has after it the "d:\path\source folder\filename" "d:\path\target folder\new filename"



Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" rem & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"

call :check_copy "D:\Users\hbezuidenhout\Documents\Windows GP Restore.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Restore\Windows GP Restore %DD% %MM% %YYYY%.txt"
call :check_copy "D:\Users\hbezuidenhout\Documents\Windows GP Clients.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Clients\Windows GP Clients %DD% %MM% %YYYY%.txt"
call :check_copy "D:\Users\hbezuidenhout\Documents\Windows GP Backup.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Backup\Windows GP Backup %DD% %MM% %YYYY%.txt"
call :check_copy "D:\Users\hbezuidenhout\Documents\Windows GP Tapes.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Tapes\Windows GP Tapes %DD% %MM% %YYYY%.txt"
pause
goto :EOF

:check_copy
:: gets the last modified timestamp in variables
:: file timestamp YYYYMMDD_HHMMSS
:: syntax: getfiledate.bat "c:\path\file.exe"
@echo off
set "file=%~1"
set "file=%file:\=\\%"
WMIC DATAFILE WHERE name="%file%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) do set dt=%%a
set dt=%dt:~0,8%_%dt:~8,6%
set "lastmod=%dt:~0,8%"
del file.tmp

if "%lastmod%" EQU "%YYYY%%MM%%DD%" (
MD "%~dp2" 2>nul
echo copying file "%~2"
copy /y "%~1" "%~2" >nul
echo done
)
goto :EOF

Re: How to copy files, but not all of them

Posted: 03 Oct 2013 01:22
by hendrikbez
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"


call :check_copy "D:\Users\hbezuidenhout\Documents\Windows GP Restore.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Restore\Windows GP Restore %DD% %MM% %YYYY%.txt"
call :check_copy "D:\Users\hbezuidenhout\Documents\Windows GP Clients.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Clients\Windows GP Clients %DD% %MM% %YYYY%.txt"
call :check_copy "D:\Users\hbezuidenhout\Documents\Windows GP Backup.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Backup\Windows GP Backup %DD% %MM% %YYYY%.txt"
call :check_copy "D:\Users\hbezuidenhout\Documents\Windows GP Tapes.txt" "D:\Users\hbezuidenhout\Documents\WindowsGP\Tapes\Windows GP Tapes %DD% %MM% %YYYY%.txt"

goto :EOF

:check_copy
:: gets the last modified timestamp in variables
:: file timestamp YYYYMMDD_HHMMSS
:: syntax: getfiledate.bat "c:\path\file.exe"
@echo off
set "file=%~1"
set "file=%file:\=\\%"
WMIC DATAFILE WHERE name="%file%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) do set dt=%%a
set dt=%dt:~0,8%_%dt:~8,6%
set "lastmod=%dt:~0,8%"
del file.tmp

if "%lastmod%" NEQ "%MM%%DD%%YYYY%" (
MD "%~dp2" 2>nul
echo copying file "%~2"
copy /y "%~1" "%~2" >nul
echo done
)
goto :EOF



I did run this, but it is still copy the file from yesterday to today's date.
I do not understand this, maby this is the problem.

Code: Select all

:check_copy
:: gets the last modified timestamp in variables
:: file timestamp YYYYMMDD_HHMMSS
:: syntax: getfiledate.bat "c:\path\file.exe"

Re: How to copy files, but not all of them

Posted: 03 Oct 2013 06:11
by foxidrive
Those lines aren't a problem and I tested it here where it worked fine.

Try copy and pasting it again - and copy the screen output if it still fails.

I added this line to help track down the issue.
echo testing - if "%lastmod%" NEQ "%YYYY%%MM%%DD%"

Re: How to copy files, but not all of them

Posted: 03 Oct 2013 06:32
by hendrikbez
Ok I see now where the problem is, I did copy the new code and run it.

The Windows GP Clients.txt and Windows GP Backup.txt is the only two files that have changed today, they must be copied to folder, but did not.

The Windows GP Restore.txt and Windows GP Tapes.txt did not change today, and should not be cpooied, but they where the two that have copied to the folder.

It must be the other ways around.

Re: How to copy files, but not all of them

Posted: 03 Oct 2013 06:48
by foxidrive
All respects, but are you certain of your file modification date?

I tested it and when I edited the file today and saved it, the code didn't copy it.

Re: How to copy files, but not all of them

Posted: 03 Oct 2013 06:55
by hendrikbez
Hi

if I have a date for today, it must copy it to folder, if the date is yesterday and older it must not be copied.
So inly dates for today's date must be copied.

Sorry if I am confusing you.

Re: How to copy files, but not all of them

Posted: 03 Oct 2013 07:05
by foxidrive
I misunderstood. Try the code now - I edited it to copy files that are changed today.

Re: How to copy files, but not all of them

Posted: 03 Oct 2013 07:20
by hendrikbez
Thank you will let you know how it is working, on the train on my way home

Re: How to copy files, but not all of them

Posted: 03 Oct 2013 09:49
by hendrikbez
Foxidrive

Thank You, it is working now