Reputation: 312
My script so far:
@ECHO OFF
XCOPY C:\Users\Jeremy\Documents\* I:\Jeremy's%1Desktop\My%~1Documents /s /i /h
Trying to create a "backup script" that will transfer all of the files from my entire "Jeremy" directory to my external harddrive. (Want to not transfer some hidden files in some folders but do transfer hidden files in another folder) so I don't know how to accomplish that either.
Anyway I want to instead of having it prompt me everytime to overwrite a file, i'd like to set it to overwrite if larger or if newer (hence the timestamp). I am unsure of how to set this and set overwrite if timestamp say's newer. Thank you for your help, as you can see two lines in I'm having a hard time.
Thanks!
Upvotes: 0
Views: 5436
Reputation: 17064
See ROBOCOPY for advanced copy-options (e.g. /XO = exclude older files).
With COPY or XCOPY, you will have to loop through the files manually and copy them individually after performing your tests.
Loop (recursively) through files in a folder:
for /r "%directory%" %%f in (*.*) do (
rem ... do sth with file %%f
)
You can also get for example the names/directories of the files with %~nf
or %~df
Get date/time of file:
for /f %f in ("%file%") do (
set timestamp=%~tf
set day=%timestamp:~0,2%"
set month=%timestamp:~3,2%"
rem ... etc.
)
Get current date/time:
echo Date: %date% rem (or: set currentdate=%date%)
echo Time: %time%
You would have to implement the comparison yourself (first compare year, then compare month, then... etc.) This could be a little tricky, or a lot of work at least.
To check if a file is hidden:
for /f "delims=" %%a in ('attrib "%file%"') do (
set attribs=%%a
if "!attribs:~4,1!"=="H" rem ... file is hidden
)
Don't forget to set this at the beginning of your batch:
setlocal enabledelayedexpansion
This ensures that you can set/read variables within a loop. Use !variable!
instead of %variable%
then.
These are just the basic tools, there's still a lot of work to do and some pitfalls to work around. You should really consider using ROBOCOPY.
Upvotes: 2