Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating reusable code in Windows scripts

Status
Not open for further replies.

DayLaborer

Programmer
Jan 3, 2006
347
US
I am trying to write a script that will show the name of all files in directory
and their age. I got this to work by creating a single CMD file. However, when I split
the code into two CMD files so that some of it can be reused elsewhere, I am having
difficulty passing variables back from the child CMD file to the parent CMD file. Please help.

Code:
REM *************************************************************************************
REM abc.cmd
REM this script is supposed to display the name of each file in a directory
REM and its age. 

for /f "tokens=1,4" %%a in ('dir D:\MSG\*.TXT ^| find "/"') do (
  call age.cmd %%a
  echo %%b: %difference%
)
REM *************************************************************************************


REM *************************************************************************************
REM age.cmd
REM this script takes a date in mm/dd/yyyy format as an input parameter.  It finds
REM the number of days between this date and the current system date by running
REM an SQL Server query.  It connects to the SQL server database using
REM Windows authentication.  
REM NOTE: I have tested this code separately and it works fine.  The problem is 
REM that I cannot pass a variable back to the CMD file that calls this CMD file.

for /f %%t in ('isql -S ^(local^) -E -Q "select datediff(d,getdate(),'%1')" ^| find "-" ^| findstr /R /C:"[0-9]"') do set difference=%%t

REM *************************************************************************************
Thanks!
Eliezer
 
Use vbscript instead. You can use functions which are re-usable code.

Here is a sample to do what you have requested, plus my sample will format the output for you into columns.

Code:
FolderPath = "C:\Windows"
ReportFileAges FolderPath




Function ReportFileAges(strFolder)
	Dim objFSO
	Dim oFolder
	Dim oFile
	Dim fileAge
	Dim padding

	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set oFolder = objFSO.GetFolder(strFolder)
	Wscript.Echo "Files In Folder " & strFolder
	Wscript.Echo
	Wscript.Echo "File Name" & Space(16) & "File Size"
	Wscript.Echo "_____________________________________"
	For Each oFile in oFolder.Files
		padding = 25 - Len(oFile.Name)
		fileAge = DateDiff("d",oFile.DateCreated,Now)
		Wscript.echo oFile.Name & Space(padding) & fileAge
	Next

End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Correction to the report in the script above:

Change: Wscript.Echo "File Name" & Space(16) & "File Size"
To: Wscript.Echo "File Name" & Space(16) & "File [red]Age[/red]"

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top