Solved

Logic to display container folder name of a batch program

Posted on 2007-04-10
24
249 Views
Last Modified: 2010-04-16
I have a working batch application I would like to enhance to display a title tag with the folder name of the directory I am running the application, for example if I am running the application from c:/inetpub/java it generate an output file (HTML) and I want the title tag to be <title>Java index of HTML files</title>, where Java is basically the value of [container folder name].

The code/reference for the batch application can be found here... http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_22124943.html

Basically I would like to add logic to grab the container folder name and display it somehow on the generated HTML file it creates on execution.

Please any help is appreciated. Thanks!

0
Comment
Question by:a0k0a7
  • 14
  • 10
24 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18885773
Here's changes to the last version of the script posted on the link you specified:

@echo off

setlocal enabledelayedexpansion

set outputName=myfile.html
set exceptionName=exceptions.txt
set filter=*.htm?

set filter=

set startdir=.\

if not "%~1"=="" set startdir=%~1

call :MAKEABSOLUTEDIR startdir "%startdir%"
call :GETLASTDIR lastdir "%startdir%"

set exception=%startdir%%exceptionName%
set output=%startdir%%outputName%

(echo ^<html^>)>"%output%"
(echo ^<head^>)>>"%output%"
(echo Output of %startdir% at %date% %time%^<br^>)>>"%output%"
(echo ^<title^>%lastdir% index of HTML files^</title^>)>>"%output%"
(echo ^</head^>)>>"%output%"
(echo ^<body^>)>>"%output%"

pushd "%startdir%"

if not exist "%exception%" echo %exception% not found. All directories will be processed.

for /f "tokens=*" %%A in ('DIR "%filter%" /b /s /a:-d') do if not "%output%"=="%%A" call :PROCESS "%%A"

(echo ^</body^>)>>"%output%"
(echo ^</html^>)>>"%output%"

"%output%"

del "%output%"

popd

goto :EOF

:MAKEABSOLUTEDIR

set %1=%~f2

goto :EOF

:PROCESS

set href=%~1
set href=!href:%startdir%=!

set matchStr=%~dp1
set matchStr=!matchStr:%startdir%=!

for /f "tokens=*" %%a in ('type "%exception%" ^| find /i "%matchStr%" 2^>NUL') do goto :EOF

(echo ^<a href=^"%href%^"^>%~n1^</a^>^<br^>)>>"%output%"

Good Luck,
Steve
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 18890301
Thanks for the reply but the code for the GETLASTDIR function is missing on the code you provided, I get error message. "The system cannot find the batch label specified - GETLASTDIR"
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18890383
Bad cut and paste job here ;)

@echo off

setlocal enabledelayedexpansion

set outputName=myfile.html
set exceptionName=exceptions.txt
set filter=*.htm?

set filter=

set startdir=.\

if not "%~1"=="" set startdir=%~1

call :MAKEABSOLUTEDIR startdir "%startdir%"
call :GETLASTDIR lastdir "%startdir%"

set exception=%startdir%%exceptionName%
set output=%startdir%%outputName%

(echo ^<html^>)>"%output%"
(echo ^<head^>)>>"%output%"
(echo Output of %startdir% at %date% %time%^<br^>)>>"%output%"
(echo ^<title^>%lastdir% index of HTML files^</title^>)>>"%output%"
(echo ^</head^>)>>"%output%"
(echo ^<body^>)>>"%output%"

pushd "%startdir%"

if not exist "%exception%" echo %exception% not found. All directories will be processed.

for /f "tokens=*" %%A in ('DIR "%filter%" /b /s /a:-d') do if not "%output%"=="%%A" call :PROCESS "%%A"

(echo ^</body^>)>>"%output%"
(echo ^</html^>)>>"%output%"

"%output%"

del "%output%"

popd

goto :EOF

:MAKEABSOLUTEDIR

set %1=%~f2

goto :EOF

