Computer Hope

Microsoft => Microsoft DOS => Topic started by: Yogesh123 on September 29, 2009, 07:00:33 AM

Title: Reading file properties
Post by: Yogesh123 on September 29, 2009, 07:00:33 AM
Using batch script how to read properties of a perticular file,

Properties like = Last-Modified date, Modified time, file extension, attributes, size, path, name etc..

please advise,
Thanx in advance
Title: Re: Reading file properties
Post by: Etherel15 on October 05, 2009, 07:12:15 PM
I'm no genius, but maybe you can use the For command, and then use the "%~tI" variable, or some of the other offshoots, (instead of %%I) to call up file properties, and then just tell it to ECHO them?
Title: Re: Reading file properties
Post by: billrich on October 05, 2009, 08:28:59 PM
 :D
Title: Re: Reading file properties
Post by: BC_Programmer on October 06, 2009, 02:05:39 AM
um... yeah... not sure what that has to do with the OP's query...
Title: Re: Reading file properties
Post by: billrich on October 06, 2009, 02:59:39 AM
 :D
Title: Re: Reading file properties
Post by: gh0std0g74 on October 06, 2009, 07:12:26 AM
@bill, environment variables or attrib doesn't really provide you file properties details. even with dir, it can't get things like creation date, accessed date.

@OP, you can try using vbscript

Code: [Select]
Set objFS=CreateObject("Scripting.FileSystemObject")
strFileName = WScript.Arguments(0)
Set objFile = objFS.GetFile(strFileName)
WScript.Echo "Date last created: " & objFile.DateCreated
WScript.Echo "Date last modified: " & objFile.DateLastModified
WScript.Echo "Date last accessed: " & objFile.DateLastAccessed
WScript.Echo "File size(bytes): " & objFile.Size
WScript.Echo "File type: " & objFile.Type

save as myscript.vbs and on command line:
Code: [Select]
C:\test>cscript /nologo myscript.vbs test.vbs
Date last created: 8/8/2009 8:20:36 AM
Date last modified: 10/6/2009 9:15:44 PM
Date last accessed: 10/6/2009 9:15:44 PM
File size(bytes): 8943
File type: VBScript Script File

Title: Re: Reading file properties
Post by: BC_Programmer on October 06, 2009, 10:16:07 AM
Yes and "um...yeah" is the perfect solution for Yogesh123, the original poster's question?


I never said it did. If you don't know *censored* your talking about don't post. that is WHY I hadn't posted before- because I don't know a solution, until your post that has absolutely no information of value, in the same way that a person might respond to a post consisting of a copy-paste from Excel help.

Quote
The environmental variables and the CMD commands contain the file properties information Yogesh123 is seeking.
no. They don't. Last-Modified date, Modified time, file extension, attributes, size, path, name- of those, I would guess that the file extension, path, and name are easily available... Oh, and with /t you can get the accessed/modified/created dates. and size.... hey wait a second, you can get all of this info from cmd... But that doesn't change the fact that showing a copy-paste from cmd of you setting and echoing environment variables and listing them is irrelevant.

Also, it can be tricky to parse the exact wanted field from dir, especially given localization concerns. That being said although your original post was a tad odd the concept you then fleshed out was not off the mark at all.

Title: Re: Reading file properties
Post by: billrich on October 07, 2009, 10:18:35 AM
 :D
Title: Re: Reading file properties
Post by: billrich on October 07, 2009, 12:14:13 PM
 :)
Title: Re: Reading file properties
Post by: wbrost on October 07, 2009, 12:38:41 PM
you replace test.vbs with the file you are looking for the info on.

if you wanted to check out test.txt

Code: [Select]
cscript.exe  /nologo myscript.vbs test.txt
Title: Re: Reading file properties
Post by: billrich on October 07, 2009, 02:07:11 PM
 :)
Title: Re: Reading file properties
Post by: gh0std0g74 on October 07, 2009, 05:55:08 PM

But it did not  show the path.
it doesn't take one to quickly find out the way to show Path. I will spoonfeed you for a liitle. try objFile.Path or read the vbscript manual in my sig.

Quote
And I would to see file property information done with a batch file.
if you are talking about PURE batch cmd.exe commands plus those command line tools (excluding cscript.exe ) that comes natively with windows, no, not in my life time. i will leave that privilege to someone else who has time to spare and brain cells to kill
Title: Re: Reading file properties
Post by: billrich on October 07, 2009, 09:07:39 PM
 :)
Title: Re: Reading file properties
Post by: gh0std0g74 on October 07, 2009, 11:42:26 PM
First, the type of file is not the file name. It should give "PDf file" for a file with .pdf extension. Or "Gif file" for .gif extension. If the file is a shortcut, it should display, "shortcut to <whatever original file> "

