Seems a good place as any to ask
So I want to assign a file name being processed by a batch file to:
LASTFOLDERIN%1PATHNAME-FOLDERNAME.rar
using the %1
Windows sends me from the context menu.
So far I'm able to ALMOST make it, I get so far as:
WinRAR.exe a -r -ep1 -u -m0 -y "C:\Sorting\ %~p1-%~n1.rar" "%~1\*.*"
Which returns:
\FULL(DIR1\DIR2\DIR3)PATH\-FOLDERNAME.RAR
But the problem is I get the FULL path (multiple directories), not just the very last one, also, I get the \
that breaks the whole thing.
Question is, is there a way to extract just the last folder in the path variable %~1 that I get from the windows context menu argument?
Or if not, can I somehow replace the \
with -
so I can rename the files manually later? to get something like this: -DIR1-DIR2-DIR3-FOLDERNAME.RAR
Any help would be greatly appreciated, I'm stuck here.
PS I'm sending %V
to the batch file, since %1
did all kinds of weird things.
CodePudding user response:
ok I've found something that is definitely not elegant but does the job, if anyone can improve by all means, this is the best I could do and it gets the job done, here's the code for the batch files: (Thanks to Squashman for the working code for spaces and recursive dirs)
For last dir only
for %%G in ("%~dp1\.") do set "PATHNAME=%%~nxG"
"WinRAR.exe" a -r -ep1 -u -m0 -y "C:\[Sorting]\%PATHNAME%-%~n1.rar" "%~1\*.*"
Returns: LASTPATHDIR-SELECTEDDIR.RAR file in a specific folder, id wish I could add this to the drag and drop handlers but alas I cant only DLLS can be there, so this is the next best thing, I just change the batch file whenever I need it stored somewhere else, you can call a path variable instead on the batch file and just update it whenever you need it changed if you like, so you dont have to change all the batch files every time.
For 2nd to last last dir:
for %%G in ("%~dp1\.") do set "PATHLAST=%%~nxG"
for %%G in ("%~dp1\..") do set "PATH2NDLAST=%%~nxG"
set PATHNAME=%PATH2NDLAST%-%PATHLAST%
ECHO Processing %PATHNAME%-%~n1.rar"
"WinRAR.exe" a -r -ep1 -u -m0 -y "C:\[Sorting]\%PATHNAME%-%~n1.rar" "%~1\*.*"
for third to last second to last last:
for %%G in ("%~dp1\.") do set "PATHLAST=%%~nxG"
for %%G in ("%~dp1\..") do set "PATH2NDLAST=%%~nxG"
for %%G in ("%~dp1\..\..") do set "PATH3RDLAST=%%~nxG"
set PATHNAME=%PATH3RDLAST%-%PATH2NDLAST%-%PATHLAST%
ECHO Processing %PATHNAME%-%~n1.rar"
"WinRAR.exe" a -r -ep1 -u -m0 -y "C:\[Sorting]\%PATHNAME%-%~n1.rar" "%~1\*.*"
make sure to make 3 different batch files and call them accordingly.
If anyone cares here's the accompanying code for the right click context menu.
(just make sure the archiver batch file and the winrar path is added to the path environment variable)
Windows Registry Editor Version 5.00
;--============== CR =============--
[HKEY_CLASSES_ROOT\*\shell\CRARFILE1]
@="Add to SORTING (Last)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\*\shell\CRARFILE1\command]
@="\"(ArchiverLast).bat\" \"%V\""
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER1]
@="Add to SORTING (Last)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER1\command]
@="\"(ArchiverLast).bat\" \"%V\""
[HKEY_CLASSES_ROOT\*\shell\CRARFILE2]
@="Add to SORTING (2ndLast)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\*\shell\CRARFILE2\command]
@="\"(Archiver2ndLast).bat\" \"%V\""
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER2]
@="Add to SORTING (2ndLast)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER2\command]
@="\"(Archiver2ndLast).bat\" \"%V\""
[HKEY_CLASSES_ROOT\*\shell\CRARFILE3]
@="Add to SORTING (3rdLast)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\*\shell\CRARFILE3\command]
@="\"(Archiver3rdLast).bat\" \"%V\""
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER3]
@="Add to SORTING (3rdLast)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER3\command]
@="\"(Archiver3rdLast).bat\" \"%V\""
Delete any entries you don't need (like if you only need the last one)
If you want to do that quickly here's a batch file to do it, it will append this path to your path variable.
@ECHO OFF
setx Path "%PATH%;C:\Program Files\WinRAR\" /m
Hope this helps others, its saved me so much work so far
CodePudding user response:
In your comment you asked: "Now if anyone knows how to get the last 2 directory variables instead of just the last one... it would be awesome haha."
My answer was: "Simple: after set "PATHNAME=%1"
use set "last1=%PATHNAME:\=" & set "last2=!last1!" & set "last1=%" & set "lastTwo=!last2!\!last1!"
The result is in %lastTwo%
"
I think this is fairly simple code (just two lines). Anyway, here it is such a code, with the mod required to also get the third-to-last directory:
@echo off
setlocal EnableDelayedExpansion
set "PATHNAME=%1"
set "last1=%PATHNAME:\=" & set "last3=!last2!" & set "last2=!last1!" & set "last1=%"
echo Full path name: %PATHNAME%
echo Last dir only: %last1%
echo From 2nd to last: %last2%\%last1%
echo From 3rd to last: %last3%\%last2%\%last1%
Output example:
Full path name: DIR1\DIR2\DIR3\DIR4\DIR5
Last dir only: DIR5
From 2nd to last: DIR4\DIR5
From 3rd to last: DIR3\DIR4\DIR5