:GETLASTDIR

set %1=%~n2

if "!%1!"=="" set %1=\

goto :EOF

:PROCESS

set href=%~1
set href=!href:%startdir%=!

set matchStr=%~dp1
set matchStr=!matchStr:%startdir%=!

for /f "tokens=*" %%a in ('type "%exception%" ^| find /i "%matchStr%" 2^>NUL') do goto :EOF

(echo ^<a href=^"%href%^"^>%~n1^</a^>^<br^>)>>"%output%"

goto :EOF
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 18890679
Its just writing / for the container folder name on the title tag, I try on several directories, any idea why?

-Thanks
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 18890834
May be lastdir variable needs to be set to something?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18891628
I'm sorry that's what I thought you wanted. For instance if the current directory is:

c:\temp\temp  --> temp
c:\mydir           --> mydir
c:\program files\Management System ---> Management System

Let me know what you want and I'll change it.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18891639
If you simply want the absolute directory name then this will work:


@echo off

setlocal enabledelayedexpansion

set outputName=myfile.html
set exceptionName=exceptions.txt
set filter=*.htm?

set filter=

set startdir=.\

if not "%~1"=="" set startdir=%~1

call :MAKEABSOLUTEDIR startdir "%startdir%"

set exception=%startdir%%exceptionName%
set output=%startdir%%outputName%

(echo ^<html^>)>"%output%"
(echo ^<head^>)>>"%output%"
(echo Output of %startdir% at %date% %time%^<br^>)>>"%output%"
(echo ^<title^>%startdir% index of HTML files^</title^>)>>"%output%"
(echo ^</head^>)>>"%output%"
(echo ^<body^>)>>"%output%"

pushd "%startdir%"

if not exist "%exception%" echo %exception% not found. All directories will be processed.

for /f "tokens=*" %%A in ('DIR "%filter%" /b /s /a:-d') do if not "%output%"=="%%A" call :PROCESS "%%A"

(echo ^</body^>)>>"%output%"
(echo ^</html^>)>>"%output%"

"%output%"

del "%output%"

popd

goto :EOF

:MAKEABSOLUTEDIR

set %1=%~f2

goto :EOF

:PROCESS

set href=%~1
set href=!href:%startdir%=!

set matchStr=%~dp1
set matchStr=!matchStr:%startdir%=!

for /f "tokens=*" %%a in ('type "%exception%" ^| find /i "%matchStr%" 2^>NUL') do goto :EOF

(echo ^<a href=^"%href%^"^>%~n1^</a^>^<br^>)>>"%output%"

goto :EOF
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 18891717
The sample you provided :

c:\temp\temp  --> temp
c:\mydir           --> mydir
c:\program files\Management System ---> Management System


Is what I want.... instead both solutions provide the following output.

<title>\ index of HTML files</title>

Any ideas why I am getting a different results?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18892142
Can't say exactly. Here's a model of the batch file I used to test this feature. Can you try it on a number of directories on your machine and post back the results?

@echo off

setlocal enabledelayedexpansion

set startdir=.\

if not "%~1"=="" set startdir=%~1

call :MAKEABSOLUTEDIR startdir "%startdir%"
call :GETLASTDIR lastdir "%startdir%"

echo startdir=%startdir%
echo lastdir=%lastdir%

goto :EOF

:MAKEABSOLUTEDIR

set %1=%~f2

goto :EOF

:GETLASTDIR

set %1=%~n2

if "!%1!"=="" set %1=\

goto :EOF
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 18892428
For the several directories I run this what gets echoed on the command prompt.

Batch application located: D:\Documents and Settings\a0k0a7\publish\IDC\
startdir=D:\Documents and Settings\a0k0a7\publish\IDC\
lastdir=\


Batch application located: D:\Documents and Settings\a0k0a7\STS
startdir=D:\Documents and Settings\a0k0a7\STS
lastdir=\