secondly, you need 3 separate dir commands to show creation, accessed and modified date. if the directory is big with many files and subfolders, it will take 3 times longer to show every files' properties. unless there's a way to combine showing these 3 date types, that's how inflexible it is. you are not able to control how you program.

here's a more detailed version recursively going through the directory to get file properties
Code: [Select]
Set objFS=CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
strFolder= "c:\\test\\"
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
  If objDIR <> "\System Volume Information" Then
    For Each eFolder in objDIR.SubFolders
      Go eFolder
    Next
For Each strFile In objDIR.Files
name=Replace(strFile.Path,"\","\\")
s="Select * from CIM_Datafile Where name = '"& name & "'"
Set colFiles = objWMIService.ExecQuery(s)
For Each objFile in colFiles
    Wscript.Echo "Access mask: " & objFile.AccessMask
    Wscript.Echo "Archive: " & objFile.Archive
    Wscript.Echo "Compressed: " & objFile.Compressed
    Wscript.Echo "Compression method: " & objFile.CompressionMethod
    Wscript.Echo "Creation date: " & objFile.CreationDate
    Wscript.Echo "Computer system name: " & objFile.CSName
    Wscript.Echo "Drive: " & objFile.Drive
    Wscript.Echo "8.3 file name: " & objFile.EightDotThreeFileName
    Wscript.Echo "Encrypted: " & objFile.Encrypted
    Wscript.Echo "Encryption method: " & objFile.EncryptionMethod
    Wscript.Echo "Extension: " & objFile.Extension
    Wscript.Echo "File name: " & objFile.FileName
    Wscript.Echo "File size: " & objFile.FileSize
    Wscript.Echo "File type: " & objFile.FileType
    Wscript.Echo "File system name: " & objFile.FSName
    Wscript.Echo "Hidden: " & objFile.Hidden
    Wscript.Echo "Last accessed: " & objFile.LastAccessed
    Wscript.Echo "Last modified: " & objFile.LastModified
    Wscript.Echo "Manufacturer: " & objFile.Manufacturer
    Wscript.Echo "Name: " & objFile.Name
    Wscript.Echo "Path: " & objFile.Path
    Wscript.Echo "Readable: " & objFile.Readable
    Wscript.Echo "System: " & objFile.System
    Wscript.Echo "Version: " & objFile.Version
    Wscript.Echo "Writeable: " & objFile.Writeable
    WScript.Echo "Install Date: " & objFile.InstallDate
                    Wscript.Echo "-----------------------------------------------------------------"
Next
Next
  End If 
End Sub

Title: Re: Reading file properties
Post by: billrich on October 08, 2009, 09:03:52 AM
 :D
Title: Re: Reading file properties
Post by: BC_Programmer on October 08, 2009, 09:32:13 AM
he requested the attributes and dates... however your solution isn't quite there yet, since it still has  the bulky surounding text output from dir and attrib.

I imagine that some weird text manipulation could be achieved in batch though, to get the dates/attributes directly into environment variables.
Title: Re: Reading file properties
Post by: gh0std0g74 on October 08, 2009, 09:37:25 AM
Ghost,

Since Ghost imports commands from around the world to a Batch Board,
nowhere in the rules says this forum is just for cmd commands. so suck it up.

Quote
I suggest Ghost use Unix( Linux )  commands  "Chmod: or "ls -last" for the attributes of the file.
chmod doesn't even come close to displaying file properties on linux. do you even understand what chmod does? Does OP want to change file permissions? I doubt so, since he doesn't say in his post. Which unix/linux platform are you on ? i don't recognise -last switch from ls. Also, the proper tools to use in *nix to list file properties are stat (or GNU find with printf ).


Quote
The Ghost VBS uses a separate line of code for each attribute.
look carefully again at the code. for each file listed, it display all the attributes of that file.

Quote
Batch only needs attrib.
bull.. show your batch code then...

Quote
By the way, Yogesh123 , the orginal poster,  requested the properties of one file each time and not all the files in the directory or on the the C Drive.
if he just want a script to pass a file name to it each time, its trivial to change. No big deal. just remove the loops....
Title: Re: Reading file properties
Post by: BC_Programmer on October 08, 2009, 09:47:30 AM
I'm looking... but I see no batch board. I see a "Microsoft DOS" board, which has been already discussed to apply to both pure DOS AND windows-based DOS prompts. This includes the windows scripting host, which was designed to replace, at least in some capacity, Batch files.
Title: Re: Reading file properties
Post by: billrich on October 08, 2009, 11:39:28 AM
 :D
Title: Re: Reading file properties
Post by: BC_Programmer on October 08, 2009, 12:10:53 PM
as we say here, shut up and have some pie.


Ok, actually I'm the only one that says it. but you cannot go wrong with pie.
Title: Re: Reading file properties
Post by: james202428 on October 08, 2009, 12:22:29 PM
I don't like most kinds of pies i do like pumkin pie with alot of whipcream though there's alot of people who don't like pie

if america had to agree on one food to eat the rest of there lives in a vote i wonder what it would be i don't think it would be pie
Title: Re: Reading file properties
Post by: billrich on October 08, 2009, 01:54:32 PM
 :D
Title: Re: Reading file properties
Post by: gh0std0g74 on October 08, 2009, 05:12:05 PM
Quote from: billrich
BC_Programmer  talks with great authority but has produced no Batch code or any other code
he has already said in his post he doesn't know a solution. And if he feels like it and wants to write that code, he will, and he certainly knows what he is talking about.
Title: Re: Reading file properties
Post by: Sidewinder on October 10, 2009, 09:09:19 AM
The easiest solution is Ghostdog's VBScript where you can refer to each property by name and not some hieroglyphic token name.

However (sigh) if you insist on a batch solution, this is one way:

Code: [Select]
@echo off
set /p fname=Enter file name:
dir /tc %fname% > dir.txt
for /f "skip=5 tokens=1" %%v in (dir.txt) do (
  echo Date Created:         %%v
  goto next
  )

:next 
dir /ta %fname% > dir.txt
for /f "skip=5 tokens=1" %%v in (dir.txt) do (
  echo Date Accessed:        %%v
  goto next
  )

:next 
dir /tw %fname% > dir.txt
for /f "skip=5 tokens=1" %%v in (dir.txt) do (
  echo Date Written:         %%v
  goto next
  )
 
:next 
dir %fname% > dir.txt
for /f "skip=5 tokens=5" %%v in (dir.txt) do (
  echo Fully Qualified Name: %%~fv
  echo Drive Specification:  %%~dv
  echo Path Specification:   %%~pv
  echo File Name:            %%~nv
  echo File Extension:       %%~xv
  echo Short Name:           %%~sv
  echo File Attributes:      %%~av
  echo File Date/Time:       %%~tv
  echo File Size:            %%~zv
  goto next
  )
 
:next
del dir.txt 
Title: Re: Reading file properties
Post by: billrich on October 10, 2009, 10:32:34 AM
Good Luck
Title: Re: Reading file properties
Post by: Sidewinder on October 10, 2009, 11:31:28 AM
(Sigh) The file I coded does not use a command line parameter, but prompts for the fie name. I'm confused about the file name timeinseconds, when in fact the file reported by the batch file is C:\$WINDOWS.~BT.

The file name and file extension are correct as reported in the output. The attributes are also correct (directory). I have no idea why size was not reported correctly,except directories do not have size properties (at least from the command prompt).

Sidewinder
Title: Re: Reading file properties
Post by: Salmon Trout on October 10, 2009, 11:47:45 AM
This thread is better than many TV soaps.
Title: Re: Reading file properties
Post by: billrich on October 10, 2009, 12:15:00 PM
Good job Sidewinder.  I'm sorry I took up too much of your time.

Keep posting;  you know what you are talking about.
Title: Re: Reading file properties
Post by: Salmon Trout on October 10, 2009, 01:18:01 PM
I used the snake's code except a command line argument instead of a prompt.

You and BC are in the same camp:  "All Hat and no Cattle."

A twinkle in his eye and murder in his trousers, my mother used to say, or "all piss and vinegar"...

Title: Re: Reading file properties
Post by: BC_Programmer on October 10, 2009, 01:33:56 PM
I used the snake's code except a command line argument instead of a prompt.

You and BC are in the same camp:  "All Hat and no Cattle."

Ironic. All you've done since your original post is purposely invent situations in which other peoples solutions break. For example by munging the batch file sidewinder provided. It works fine for me.

Obviously you failed to "convert" it to use a command-line argument, because it works fine here.

for a icon file I have:

Quote

D:\>side
Enter file name: D:\question.ico
Date Created:         09/08/2009
Date Accessed:        09/08/2009
Date Written:         09/08/2009
Fully Qualified Name: D:\question.ico
Drive Specification:  D:
Path Specification:   \
File Name:            question
File Extension:       .ico
Short Name:           D:\question.ico
File Attributes:      --a------
File Date/Time:       09/08/2009 05:39 PM
File Size:            15086

D:\>
It failed when I tried to use it on files from another drive:




Probably easily fixed with a PushD of some form, since it works from that directory:

Quote



D:\>side C:\windows\syswow64\shell32.dll
Enter file name: C:\windows\syswow64\shell32.dll
Date Created:         07/30/2009
Date Accessed:        07/30/2009
Date Written:         04/10/2009
Fully Qualified Name: C:\Windows\SysWOW64\shell32.dll
Drive Specification:  C:
Path Specification:   \Windows\SysWOW64\
File Name:            shell32
File Extension:       .dll
Short Name:           C:\Windows\SysWOW64\shell32.dll
File Attributes:      --a------
File Date/Time:       04/10/2009 11:28 PM
File Size:            11584000


In either case perhaps before making alterations to a batch file Billrich should test it as provided, rather then making changes and proclaiming epic failure even when it's fully possible the changes caused the problem, as I believe to be the case here.







Seems to work fine. Of course I didn't mess about with the innards like a inept surgery intern. I believe the logic at this point speaks for itself for everybody but SpectateSwamp Billrich, who will continue on with analogies that bring back his fond memories of the ranch, and yet have no bearing to the context at all.



In other news, I also converted (somewhat more successfully then the batch code... which  I didn't mess with at all) the VBS file to access only a single file specified as an argument.

Code: [Select]
Set objFS=CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
strFile= wscript.Arguments(0)
Set objFile = objFS.GetFile(strFile)
 
name=Replace(ObjFile.Path,"\","\\")
s="Select * from CIM_Datafile Where name = """ & name & """"
Set colFiles = objWMIService.ExecQuery(s)
For Each objFile in colFiles
    Wscript.Echo "Access mask: " & objFile.AccessMask
    Wscript.Echo "Archive: " & objFile.Archive
    Wscript.Echo "Compressed: " & objFile.Compressed
    Wscript.Echo "Compression method: " & objFile.CompressionMethod
    Wscript.Echo "Creation date: " & objFile.CreationDate
    Wscript.Echo "Computer system name: " & objFile.CSName
    Wscript.Echo "Drive: " & objFile.Drive
    Wscript.Echo "8.3 file name: " & objFile.EightDotThreeFileName
    Wscript.Echo "Encrypted: " & objFile.Encrypted
    Wscript.Echo "Encryption method: " & objFile.EncryptionMethod
    Wscript.Echo "Extension: " & objFile.Extension
    Wscript.Echo "File name: " & objFile.FileName
    Wscript.Echo "File size: " & objFile.FileSize
    Wscript.Echo "File type: " & objFile.FileType
    Wscript.Echo "File system name: " & objFile.FSName
    Wscript.Echo "Hidden: " & objFile.Hidden
    Wscript.Echo "Last accessed: " & objFile.LastAccessed
    Wscript.Echo "Last modified: " & objFile.LastModified
    Wscript.Echo "Manufacturer: " & objFile.Manufacturer
    Wscript.Echo "Name: " & objFile.Name
    Wscript.Echo "Path: " & objFile.Path
    Wscript.Echo "Readable: " & objFile.Readable
    Wscript.Echo "System: " & objFile.System
    Wscript.Echo "Version: " & objFile.Version
    Wscript.Echo "Writeable: " & objFile.Writeable
    WScript.Echo "Install Date: " & objFile.InstallDate
                    Wscript.Echo "-----------------------------------------------------------------"
Next

 


This can be accessed in a batch file:

Code: [Select]

cscript fileattribs.vbs D:\testads2.txt


D:\>fileattribs D:\testads2.txt
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Access mask:
Archive: True
Compressed: False
Compression method:
Creation date: 20090803234730.934563-420
Computer system name: TERATRON
Drive: d:
8.3 file name: d:\testads2.txt
Encrypted: False
Encryption method:
Extension: txt
File name: testads2
File size: 287
File type: Text Document
File system name: NTFS
Hidden: False
Last accessed: 20090803234730.934563-420
Last modified: 20090303190457.875000-480
Manufacturer:
Name: d:\testads2.txt
Path: \
Readable: True
System: False
Version:
Writeable: True
Install Date: 20090803234730.934563-420
-----------------------------------------------------------------









Title: Re: Reading file properties
Post by: billrich on October 10, 2009, 06:58:29 PM
Unable to keep up with such bright people.

Good Job.  The Computer Hope Crew helps many people.
Title: Re: Reading file properties
Post by: BC_Programmer on October 11, 2009, 11:12:49 AM
Quote
After four rewrites

what rewrites? It's ghostdogs script, but I changed it use a filename from the commandline. There was no "rewriting".