Cyyrils
Cyyrils

Reputation: 97

Batch - copy of a file for backup

I am trying to make a copy of a file in a folder having the name of today. If the folder exists it just makes the copy inside, if not it will create the folder and copy it there. Here is what I am doing

setlocal

Y:
set dateT=%date:~10,4%%date:~7,2%%date:~4,2%
set "pathFiles=Y:\Myfolder\"
set pathBackup = %pathFiles%%dateT%

pause
if not exist %pathBackup% (
    mkdir %pathBackup%
)

cd %pathBackup%
robocopy C:\Users\xxx\Desktop\file.mdb %pathBackup% 

It is not really working, do you see any errors ?

Thanks a lot

Upvotes: 1

Views: 77

Answers (1)

MC ND
MC ND

Reputation: 70933

While the question is formulated, this is what can be seen

Problems with variables

                v--- Space included in value
set pathBackup = %pathFiles%%dateT%
              ^----- Space included in variable name

set "pathBackup=%pathFiles%%dateT%"

Maybe problems with spaces in paths. Better use quotes

if not exist "%pathBackup%\" (
    mkdir "%pathBackup%"
)

Wrong robocopy usage. You are using the copy or xcopy syntax. In robocopy you use sourceFolder targetFolder fileMask as arguments.

robocopy "C:\Users\xxx\Desktop" "%pathBackup%" file.mdb
xcopy "C:\Users\xxx\Desktop\file.mdb" "%pathBackup%"
copy "C:\Users\xxx\Desktop\file.mdb" "%pathBackup%"

Upvotes: 1

Related Questions