Batch application located: D:\Documents and Settings\a0k0a7\publish\SAP
startdir=D:\Documents and Settings\a0k0a7\publish\SAP
lastdir=\

It does everything else fine but it assigns always / as the name for the directory.

It seems like the following condition statement is always true...   if "!%1!"=="" set %1=\
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18892516
What version of Windows are you running?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18892561
I just tested it out on my Windows 2000 machine and it works there as well. You could get around this by doing this:

@echo off

setlocal enabledelayedexpansion

set outputName=myfile.html
set exceptionName=exceptions.txt
set filter=*.htm?

set filter=

set startdir=.\

if not "%~1"=="" set startdir=%~1

call :MAKEABSOLUTEDIR startdir "%startdir%"
call :GETLASTDIR lastdir "%startdir%"

if "%lastdir%"=="" set lastdir=\

set exception=%startdir%%exceptionName%
set output=%startdir%%outputName%

(echo ^<html^>)>"%output%"
(echo ^<head^>)>>"%output%"
(echo Output of %startdir% at %date% %time%^<br^>)>>"%output%"
(echo ^<title^>%lastdir% index of HTML files^</title^>)>>"%output%"
(echo ^</head^>)>>"%output%"
(echo ^<body^>)>>"%output%"

pushd "%startdir%"

if not exist "%exception%" echo %exception% not found. All directories will be processed.

for /f "tokens=*" %%A in ('DIR "%filter%" /b /s /a:-d') do if not "%output%"=="%%A" call :PROCESS "%%A"

(echo ^</body^>)>>"%output%"
(echo ^</html^>)>>"%output%"

"%output%"

del "%output%"

popd

goto :EOF

:MAKEABSOLUTEDIR

set %1=%~f2

goto :EOF

:GETLASTDIR

set %1=%~n2

goto :EOF

:PROCESS

set href=%~1
set href=!href:%startdir%=!

set matchStr=%~dp1
set matchStr=!matchStr:%startdir%=!

for /f "tokens=*" %%a in ('type "%exception%" ^| find /i "%matchStr%" 2^>NUL') do goto :EOF

(echo ^<a href=^"%href%^"^>%~n1^</a^>^<br^>)>>"%output%"

goto :EOF
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 8

Author Comment

by:a0k0a7
ID: 18893111
I am running Windows XP SP2.

I am having the same results that leads with / as the directory name. Any ideas why we are having different results?

I noticed there were no set lastdir statement on the code, I am not sure if that could be related to the issue but I tried on different workstations and I got the same result.

Thanks for your help!
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18893141
It's set by referencing %1. I have no idea why you get difference results. Are you running the script exactly as I've posted?
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 18898609
Yes, I am running the script exactly the way it was pasted, I just removed the set filters  =  and commented out the del "%output%" lines. I really don't know why always the container name end up being /.

I tested the batch application on others Windows XP & Windows 2003 Server but no luck.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18898682
Post what you are running...
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18898696
Also post the command line arguments you are running with.

If no command line arguments are being run, then post the directory you are running from.

Don't forget to post that batch file.

Thanks :)
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 18927118
The directory I am running the application is D:\Documents and Settings\a0k0a7\root\STS

I run the application by just double click the batch program on the directory its resides...

For some reason ALWAYS use \ as the directory name. it seems always lastdir is \

My batch looks like below..


@echo off

setlocal enabledelayedexpansion

set outputName=myfile.html
set exceptionName=exceptions.txt
set filter=*.html?

set startdir=.\

if not "%~1"=="" set startdir=%~1

call :MAKEABSOLUTEDIR startdir "%startdir%"
call :GETLASTDIR lastdir "%startdir%"

if "%lastdir%"=="" set lastdir=\

set exception=%startdir%%exceptionName%
set output=%startdir%%outputName%

