0

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
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
akinuri
  • 10,690
  • 10
  • 65
  • 102

1 Answers1

1

Using the ~n modifier you can easely get the last element of a path :

for %%a in (%cd%) do set "currentfoldername=%~na"
SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Jun 20 '17 at 14:15
  • 1
    I'm Ok with that @DimaSan, edited with a simple explanation ! – SachaDee Jun 20 '17 at 15:48