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
Solved

Logic to display container folder name of a batch program

Posted on 2007-04-10
24
255 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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