ADSM-L

Re: Batch script in Windows to create filelist

2006-08-21 20:30:10
Subject: Re: Batch script in Windows to create filelist
From: Andrew Raibeck <storman AT US.IBM DOT COM>
To: ADSM-L AT VM.MARIST DOT EDU
Date: Mon, 21 Aug 2006 18:27:58 -0600
The first version of the script did not account for file names containing
spaces. Now file names will be written with quotes around them.

Also, the script does not traverse subdirectories. While that might be
easily done, you can also use a batch file to wrap the script.

New version of script:

==========================================================
' (c) Copyright by IBM Corporation 2006. All Rights Reserved.

' makefilelist.vbs - Version 1.2

' This script takes three arguments:

'   - The fully-qualified directory name to scan
'   - The maximum number of days between now and the time the file was
'     last modified for which the file will be flagged
'   - The name of the output file that contains the resulting file list

' This code has not been through any formal testing, and is not
' warranted by IBM. Use at your own risk.



Const ForWriting   = 2
Const ForAppending = 8
today              = Date
nArgs              = WScript.Arguments.Count()

If nArgs = 3 Then
  ' Add 0 to coerce the second argument to a number.
  BuildFileList WScript.Arguments.Item(0), _
                WScript.Arguments.Item(1) + 0, _
                WScript.Arguments.Item(2)
Else
  WScript.Echo "Usage: cscript makefilelist <directory> <days> " & _
               "<output file name>"
End If



