Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 


Sign in to follow this  
braindedd

Batch file help

Recommended Posts

Hi guys,

Trying to get a batch file to set the current directory name only to a variable ... best I've come up with is this:

@ECHO OFF
set Test2=%CD%
cd ..
set Test1=%CD%
set Test1=%Test1%\
cd %Test2%
ECHO @echo off >"%Temp%\Test2.bat"
ECHO Set Test=%%CD:%Test1%=%% >>"%Temp%\Test2.bat"
rem ECHO echo %%test%% >>"%Temp%\Test2.bat"
call "%Temp%\Test2.bat"
del "%Temp%\Test2.bat"
echo %Test%

Must be an easier way to do it ... anyone?

Share this post


Link to post
Share on other sites

echo %CD% | mawk -F\\ "{print $NF}" > cwd.tmp

set /p CWD=<cwd.tmp

del cwd.tmp

echo %CWD%

http://www.klabaster.com/freeware.htm#mawk

(if you don't mind using an external program)

Also, run:

call /?

This might help you, if you can figure out a way to use the special %~ variables.

-John

Edited by jftuga

Share this post


Link to post
Share on other sites

Will this help

@IF %~D0\ EQU %~DP0 @ECHO/%~NX0 is at root level &GOTO :EOF
@FOR /F "DELIMS=" %%? IN ("%CD%") DO @ECHO/The `Parent Folder` is %%~N?

Share this post


Link to post
Share on other sites

Thanks guys.

Yzöwl, your script looks good, just have to find a way that it works if run from a different directory?

Share this post


Link to post
Share on other sites

My command which performs the task you require would still be the same, just the first line, (error trapping), would change.

@IF %CD:*:=% EQU \ @ECHO/The current directory is at root level &GOTO :EOF
@FOR /F "DELIMS=" %%? IN ("%CD%") DO @ECHO/The `Parent Folder` is %%~N?

Share this post


Link to post
Share on other sites

This VBS script does the same but uses VBS to pass the Folder and Path varibles back to Cmd Promt

Save As LastFolder.vbs

Dim Fso   : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Act : Set Act = CreateObject("Wscript.Shell")
Dim File,Folder,StrF
StrF = Fso.GetFolder(Fso.GetParentFolderName(WScript.ScriptFullName))
Set Folder = Fso.GetFolder(StrF)
Act.Run("%Comspec% /c @Echo Off && CLS && Color F9 && Mode 75,5 && Title Folder Path && Echo. && " &_
"Echo -^> The Last Folder : "&Folder.Name&" && Echo -^> The Folder Path : " &StrF& " && Pause"),1,True

This Script will pass all the Varibles from an Array to the Cmd Promt Screen.

This will show the Last Folder Name , Start Folder Path, Start Folder Date Created,

Start Folder Last Aceess Date, Start Folder Size, this will then be dispalyed in Cmd

Promt, each with it own text color and Cmd Bar Title.

Save As LastFolderV1.vbs

Dim Fso	: Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Act : Set Act = CreateObject("Wscript.Shell")
Dim Folder : Set Folder = Fso.GetFolder(Fso.GetParentFolderName(WScript.ScriptFullName))
'/-> Array To Build The Cmd Promt Output
Dim VAR : VAR = Array(_
" && ","CLS && Echo.","ping -n 3 127.0.0.1>nul","ping -n 4 127.0.0.1>nul",_
Int(1024 * 1024),Int(1024 * 1024 * 1024),Folder.Size,_
"CLS && Color F9 && Mode 45,3 && Title Last Folder && Echo. ",_
"CLS && Color F4 && Mode 85,3 && Title Folder Path && Echo. ",_
"CLS && Color F2 && Mode 50,3 && Title Date Created && Echo. ",_
"CLS && Color F5 && Mode 55,3 && Title Date Last Modified && Echo. ",_
"CLS && Color F8 && Mode 35,3 && Title Folder Size && Echo. ",_
" && Echo -^> Processing Folder "," && Echo -^> ","Path Last Name",_
"Path","Created Date","Last Modified Date","Total Size")
'/-> Converts Folder Size To MB Or GB
If VAR(6) > VAR(5) Then
VAR(6) = Left(Folder.Size /VAR(5),4) & " GB"
Else
VAR(6) = Left(Folder.Size /VAR(4),4) & " MB"
End If
'/-> Pass All The Varibles To Cmd Promt
Act.Run("%Comspec% /c @Echo Off && " & VAR(7) &_
VAR(12) & VAR(14) & VAR(0) & VAR(2) & VAR(0) & VAR(1) &_
VAR(13) & VAR(14) & " : " & Folder.Name & VAR(0) & VAR(3) & VAR(0) & VAR(8) &_
VAR(12) & VAR(15) & VAR(0) & VAR(2) & VAR(0) & VAR(1) &_
VAR(13) & VAR(15) & " : " & Folder.Path & VAR(0) & VAR(3) & VAR(0) & VAR(9) &_
VAR(12) & VAR(16) & VAR(0) & VAR(2) & VAR(0) & VAR(1) &_
VAR(13) & VAR(16) & " : " & Folder.DateCreated & VAR(0) & VAR(3) & VAR(0) & VAR(10) &_
VAR(12) & VAR(17) & VAR(0) & VAR(2) & VAR(0) & VAR(1) &_
VAR(13) & VAR(17) & " : " & Folder.DateLastModified & VAR(0) & VAR(3) & VAR(0) & VAR(11) &_
VAR(12) & VAR(18) & VAR(0) & VAR(2) & VAR(0) & VAR(1) & VAR(13) & VAR(18) & " : " & VAR(6) &_
VAR(0) & VAR(3)),1,True

Edited by gunsmokingman

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...