Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Logic to display container folder name of a batch program

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
a0k0a7
Asked:
a0k0a7
  • 14
  • 10
1 Solution
 
SteveGTRCommented:
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
 
a0k0a7Author Commented:
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
 
SteveGTRCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
a0k0a7Author Commented:
Its just writing / for the container folder name on the title tag, I try on several directories, any idea why?

-Thanks
0
 
a0k0a7Author Commented:
May be lastdir variable needs to be set to something?
0
 
SteveGTRCommented:
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
 
SteveGTRCommented:
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
 
a0k0a7Author Commented:
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
 
SteveGTRCommented:
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
 
a0k0a7Author Commented:
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
 
SteveGTRCommented:
What version of Windows are you running?
0
 
SteveGTRCommented:
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
 
a0k0a7Author Commented:
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
 
SteveGTRCommented:
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
 
a0k0a7Author Commented:
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
 
SteveGTRCommented:
Post what you are running...
0
 
SteveGTRCommented:
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
 
a0k0a7Author Commented:
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
 
SteveGTRCommented:
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
 
a0k0a7Author Commented:
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
 
SteveGTRCommented:
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
 
SteveGTRCommented:
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
 
a0k0a7Author Commented:
It works!!!!!!!!!!!!!!!!

Thank you so much for you help, best wishes!

0
 
SteveGTRCommented:
Thanks :)
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 14
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now