(echo ^<html^>)>"%output%"
(echo ^<head^>)>>"%output%"
(echo Output of %startdir% at %date% %time%^<br^>)>>"%output%"
(echo ^<title^>%lastdir% index of HTML files^</title^>)>>"%output%"
(echo ^</head^>)>>"%output%"
(echo ^<body^>)>>"%output%"

pushd "%startdir%"

if not exist "%exception%" echo %exception% not found. All directories will be processed.

for /f "tokens=*" %%A in ('DIR "%filter%" /b /s /a:-d') do if not "%output%"=="%%A" call :PROCESS "%%A"

(echo ^</body^>)>>"%output%"
(echo ^</html^>)>>"%output%"

"%output%"

REM del "%output%"

popd

goto :EOF

:MAKEABSOLUTEDIR

set %1=%~f2

goto :EOF

:GETLASTDIR

set %1=%~n2

goto :EOF

:PROCESS

set href=%~1
set href=!href:%startdir%=!

set matchStr=%~dp1
set matchStr=!matchStr:%startdir%=!

for /f "tokens=*" %%a in ('type "%exception%" ^| find /i "%matchStr%" 2^>NUL') do goto :EOF

(echo ^<a href=^"%href%^"^>%~n1^</a^>^<br^>)>>"%output%"

goto :EOF
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18927445
Okay, I see:

@echo off

setlocal enabledelayedexpansion

set outputName=myfile.html
set exceptionName=exceptions.txt
set filter=*.html?

set startdir=.\

if not "%~1"=="" set startdir=%~1

call :MAKEABSOLUTEDIR startdir "%startdir%"
call :GETLASTDIR lastdir "%startdir%"

set exception=%startdir%%exceptionName%
set output=%startdir%%outputName%

(echo ^<html^>)>"%output%"
(echo ^<head^>)>>"%output%"
(echo Output of %startdir% at %date% %time%^<br^>)>>"%output%"
(echo ^<title^>%lastdir% index of HTML files^</title^>)>>"%output%"
(echo ^</head^>)>>"%output%"
(echo ^<body^>)>>"%output%"

pushd "%startdir%"

if not exist "%exception%" echo %exception% not found. All directories will be processed.

for /f "tokens=*" %%A in ('DIR "%filter%" /b /s /a:-d') do if not "%output%"=="%%A" call :PROCESS "%%A"

(echo ^</body^>)>>"%output%"
(echo ^</html^>)>>"%output%"

"%output%"

REM del "%output%"

popd

goto :EOF

:MAKEABSOLUTEDIR

set %1=%~f2

goto :EOF

:GETLASTDIR

set _t=%~2

if "%_t:~3,1%"=="" set %1=\&goto :EOF

if not "%_t:~-1%"=="\" set %1=%~n2&goto :EOF

set _t=%_t:~0,-1%

call :GETLASTDIR %1 %_t%

goto :EOF

:PROCESS

set href=%~1
set href=!href:%startdir%=!

set matchStr=%~dp1
set matchStr=!matchStr:%startdir%=!

for /f "tokens=*" %%a in ('type "%exception%" ^| find /i "%matchStr%" 2^>NUL') do goto :EOF

(echo ^<a href=^"%href%^"^>%~n1^</a^>^<br^>)>>"%output%"

goto :EOF
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 18932449
I appreciate a lot your efforts it works but there's a minor bug.  It seems it gets confused when the directory contains spaces i.e. d:/documents and settings/a0k0a7.

For example if, I trigger the batch...

D:\Temp\SQL 2005 cd1\Setup\IDC   --    it display:  SQL index of HTML files

This case it works fine...

D:\Temp\SQL_2005_cd1\Setup\IDC -- it display: IDC index of HTML fles

If you think could be a workaround please let me know, thanks!
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18932519
Sorry about that:

@echo off

setlocal enabledelayedexpansion

set outputName=myfile.html
set exceptionName=exceptions.txt
set filter=*.html?

set startdir=.\

if not "%~1"=="" set startdir=%~1

