Home / Microsoft / Microsoft DOS / Decypher file attribute in a FOR loop
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: 1 2 [All] - (Bottom) Print
Author Topic: Decypher file attribute in a FOR loop  (Read 964 times)
kwc1059
Topic Starter
Greenhorn



Posts: 6


« on: November 18, 2009, 01:45:43 PM »

I am running a .bat file using the "for" command to run through files in a directory.  I am retrieving the file name and date created through the appropriate variables.  If the file is over a certain age I delete it.  I want to add code to check if the file has been archived.  I added the variable to get the file attribute.  However, I am having trouble decyphering the format.  Can someone help me out here???  Here is the "for" command:


for /r C:\Work\File %%a in ("*.*") do call :delfile %%~sa %%~ta %%~aa


Thanks,
Ken
IP logged
Salmon Trout
Prodigy



Thanked: 501
Posts: 7,365

Computer: Specs
Experience: Guru
OS: Linux variant

1
« Reply #1 on: November 18, 2009, 03:35:22 PM »

For %~a recognizes 9 attributes. In the 9 character string returned, if a particular attribute is present, the character in the corresponding position is shown. if the attribute is absent, then a dash is shown.

For example, if the archive bit is set, then the third character is 'a', if it is unset, then the third character is '-'.

Code: [Select]
Directory        d--------
Readonly         -r-------
Archive          --a------
Hidden           ---h-----
System           ----s----
Compressed       -----c---
Offline          ------o--
Temporary        -------t-
Reparse_Point    --------l
« Last Edit: November 19, 2009, 12:27:44 AM by Salmon Trout » IP logged

billrich
Guest
« Reply #2 on: November 18, 2009, 07:03:31 PM »

Hello
« Last Edit: November 21, 2009, 10:26:31 PM by billrich » IP logged
gh0std0g74
Apprentice



Thanked: 37
Posts: 590


« Reply #3 on: November 18, 2009, 07:58:31 PM »

@OP, ignore bill as he is trolling. Follow Salmon's method and you should be fine
IP logged

billrich
Guest
« Reply #4 on: November 18, 2009, 08:03:05 PM »

@OP, ignore bill as he is trolling. Follow Salmon's method and you should be fine

IP logged
BC_Programmer
Mastermind


Thanked: 682
Posts: 15,624

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #5 on: November 18, 2009, 08:49:54 PM »


What is delfile?


A Label in the batch file that the OP has taken the excerpt from.
IP logged

kwc1059
Topic Starter
Greenhorn



Posts: 6


« Reply #6 on: November 19, 2009, 08:23:28 AM »

Here is more of the code.  The delfile is a label to the body of the code.  Any way that I attempt to check the contents of %5 I get an error.  I am not a DOS expert.  I know enough to be dangerous but not effective!!


for /r C:\Work\File %%a in ("*.*") do call :delfile %%~sa %%~ta %%~aa

goto :end


:delfile %1 %2 %3 %4 %5

echo %1
echo %2
echo %3
echo %4
echo %5

if %5:~2,1% EQU "a"

IP logged
Salmon Trout
Prodigy



Thanked: 501
Posts: 7,365

Computer: Specs
Experience: Guru
OS: Linux variant

1
« Reply #7 on: November 19, 2009, 08:58:40 AM »

(a)

:delfile is only seeing 4 passed parameters, so your attempt to process the 5th is causing an error.

they are:

1. %%~sa short file name of file which %%a expands to.

%%~ta expands to date [space] time stamp of %%a, so:

2. date part of %%~ta
3. time part of %%~ta

and finally

4. %%~aa attributes of %%a

(b)

%5 is a passed parameter. You cannot just tack another % sign on the end and treat it like an ordinary variable.



IP logged

kwc1059
Topic Starter
Greenhorn



Posts: 6


« Reply #8 on: November 19, 2009, 09:12:23 AM »

%4 contains the AM/PM part of the time. 

I attempted to set variable fattr to %5.  When I echo that variable there is no value in it.  Here is the output.



C:\Work\File Purge>for /R C:\Work\File %a in ("*.*") do call :delfile %~sa %~ta %~aa

C:\Work\File Purge>call :delfile C:\Work\File\AIMS_I~1.TXT 02/18/2008 02:36 PM --a------

