[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1025
  • Last Modified:

batch file/xcopy.

Hi there,

I'm trying to write a batch file using xcopy.  I aim to copy outlooks NK2 file from C: to a network location.  Which I have accomplished.

However, the bit I'm struggling with is that I need to have the batch file make the copy, then instead of overwriting that copy the next time the batch file is run(which will be on start up).  I would like it to make a fresh copy.  

I.e. the original would be seacore.nk2, the second copy would be seacore1.nk2,  the third copy to be secore3.nk2 and so on.  I would want it to do this 5 times, then on the 6th time to overwrite the original seacore.nk2 copy, then on the 7th time the seacore1.nk2 and so on in a permanent cycle.

I understand that this is possible, but I have a very poor understanding of batch files and I'm not sure in what context to use the commands.

Please can you help?!



@echo off 
xcopy "C:\Documents and Settings\swilkins\Application Data\Microsoft\Outlook\seacore.nk2" "\\main_fileserver\IT Files\Test Batch file" /y

Open in new window

0
LetchfordP
Asked:
LetchfordP
  • 3
  • 3
1 Solution
 
bategCommented:
HI LetchfordP
I recently create a patch file, modify it to comply your needs
cls
@echo off
echo off
:Start
 
 
rem -----------------------------------    main menue   ---------------------------------------
 
@echo off
 
 
echo  ##########AH5#####
echo.
echo     a - Remove all previous backups before proceeding
echo     b - Backup		
echo.
echo     x - Exit       h - owner information
echo.
echo  ##################
set /p userinpa=Choose your Aim =  
if "%userinpa%"=="a" goto Remove
if "%userinpa%"=="b" goto Backup
if "%userinpa%"=="x" goto Exit
if "%userinpa%"=="h" goto Info
 
 
 
cls
@echo off
echo off
 
echo. 
echo.
echo.
echo                                  INVALID CHOICE PLEASE TRY AGAIN
echo.
echo.
echo.
 
 
goto Start
 
rem----------------------------- Remove previous backups -----------------------------------------------
 
:Remove
 
cls
@echo off
echo off
 
set /p userinpb=Choose your Drive =
 
rmdir /s /q "%userinpb%":\backup 
 
 
cls
 
echo  ##########AH5#####
echo.
echo     The backup has been removed successfuly
echo.
echo  ##################
echo.
echo.
echo.
echo.
 
goto start
 
 
:Backup
 
cls
@echo off
echo off
 
REM ------------------------------------ REMOVE TEMP FILES --------------------------------------------------
 
del /s /f /q c:\windows\temp
del /f /s /q "c:\Documents and Settings"\%username%\"local settings"\Cookies
del /f /s /q "c:\Documents and Settings"\%username%\"local settings"\Temp
del /f /s /q "c:\Documents and Settings"\%username%\"local settings"\History
del /f /s /q "c:\Documents and Settings"\%username%\"local settings"\"Temporary Internet Files"
 
 
RD /s /q "c:\Documents and Settings"\%username%\"local settings"\Temp
MD "c:\Documents and Settings"\%username%\"local settings"\Temp
 
rd /s /q c:\windows\temp
md c:\windows\temp
 
cls
 
echo  ##########AH5#####
echo.
echo     The Temp Files and folder has been cleared
echo.
echo  ##################
echo.
echo.
echo.
echo.
 
 
 
set /p userinpc=Choose Drive you want to save backup to = 
 
 
 
REM -------------------------------------------- CREATING DIRECTORIES -------------------------------------------
 
 
md "%userinpc%":\backup
md "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%
md "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%
 
REM ------------------------------------------- cOPY FEVORITES ----------------------------------------------
 
md "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\favorites
 
xcopy /E /C /H /O /V /Y c:\"documents and settings"\%username%\favorites\*.* "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\favorites
 
REM ------------------- COPY LOCAL SETTINGS "MICROSOFT OUTLOOK EXPRESS AND THE MICROSOFT OUTLOOK" ------------
 
md "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\"Local Settings"
md "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\"Local Settings"\"Application Data"
md "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\"Local Settings"\"Application Data"\Identities
md "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\"Local Settings"\"Application Data"\Microsoft
md "%userinpa%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\"Local Settings"\"Application Data"\Microsoft\Outlook
 
 
 
xcopy /E /C /H /O /V /Y c:\"documents and settings"\%username%\"Local Settings"\"Application Data"\Identities\*.* "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\"Local Settings"\"Application Data"\Identities
xcopy /E /C /H /O /V /Y c:\"documents and settings"\%username%\"Local Settings"\"Application Data"\Microsoft\Outlook\*.* "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\"Local Settings"\"Application Data"\Microsoft\Outlook
 
 
REM ----------------------------------------------- COPY MY DOCUMENTS ---------------------------------------
 
md "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\"My Documents"
 
xcopy /E /C /H /O /V /Y c:\"documents and settings"\%username%\"My Documents"\*.* "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\"My Documents"
 
REM ---------------------------------------------- COPY Application Data ------------------------------------
 
md "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\"Application Data"
 
xcopy /E /C /H /O /V /Y c:\"documents and settings"\%username%\"Application Data"\*.* "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\"Application Data"
 
 
REM ---------------------------------------------- COPY Desktop ----------------------------------------------
 
md "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\Desktop
 
xcopy /E /C /H /O /V /Y c:\"documents and settings"\%username%\Desktop\*.* "%userinpc%":\backup\%date:~4,2%%date:~7,2%%date:~-2%\%username%\Desktop
 
 
 
echo  ##########AH5#####
echo.
echo     You will find your backup data under %userinpa%:\backup
echo     Please don't hesitate to contact IT helpdesk if you need any further clarification or assistance 
echo.
echo  ##################
 
Pause
 
 
 
 
goto exit
 
 
 
:Exit
exit
 
:Info 
 
cls
@echo off
echo off
 
 
echo.
echo.
echo.
echo.
echo                             Put Your Information Here 
echo                                  
echo                                  
echo.
echo.               
set /p userinp=press(b)to back (x)to Exit = 
if "%userinp%"=="b" goto start

Open in new window

0
 
LetchfordPAuthor Commented:

Thank you for your reply.  Which section of your batch should I be looking at copying/editing to meet my needs?  

I am very new to batch files.

I only need to copy one file (the nk2) and place in a share on our network.  But I do need to have maybe five copies of the nk2 file on the network prefixed with date or a number or something before they start to over write the oldest.  So I assume I wouldn't need to use whole of your batch, but I havent the foggiest where and how to break it down into what I need.

Also, I am going to specify a destination location for each user i.e.  \\main_fileserver\outlook backup\joe bloggs\   so does that make the md "%userinpc% bit irrelevant as well?

Sorry if Im not making myself very clear, but like I said..Im very unsure of batch file commands, the and context in which they are used

Thanks again.

this is what I have tried!

\\main_fileserver\IT Files\Test Batch file\%date:~4,2%%date:~7,2%%date:~-2%
xcopy /E /C /H /O /V /Y "C:\Documents and Settings\swilkins\Application Data\Microsoft\Outlook\seacore.nk2" "\\main_fileserver\IT Files\Test Batch file"
0
 
AmazingTechCommented:
Keep track of the number in a file. Here I'm using the extension of the file for the number.

Once it's copied rename the number
If Not Exist "\\main_fileserver\IT Files\Test Batch file\nextseacore.*" Copy NUL "\\main_fileserver\IT Files\Test Batch file\NextSeacore.1"
 
for /f %%a in ('dir /b "\\main_fileserver\IT Files\Test Batch file\NextSeacore.*"') do Set Number=%%~xa
 
echo f|xcopy /E /C /H /O /V /Y "C:\Documents and Settings\swilkins\Application Data\Microsoft\Outlook\seacore.nk2" "\\main_fileserver\IT Files\Test Batch file\seacore%Number%.nk2"
 
Set /a Number+=1
If %Number% GTR 5 Set Number=1
REN "\\main_fileserver\IT Files\Test Batch file\NextSeacore.*" NextSeacore.%Number%

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LetchfordPAuthor Commented:
Ah brilliant.....  

One last thing (hopefully), this is going to be deployed on multple user machines in the start up folder.  It would be quite an effort to remame the nextseacore file for each of them everythime they reboot.

Is there anyway of writting more script to automate it? In honesty, it's not that important that the file overwrites itselft after five.  If it can be set up to just keep going i.e. seacore.1 - seacore.5000 that would be just as good.
0
 
AmazingTechCommented:
OK. Now that the rolling number requirement is gone. We don't need a file to keep track of the number.

I haven't tried it yet but this should work.

I'm using 10000 to keep the leading zeros this is needed for correct sort ordering. With this number I'm using the last 4 digits in the filename.

I also renamed the nk2 file to have the username and a number.
Set Number=10001
for /f "Tokens=2 delims=-." %%a in ('dir /on /b "\\main_fileserver\IT Files\Test Batch file\%Username%_Seacore-*.nk2"') do Set /a Number+=1%%a
 
echo f|xcopy /E /C /H /O /V /Y "%appdata%\Microsoft\Outlook\seacore.nk2" "\\main_fileserver\IT Files\Test Batch file\%Username%_seacore-%Number:~-4%.nk2"

Open in new window

0
 
LetchfordPAuthor Commented:
Works flawlessly.  Thanks very much.
0
 
AmazingTechCommented:
Thanks for the grade.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now