How to copy files, but not all of them

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
hendrikbez
Posts: 20
Joined: 11 Sep 2008 22:36

How to copy files, but not all of them

#1 Post by hendrikbez » 03 Oct 2013 00:22

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


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#2 Post by foxidrive » 03 Oct 2013 00:33

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.

hendrikbez
Posts: 20
Joined: 11 Sep 2008 22:36

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

#3 Post by hendrikbez » 03 Oct 2013 00:44

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#4 Post by foxidrive » 03 Oct 2013 00:50

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

hendrikbez
Posts: 20
Joined: 11 Sep 2008 22:36

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

#5 Post by hendrikbez » 03 Oct 2013 01:22

@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"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#6 Post by foxidrive » 03 Oct 2013 06:11

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%"

hendrikbez
Posts: 20
Joined: 11 Sep 2008 22:36

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

#7 Post by hendrikbez » 03 Oct 2013 06:32

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#8 Post by foxidrive » 03 Oct 2013 06:48

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.

hendrikbez
Posts: 20
Joined: 11 Sep 2008 22:36

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

#9 Post by hendrikbez » 03 Oct 2013 06:55

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#10 Post by foxidrive » 03 Oct 2013 07:05

I misunderstood. Try the code now - I edited it to copy files that are changed today.

hendrikbez
Posts: 20
Joined: 11 Sep 2008 22:36

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

#11 Post by hendrikbez » 03 Oct 2013 07:20

Thank you will let you know how it is working, on the train on my way home

hendrikbez
Posts: 20
Joined: 11 Sep 2008 22:36

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

#12 Post by hendrikbez » 03 Oct 2013 09:49

Foxidrive

Thank You, it is working now

Post Reply