Click here to Skip to main content
15,306,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I am using robocopy to copy files from source to destination folder but there are some problems.

In the source folder .csv files are created by various users every 15-20 mins for example User1.csv, User2.csv, User3.csv. There is an archive folder on the source folder.

These files have to be moved to a destination folder. The files on the destination folder are uploaded to a different application every 30 mins.

Problem: When user1 creates a file user1.csv, it has to be moved to the destination folder. If a file already exists on the destination folder then it should not be moved. But if no file exists on the destination folder then the file needs to be moved and a copy of the file has to be saved on archive folder.

@echo off

SET SORC= "C:\Test\Source" 
SET DEST="C:\Test\Destination"

ROBOCOPY %SORC% %DEST% *.csv /R:1 /W:1 /MOV /XN /XO /LOG:%LOG%


What I have tried:

I am using the above code to move the files.

How can i save the files on the archive folder with the same name every time a new file is created?
Updated 23-Aug-18 2:07am

1 solution

One solution would be getting the files one by one using a for loop and process them.

ECHO Copy session %date% %time%>%LOG%
FOR %%f in (%SORC%\*.csv) do (
    SET fname=%%~nxf
    IF NOT exist %DEST%\%fname% (
        xcopy %%f %SORC%\archive /q
        move /Y %%f %DEST%
        ECHO Copied file %fname%>>%LOG%
    ) ELSE (
        ECHO Skipped file %fname%>>%LOG%
Dave Kreskowiak 23-Aug-18 9:17am
I don't know why anyone would you give a 1-vote. It's a perfectly reasonable answer. Countered.
Jochen Arndt 23-Aug-18 13:05pm
Thank you Dave.
Member 13958091 23-Aug-18 12:54pm
Any other solutions?
Jochen Arndt 23-Aug-18 13:04pm
What is the problem with my solution?

However, you may write something similar using PowerShell commands where you have more options.
Member 13958091 23-Aug-18 15:57pm
I tested it and tried to make it work but no luck.
Jochen Arndt 23-Aug-18 17:04pm
I did not have your directory structure so that I can't test it. But you can by inspecting the log file or inserting additional ECHO commands.
Member 13958091 23-Aug-18 17:28pm
The source and destination are two different servers but I am testing it out on my PC until I get it right.

Source folder: C:\Test\Source
Target foler: C:\Test\Destination
Archive folder: C:\Test\Source\Archive
Member 13958091 23-Aug-18 17:29pm
Here is a sample code to copy file with same name and rename after copying.

@echo off
set TESTFOLDER=test_folder

set /a counter=0
FOR /F "tokens=*" %%i IN ('DIR /S /B /A-D .\src\*') DO FOR /F "tokens=*" %%j IN ('DIR /B "%%i"') DO IF EXIST ".\%TESTFOLDER%\%%j" (
set /a counter=!counter!+1
echo folder: %TESTFOLDER%
copy "%%i" ".\%TESTFOLDER%\%%j_!counter!"
) ELSE copy "%%i" ".\%TESTFOLDER%\%%j"
Jochen Arndt 24-Aug-18 2:46am
And that is working?
It is not much different than my solution but more complicated.
Note the "%%~nxf" from my solution which gets the name and extension from the full path in %%f.

The problem might be with assigning that to a variable. Then use it like in your example (enable delayed extension and use !fname!) or just avoid using a local variable.

You did not told us that the destination is on another system. But that is no problem as long as you have access to the path.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900