Function BuildFileList(dir, days, outFile)
  ' Open the output file.
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objOutFile = objFSO.OpenTextFile(outFile, ForAppending, True)

  ' List the files in the directory specified by the user.
  Set objWMIService = GetObject("winmgmts:" & _
                        "{impersonationLevel=impersonate}!\\." & _
                        "\root\cimv2")
  Set colFiles = objWMIService.ExecQuery("ASSOCIATORS OF " & _
                   "{Win32_Directory.Name='" & dir & _
                   "'} Where ResultClass = CIM_DataFile")

  ' Check each file to see if it has been modified within the last
  ' <days> days.
  For Each objFile in colFiles
    ' WScript.Echo objFile.LastModified & ": " & objFile.Name

    ' Uncomment the line above if you want to see what the LastModified
    ' field looks like. The format is:
    '
    '   yyyyMMddhhmmss.nnnnnn#uuu
    '
    ' yyyy   = year
    ' MM     = month
    ' dd     = day of month
    ' hh     = hour
    ' mm     = minute
    ' ss     = second
    ' nnnnnn = fraction of a second
    ' #      = '+' or '-' offset from UTC
    ' uuu    = offset from UTC in minutes

    lastMod = Left(objFile.LastModified, 4)   & "-" & _
              Mid(objFile.LastModified, 5, 2) & "-" & _
              Mid(objFile.LastModified, 7, 2)

    diff = DateDiff("d", lastMod, today)

    If diff <= days Then
      ' Write the file name with double quotes around it.
      objOutFile.WriteLine("""" & objFile.Name & """")
    End If
  Next

  objOutFile.Close()
End Function
==========================================================

And batch file if you want to traverse subdirectories of the input
directory (quick & dirty, error checking left as an exercise). Input parms
are the same as for the VBScript file.

==========================================================
@echo off

if exist %3 del %3

for /r %1 %%a in (.) do (@echo Processing directory %%a & @cscript
makefilelist.vbs //nologo "%%a" %2 %3)
==========================================================

Andy Raibeck
IBM Software Group
Tivoli Storage Manager Client Development
Internal Notes e-mail: Andrew Raibeck/Tucson/IBM@IBMUS
Internet e-mail: storman AT us.ibm DOT com

IBM Tivoli Storage Manager support web page:
http://www-306.ibm.com/software/sysmgmt/products/support/IBMTivoliStorageManager.html

The only dumb question is the one that goes unasked.
The command line is your friend.
"Good enough" is the enemy of excellence.

"ADSM: Dist Stor Manager" <ADSM-L AT VM.MARIST DOT EDU> wrote on 08/21/2006
12:55:55 PM:

> Standard command line commands are insufficient, so VBScript would be
the
> answer. I'm not overly familiar with VBScript, but a couple of hours or
so
> with a reference on Windows scripting and some searches at
> http://msdn.microsoft.com allowed me to come up with the following:
>
> ==========================================================
> ' (c) Copyright by IBM Corporation 2006. All Rights Reserved.
>
> ' makefilelist.vbs
>
> ' This script takes three arguments:
>
> '   - The fully-qualified directory name to scan
> '   - The maximum number of days between now and the time the file was
> '     last modified for which the file will be flagged
> '   - The name of the output file that contains the resulting file list
>
> ' This code has not been through any formal testing, and is not
> ' warranted by IBM. Use at your own risk.
>
>
>
> Const ForWriting = 2
> today            = Date
> nArgs            = WScript.Arguments.Count()
>
> If nArgs = 3 Then
>   ' Add 0 to coerce the second argument to a number.
>   BuildFileList WScript.Arguments.Item(0), _
>                 WScript.Arguments.Item(1) + 0, _
>                 WScript.Arguments.Item(2)
> Else
>   WScript.Echo "Usage: cscript makefilelist <directory> <days> " & _
>                "<output file name>"
> End If
>
>
>
> Function BuildFileList(dir, days, outFile)
>   ' Open the output file.
>   Set objFSO = CreateObject("Scripting.FileSystemObject")
>   Set objOutFile = objFSO.OpenTextFile(outFile, ForWriting, True)
>
>   ' List the files in the directory specified by the user.
>   Set objWMIService = GetObject("winmgmts:" & _
>                         "{impersonationLevel=impersonate}!\\." & _
>                         "\root\cimv2")
>   Set colFiles = objWMIService.ExecQuery("ASSOCIATORS OF " & _
>                    "{Win32_Directory.Name='" & dir & _
>                    "'} Where ResultClass = CIM_DataFile")
>
>   ' Check each file to see if it has been modified within the last
>   ' <days> days.
>   For Each objFile in colFiles
>     ' WScript.Echo objFile.LastModified & ": " & objFile.Name
>
>     ' Uncomment the line above if you want to see what the LastModified
>     ' field looks like. The format is:
>     '
>     '   yyyyMMddhhmmss.nnnnnn?uuu
>     '
>     ' yyyy   = year
>     ' MM     = month
>     ' dd     = day of month
>     ' hh     = hour
>     ' mm     = minute
>     ' ss     = second
>     ' nnnnnn = fraction of a second
>     ' ?      = '+' or '-' offset from UTC
>     ' uuu    = offset from UTC in minutes
>
>     lastMod = Left(objFile.LastModified, 4)   & "-" & _
>               Mid(objFile.LastModified, 5, 2) & "-" & _
>               Mid(objFile.LastModified, 7, 2)
>
>     diff = DateDiff("d", lastMod, today)
>
>     If diff <= days Then
>       objOutFile.WriteLine(objFile.Name)
>     End If
>   Next
>
>   objOutFile.Close()
> End Function
> ==========================================================
>
> Note that I make no promises that this will do what you need, but at
least
> it might present a starting point.
>
> Regards,
>
> Andy
>
> Andy Raibeck
> IBM Software Group
> Tivoli Storage Manager Client Development
> Internal Notes e-mail: Andrew Raibeck/Tucson/IBM@IBMUS
> Internet e-mail: storman AT us.ibm DOT com
>
> IBM Tivoli Storage Manager support web page:
> http://www-306.ibm.
> com/software/sysmgmt/products/support/IBMTivoliStorageManager.html
>
> The only dumb question is the one that goes unasked.
> The command line is your friend.
> "Good enough" is the enemy of excellence.
>
> "ADSM: Dist Stor Manager" <ADSM-L AT VM.MARIST DOT EDU> wrote on 08/18/2006
> 08:07:29 AM:
>
> > Hi,
> >
> > I need to create a filelist of files that exist in a directory that
> > have been updated in the past 7 days and have the list of files
> > backed up by TSM. Does anyone know how tis can be done be commands
> > native to Windows without requiring any third party software ?
> >
> > Rich

<Prev in Thread] Current Thread [Next in Thread>