C:\Work\File Purge>echo 1 = C:\Work\File\AIMS_I~1.TXT
1 = C:\Work\File\AIMS_I~1.TXT

C:\Work\File Purge>echo 2 = 02/18/2008
2 = 02/18/2008

C:\Work\File Purge>echo 3 = 02:36
3 = 02:36

C:\Work\File Purge>echo 4 = PM
4 = PM

C:\Work\File Purge>echo 5 = --a------
5 = --a------

C:\Work\File Purge>set fattr = --a------

C:\Work\File Purge>echo 
ECHO is on.
IP logged
Salmon Trout
Prodigy



Thanked: 501
Posts: 7,365

Computer: Specs
Experience: Guru
OS: Linux variant

1
« Reply #9 on: November 19, 2009, 09:21:20 AM »

Oh so you are using US time format so I was wrong about the date & time (I am in a locale that use 24 hour time format)

Anyhow

don't use spaces in SET statements.

set fattr=%5

and, you don't need the parameter list after the subroutine label

:delfile %1 %2 %3 %4 %5
« Last Edit: November 19, 2009, 09:34:23 AM by Salmon Trout » IP logged

kwc1059
Topic Starter
Greenhorn



Posts: 6


« Reply #10 on: November 19, 2009, 10:33:40 AM »

OK, I got it working. 

Thanks for your help! 


Ken
IP logged
Salmon Trout
Prodigy



Thanked: 501
Posts: 7,365

Computer: Specs
Experience: Guru
OS: Linux variant

1
« Reply #11 on: November 19, 2009, 11:18:18 AM »

Quote from: Me
don't use spaces in SET statements.

That was a bit hasty; you must not put a space after the variable name, before the equals sign; you can put a space or spaces after the equals sign. But it or they will be part of the string assigned to the variable.

Code: [Select]
set variable =value
            ^
            |
            ^
 This space is an error; %variable% will be empty
 
 set variable= value
              ^
              |
              ^

 This space is allowed; it results
 in the string held in %variable% having a leading space

IP logged

billrich
Guest
« Reply #12 on: November 19, 2009, 01:31:22 PM »

Hello
« Last Edit: November 21, 2009, 10:37:42 PM by billrich » IP logged
kwc1059
Topic Starter
Greenhorn



Posts: 6


« Reply #13 on: November 19, 2009, 01:42:54 PM »

Well, there is nothing secret about what I am doing.  Here ya go.



for /r C:\Work\File %%a in ("*.*") do call :delfile %%~sa %%~ta %%~aa

goto :end


:delfile

echo 1 = %1
echo 2 = %2
echo 3 = %3
echo 4 = %4
echo 5 = %5

set fattr=%5

echo %fattr:~2,1%

if %fattr:~2,1% == a goto :end


set fdate=%2

set fdate
 
set mm=%fdate:~0,2%
set dd=%fdate:~3,2%
set yy=%fdate:~6,4%

set /a jd2=%yy%*1000

if %mm% GTR 01 set /a jd2=%jd2%+31
if %mm% GTR 02 set /a jd2=%jd2%+28
if %mm% GTR 03 set /a jd2=%jd2%+31
if %mm% GTR 04 set /a jd2=%jd2%+30
if %mm% GTR 05 set /a jd2=%jd2%+31
if %mm% GTR 06 set /a jd2=%jd2%+30
if %mm% GTR 07 set /a jd2=%jd2%+31
if %mm% GTR 08 set /a jd2=%jd2%+31
if %mm% GTR 09 set /a jd2=%jd2%+30
if %mm% GTR 10 set /a jd2=%jd2%+31
if %mm% GTR 11 set /a jd2=%jd2%+30
if %mm% GTR 12 set /a jd2=%jd2%+31

set /a jd2=%jd2%+%dd%

for /f "tokens=1-3 delims=/- " %%a in ('date /t') do (
set mm=%%a
set dd=%%b
set yy=%%c
)

set /a jd1=%yy%*1000

