Rename file if already exists

March 27, 2011 at 15:19:52
Specs: Windows 7
Situation: Reports are done in the field, and when the user returns back to the office, the batch file uploads the completed reports to the pdf storage server.

The issue I have is sometimes the user will incorrectly type the file name.
(Incident numbers / file name are 5 digit numbers)
This has the result of overwriting the existing file on the server if they typed it incorrectly and that numbers has already been used.

I need some help coming up with a way to check C:\Reports\ & \\Mcj_pdc\reports\TIBRS\ before the robocopy.exe line to see if the file already exists on the server, and renaming one of the files to add "_old" so one file will not be deleted off the server if this happens again.

Here is my current code from that section of the batch file.
:UPLOAD
cls
echo.
echo.
echo.
ECHO Checking connection status, please wait...
echo.
echo.
PING -n 5 Mcj_pdc|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :BEGINUPLOAD
IF ERRORLEVEL 1 goto :TRYAGAIN

:BEGINUPLOAD
echo Uploading Reports to Server
echo.
::UPLOAD ALL REPORT FILES TO PDF SERVER
Robocopy.exe C:\Reports\ \\Mcj_pdc\reports\TIBRS\*.pdf /MOV /NJH /NJS /NC /Z


See More: Rename file if already exists

Report •

#1
March 28, 2011 at 18:57:26
The only thing I have been able to come up with is using
Robocopy.exe C:\Reports\ \\Mcj_pdc\reports\TIBRS\*.pdf /MOV /NJH /NJS /NC /XN /XC /Z
(Added /XN /XC)
then rerunning Robocopy to send the remaining files to another directory
Robocopy.exe C:\Reports\ \\Mcj_pdc\reports\TIBRS\Mobile\*.pdf /MOV /NJH /NJS /NC /Z

Not the best solution, but it works until someone here can suggest me another & better way.


Report •

#2
March 30, 2011 at 09:24:14
You really need to look into alternate methods, like some internal web application. A web forum would let you sanitize data as it's entered; a web uploader would allow you to control the name of the file from the server.

Untested, and it'll probably bog things down:

FOR %%a IN (C:\Reports\*.pdf) DO REN "\\Mcj_pdc\reports\TIBRS\%%~NXa" "%%~Na_old%%~Xa" 2>NUL

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question