?
Solved

Logic to display container folder name of a batch program

Posted on 2007-04-10
24
Medium Priority
?
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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
 
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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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