if %mm% GTR 01 set /a jd1=%jd1%+31
if %mm% GTR 02 set /a jd1=%jd1%+28
if %mm% GTR 03 set /a jd1=%jd1%+31
if %mm% GTR 04 set /a jd1=%jd1%+30
if %mm% GTR 05 set /a jd1=%jd1%+31
if %mm% GTR 06 set /a jd1=%jd1%+30
if %mm% GTR 07 set /a jd1=%jd1%+31
if %mm% GTR 08 set /a jd1=%jd1%+31
if %mm% GTR 09 set /a jd1=%jd1%+30
if %mm% GTR 10 set /a jd1=%jd1%+31
if %mm% GTR 11 set /a jd1=%jd1%+30
if %mm% GTR 12 set /a jd1=%jd1%+31

set /a jd1=%jd1%+%dd%

if %mm% EQU 01 (
if %dd% GEQ 06 (
set /a jd1=%jd1%-8
)
)

if %mm% GTR 01 (
set /a jd1=%jd1%-8
)

if %jd2% LSS %jd1% (
echo delete file %1
del %1
)

:end
IP logged
Salmon Trout
Prodigy



Thanked: 501
Posts: 7,365

Computer: Specs
Experience: Guru
OS: Linux variant

1
« Reply #14 on: November 19, 2009, 01:46:36 PM »

Code: [Select]
if %fattr:~2,1% == a goto :end
That won't work. Lose the spaces around the equals signs, and also quotes are usual

This will

Code: [Select]
if "%fattr:~2,1%"=="a" goto :end
what's the logic of these lines

Code: [Select]
if %mm% GTR 01
etc?

is jd Julian date?









IP logged

kwc1059
Topic Starter
Greenhorn



Posts: 6


« Reply #15 on: November 19, 2009, 02:28:09 PM »

My tests on the attribute variable seemed to work but I will give your suggestion a try.

Code: [Select]
if %mm% GTR 01 set /a jd1=%jd1%+31
I am creating two dates in the format yyyyddd.  I start by taking the year and multiplying by 1000.  Then I have to add days based on the month.  If the month is March then I need to add the days for January and February.  Then I add the day for the date in question. 

For eample, suppose the date is 03/15/2009.  I take the year and multiply by 1000 getting 2009000.  The month is greater than 1 so I add 31 days for January.  The month is greater than 2 so I add 28 days for February (yes, I do not account for leap year).  The month is not greater than 3 so it falls through the rest of the "if" statements.  I then add the current day.  Now the date is 2009074.   

The "if" statement for greater than 12 will never be true and should be deleted.



IP logged
Salmon Trout
Prodigy



Thanked: 501
Posts: 7,365

Computer: Specs
Experience: Guru
OS: Linux variant

1
« Reply #16 on: November 19, 2009, 04:01:41 PM »

Quote from: kwc1059
I am creating two dates in the format yyyyddd.  I start by taking the year and multiplying by 1000

You may find these scripts useful

http://www.commandline.co.uk/cmdfuncs/dandt/index.html

Also, VBscript has built in date math functions. You can use them from batch, if VBscripts are allowed on your system

Code: [Select]
@echo off

REM Create VBscript
echo wsh.echo Year(wscript.arguments(0))^&DatePart("y", wscript.arguments(0))>Daynumber.vbs

REM now you can use it in your script as many times as you want

set querydate1=20/11/2009

REM pass the date to the VBscript & get back the result
for /f "delims==" %%D in ('cscript //nologo Daynumber.vbs %querydate1%') do set Daynumber1=%%D

set querydate2=20/03/2009

for /f "delims==" %%D in ('cscript //nologo Daynumber.vbs %querydate2%') do set Daynumber2=%%D

echo The day number of %querydate1% is %Daynumber1%
echo The day number of %querydate2% is %Daynumber2%

output

Code: [Select]
The day number of 20/11/2009 is 2009324
The day number of 20/03/2009 is 200979

As you can see, my system uses the European dd/mm/yyyy format for dates but I believe that if your system locale settings are US you would find that 11/19/2009 would give you 2009323 but you would have to try that yourself.

« Last Edit: November 20, 2009, 12:10:47 AM by Salmon Trout » IP logged

billrich
Guest
« Reply #17 on: November 19, 2009, 04:41:39 PM »

Hello
« Last Edit: November 21, 2009, 10:36:54 PM by billrich » IP logged
Pages: 1 2 [All] - (Top) Print 
Home / Microsoft / Microsoft DOS / Decypher file attribute in a FOR loop « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.109 seconds with 20 queries.