Solved

Logic to display container folder name of a batch program

Posted on 2007-04-10
24
246 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
May be lastdir variable needs to be set to something?
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
What version of Windows are you running?
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Author Comment

by:a0k0a7
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Post what you are running...
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It works!!!!!!!!!!!!!!!!

Thank you so much for you help, best wishes!

0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Thanks :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
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 …
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

11 Experts available now in Live!

Get 1:1 Help Now