Q:

How do I individually archive top-level sub-folders with 7za?

I have this batch file script to apply 7zip to sub-folders:
FOR /D %%i IN (*.*) DO 7za a -v1G -y -t7z -mx=3 "d:\backuptemp\tools\%%i.7za" "%%i\"

If I run this from within the "tools" folder, it backups up all of the sub-folders within "tools" and puts them in "backuptemp\tools" as 7zip archives.
What I want to do is be able to run this from outside the tools folder, however when I use the following code:
FOR /D %%i IN (d:\tools\*.*) DO 7za a -v1G -y -t7z -mx=3 "d:\backuptemp\tools\%%i.7za" "%%i\"

It tries to create a folder called d:\backuptemp\d:\tools\%nameoffolder%.7za and fails saying the filename syntax is incorrect.
What do I need to change in the syntax to be able to use this batch file outside of the "tools" folder?
Also, if possible I'd like to be able to append a date to the end of the file name. Let's say I have a folder called "Patches" within "tools", how do I make it so the archive will be "Patches_20140207.7za"?

A:

This change should help:
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

FOR /D %%i IN (d:\tools\*.*) DO 7za a -v1G -y -t7z -mx=3 "d:\backuptemp\tools\%%~nxi_%datestamp%.7za" "%%i\"