call :MAKEABSOLUTEDIR startdir "%startdir%"
call :GETLASTDIR lastdir "%startdir%"

set exception=%startdir%%exceptionName%
set output=%startdir%%outputName%

(echo ^<html^>)>"%output%"
(echo ^<head^>)>>"%output%"
(echo Output of %startdir% at %date% %time%^<br^>)>>"%output%"
(echo ^<title^>%lastdir% index of HTML files^</title^>)>>"%output%"
(echo ^</head^>)>>"%output%"
(echo ^<body^>)>>"%output%"

pushd "%startdir%"

if not exist "%exception%" echo %exception% not found. All directories will be processed.

for /f "tokens=*" %%A in ('DIR "%filter%" /b /s /a:-d') do if not "%output%"=="%%A" call :PROCESS "%%A"

(echo ^</body^>)>>"%output%"
(echo ^</html^>)>>"%output%"

"%output%"

REM del "%output%"

popd

goto :EOF

:MAKEABSOLUTEDIR

set %1=%~f2

goto :EOF


:GETLASTDIR

set _t=%~2

if "%_t:~3,1%"=="" set %1=\&goto :EOF

if not "%_t:~-1%"=="\" set %1=%~n2&goto :EOF

set _t=%_t:~0,-1%

call :GETLASTDIR %1 "%_t%"

goto :EOF
goto :EOF

:PROCESS

set href=%~1
set href=!href:%startdir%=!

set matchStr=%~dp1
set matchStr=!matchStr:%startdir%=!

for /f "tokens=*" %%a in ('type "%exception%" ^| find /i "%matchStr%" 2^>NUL') do goto :EOF

(echo ^<a href=^"%href%^"^>%~n1^</a^>^<br^>)>>"%output%"

goto :EOF
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 18932522
Here's a version without the double goto pasting error :)

@echo off

setlocal enabledelayedexpansion

set outputName=myfile.html
set exceptionName=exceptions.txt
set filter=*.html?

set startdir=.\

if not "%~1"=="" set startdir=%~1

call :MAKEABSOLUTEDIR startdir "%startdir%"
call :GETLASTDIR lastdir "%startdir%"

set exception=%startdir%%exceptionName%
set output=%startdir%%outputName%

(echo ^<html^>)>"%output%"
(echo ^<head^>)>>"%output%"
(echo Output of %startdir% at %date% %time%^<br^>)>>"%output%"
(echo ^<title^>%lastdir% index of HTML files^</title^>)>>"%output%"
(echo ^</head^>)>>"%output%"
(echo ^<body^>)>>"%output%"

pushd "%startdir%"

if not exist "%exception%" echo %exception% not found. All directories will be processed.

for /f "tokens=*" %%A in ('DIR "%filter%" /b /s /a:-d') do if not "%output%"=="%%A" call :PROCESS "%%A"

(echo ^</body^>)>>"%output%"
(echo ^</html^>)>>"%output%"

"%output%"

REM del "%output%"

popd

goto :EOF

:MAKEABSOLUTEDIR

set %1=%~f2

goto :EOF

:GETLASTDIR

set _t=%~2

if "%_t:~3,1%"=="" set %1=\&goto :EOF

if not "%_t:~-1%"=="\" set %1=%~n2&goto :EOF

set _t=%_t:~0,-1%

call :GETLASTDIR %1 "%_t%"

goto :EOF

:PROCESS

set href=%~1
set href=!href:%startdir%=!

set matchStr=%~dp1
set matchStr=!matchStr:%startdir%=!

for /f "tokens=*" %%a in ('type "%exception%" ^| find /i "%matchStr%" 2^>NUL') do goto :EOF

(echo ^<a href=^"%href%^"^>%~n1^</a^>^<br^>)>>"%output%"

goto :EOF
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 18932892
It works!!!!!!!!!!!!!!!!

Thank you so much for you help, best wishes!

0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18933452
Thanks :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now