I have a simple batch file that I use to archive files in tar/gzip format. I have placed the batch file in system32 so that I can access it from anywhere.
I open the command window using "shift + right click" in a particular folder where I want the contents of the folder to be archived and enter the name of the batch file (targz.bat
). Batch file does the archiving/compressing.
The problem is I use absolute paths. I need a way to get the current directory and the name of the current folder. I can get the current directory with %cd%
, but how do I get the folder name?
For example:
set currentdir=%cd% "C:\xampp\htdocs\wordpress"
set currentfoldername= should be just "wordpress"
Actual code:
@echo off
cd "C:\Program Files\7-Zip"
7z a -ttar "C:\xampp\htdocs\wordpress\archive.tar" "C:\xampp\htdocs\wordpress\*"
7z a -tgzip "C:\xampp\htdocs\wordpress\archive.tar.gz" "C:\xampp\htdocs\wordpress\archive.tar"
del "C:\xampp\htdocs\wordpress\archive.tar"
exit
Desired:
@echo off
set currentdir=%cd%
set currentfoldername=
cd "C:\Program Files\7-Zip"
7z a -ttar "%currentdir%\%currentfoldername%.tar" "%currentdir%\*"
7z a -tgzip "%currentdir%\%currentfoldername%.tar.gz" "%currentdir%\%currentfoldername%.tar"
del "%currentdir%\%currentfoldername%.tar"
exit