Batch script to copy, zip, move then delete

I have  a bunch of files in C:\Temp. They range from .doc, .xls, .pdf, etc. Every day, I am manually moving (cut) those to C:\Backup. After I cut the files from C:\Temp, I create a new folder with today's date in C:\Backup, then paste those files from C:\Temp into the newly created folder in C:\Backup. I would like to automate this if I can. I have been trying to figure out a batch script that will do this:
1. Zip all the files in C:\Temp
2. Create a new folder on C:\Backup with today's date
3. Paste that zipped folder in C:\Backup in that new folder
4. Delete the files in the C:\Temp folder AFTER the zipped folder has been moved to C:\Backup

Is there a way to create a batch file to do this? If its multiple scripts, thats fine too. I have 7z on the server if that helps. Thanks!!
brasimanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TemodyPickalbatros, IT ManagerCommented:
What about backup application can do that automatically, schedule, incremental and safety
Acronis or paragon
0
NVITCommented:
Make a .bat file of this code.

- Change to your needs: srcdir, TgtDir
- Processes files only. Sub-folders not included.
- Assumes 7zip is installed.
- If files are successfully zipped, they are deleted.
For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(Set Day=%%A&Set Month=%%B&Set Year=%%C)
For /F "tokens=1-4 delims=: " %%A in ('time /t') do @(Set HH=%%A&Set MM=%%B&Set AM=%%C)

set SrcDir=c:\temp
set TgtDir=c:\Backup

set FNDate=%Year%_%Month%_%Day%_%HH%_%MM%_%AM%
set ZipFN=%FNDate%.zip

pushd %SrcDir%

"%ProgramFiles(x86)%\7-Zip\7z.exe" a "%temp%\%ZipFN%" "*.*"

if %errorlevel% equ 0 (
  md "%TgtDir%\%FNDate%"
  move "%temp%\%ZipFN%" "%TgtDir%\%FNDate%"
  del /q "*.*"
)
popd

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
Since you are backing up all files to a single ZIP anyway, then why have a folder with the date stamp for each day, since that will only hole the one file?  I would suggest using the date time stamp as the name of the ZIP, and placing them all in the backup folder itself.  That could look like this.  Adjust paths as needed near the top.

@echo off
setlocal

REM Set paths to work with
set BaseDir=C:\Temp
set DestDir=C:\Backup
set zip=C:\Program Files\7-zip\7z.exe

REM Get current date and time stamp (YYYYMMDD_hhmmss)
for /f "tokens=1 delims=. skip=1" %%A in ('wmic os get LocalDateTime') do (
  if not defined LocalDateTime (
    set LocalDateTime=%%A
    set CurrentDateTime=!LocalDateTime:~0,8!_!LocalDateTime:~8,6!
  )
)

REM Zip files to timestamped file in backup folder
"%zip%" a "%DestDir%\%CurrentDateTime%.zip" "%BaseDir%\*.*" -tzip

REM Remove files
if %ERRORLEVEL% EQU 0 del /q "%BaseDir%\*.*"

Open in new window

~bp
0
brasimanAuthor Commented:
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.