-1

I have multiple auto-generated folders in the location C:\ABC . Each auto-generated folder is created at the start of the day and naming convention is "HS".

Eg:

 - C:\ABC\HS08042015
 - C:\ABC\HS08032015
 - C:\ABC\HS08022015
 - C:\ABC\HS08012015

So if today's date is 08042015, then I want the batch file to copy the previous day's folder (HS08032015) to the new location. So on 08042015, the folder C:\ABC\HS08032015 needs to be copied to another location D:\Reports\HS08032015

As such, this batch file should always copy the previous days, dated folder over to the new location.

I was thinking of using XCOPY, but really could not write a working code.

Any help is highly appreciated. Thanks much.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
Naved Zia
  • 13
  • 1
  • 4
  • how was the folders getting created , due you have any batch script for that ? If yes , then share your code so that we can try if some thing can be worked using that. – prudviraj Aug 04 '15 at 06:12
  • Hi Melebius and Prudviraj I am very new to batch file writing and this is a quickfix to a reporting requirement that I need run for few months. Sorry, but all I got was a simple xcopy commands, etc. The folders are auto-generated everyday and we have the past 7 days folder all the time. Older then that are auto deleted by the application. What we need to do is copy over the previous day's folder to a new location so that another application can extract the reports from them from the new location. – Naved Zia Aug 04 '15 at 13:00

2 Answers2

0

Tell me if your date display is different from what you want.

@echo off

set m=%date:~-7,2%
set today_date=%date:~-10,2%%m%%date:~-4,4%
set /a m -= 1
set yesterday_date=%date:~-10,2%0%m%%date:~-4,4%

echo Folder to copy: HS%today_date%
echo Folder copied: HS%yesterday_date%
echo d | xcopy C:\ABC\HS%today_date% D:\Reports\HS%yesterday_date% /e /y >nul
pause >nul
Happy Face
  • 1,061
  • 7
  • 18
  • Hi Dark Fang Thanks for this. It is working as required by editing the xcopy command. The above code is copying today's folder into a folder named after yesterday's date. I edited it as below: echo d | xcopy C:\ABC\HS%yesterday_date% D:\Reports\HS%yesterday_date% /e /y >nul However I always need to copy yesterday folder to a new location keeping the folder name intact. Thanks for your code. Very much appreciated. I am very new to this. If you can explain in summary what the code is doing, it would help learn. Thanks again. – Naved Zia Aug 04 '15 at 11:51
  • Oops, sorry about the today_date :P. Btw, do you want to move it instead of copying it? I mean if you move it, the yesterday_date file in C:\ABC\ will be deleted (as the file is moved to D:\Reports\), so you'll no need to delete it manually. Is this what you wanted too? – Happy Face Aug 04 '15 at 11:58
  • @NavedZia This will not calculate yesterdays date reliably. I presume you want to use it on days all through the year. – foxidrive Aug 04 '15 at 12:33
  • No I just need to copy it over. Basically, I need to copy the previous day's folder everyday to a new location in reports folder. Please explain if possible, the cide. Thanks. @Foxidrive Please let me know what could be the issue as this will be run on a reporting server and running into errors could be problematic. – Naved Zia Aug 04 '15 at 12:54
  • @NavedZia The solution shown doesn't take account of calendar dates - it will fail on the first of next month. – foxidrive Aug 04 '15 at 13:09
  • @foxidrive Yes you are correct. It fails when I set today's date on my Server as 01-Aug. It tries to search for folder HS08002015, instead of HS07312015. Thanks. – Naved Zia Aug 05 '15 at 05:31
0

This uses a script to get yesterdays date in the format you need
and then uses Robocopy to copy the entire folder tree.

@echo off
:: date yesterday or any number
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "data=%mm%%dd%%yyyy%"

robocopy "C:\ABC\HS%data%" "D:\Reports\HS%data%" /mir
pause
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Hi Foxidrive Thanks for the code. I dont have Robocopy unfortunately and cannot install it as well in my environment. As such will need XCOPY. I tried your code with XCOPY instead of robocopy, but it returned a parse error to me. Thanks once again. Appreciate your help.. helps me learn. – Naved Zia Aug 04 '15 at 11:54
  • `xcopy "C:\ABC\HS%data%\*.*" "D:\Reports\HS%data%\" /s/h/e/k/f/c` <--- test this command in place of the Robocopy command. – foxidrive Aug 04 '15 at 12:35
  • I tried this, but it did not find any files. gave no file found error. 0 files copied. Thanks for quick response to you and all. :) – Naved Zia Aug 04 '15 at 12:55
  • Your folder doesn't exist, or it's empty. – foxidrive Aug 04 '15 at 13:06
  • There are multiple files inside the folder.. around 13MB data. When I try to execute, it gives me the following error: <<--- "File not found = HSipt 0 File(s) copied Press any key to continue..." --->> Please advice and help. – Naved Zia Aug 05 '15 at 05:11
  • You wrote `c:\abc` and I doubt it is `c:\abc` - but I can't see what it is, or the file system, so I have no way of knowing where you made an error that broke the command. – foxidrive Aug 05 '15 at 05:56
  • Yes the path that I am testing on is C:\ABC. The file structure is as defined in the question. The code from dark_fang worked, but failed at start of the month as pointed by you. When I run your code, it looks like it is searching for the name HSipt. Not sure why. FYI.. The date format on my system is M/d/yyyy. Can you please help correct the error I am getting with your code please? Thanks in advance. – – Naved Zia Aug 05 '15 at 07:55
  • Sure. Add `echo` before the xcopy command and run the batch file, then paste the line it shows here. – foxidrive Aug 05 '15 at 09:24
  • on adding echo before copy, and executing, it gives the below: `xcopy "C:\ABC\HSipt" "D:\Reports\HSipt" /s/h/e/k/f/c Press any key to continue...` Thanks – Naved Zia Aug 05 '15 at 09:48
  • Correction: On adding echo before copy, and executing, it gives the below: `xcopy "C:\ABC\HSiptCScr" "D:\Reports\HSiptCScr" /s/h/e/k/f/c Press any key to continue... ` Thanks – Naved Zia Aug 05 '15 at 10:03
  • Copy the code as I posted it and try that. Just substitute the `echo xcopy` line and confirm it is looking at the correct folder. You have made an error in your editing of the file by the look of it. – foxidrive Aug 05 '15 at 10:32
  • I think I found the issue. Your code is excellent and correct. My temp folder was giving permission issue and as such the output was error. I fixed the permission on the temp folder and it now works awesome. I tested for end of month and start of month too and it works fine. Thanks soo very much for your help. Much much appreciated. :) – Naved Zia Aug 05 '15 at 10:51