Page 1 of 1 1
Topic Options
#209724 - 2014-12-04 02:32 PM DIR() Function - What am I doing wrong?
JNK Offline
Fresh Scripter

Registered: 2006-04-11
Posts: 33
Loc: USA
Apologies in advance if this question is already answered in the Forum ... I couldn't find it.

I am writing a script to perform file maintenance / cleanup. Specifically, I need to enumerate the user profile folders (c:\users\*) and then look for some specific folders under each user profile folder ... and then delete them. I am working on getting the folder enumeration code working properly before adding the delete routine. I have a placeholder for that right now.

According to the documentation, the DIR() function can enumerate a second folder by adding index 1 [i.e., $filename = dir($path,1)] but this does not seem to work. The script only "sees" the first profile folder (administrator), logs the first instance and then logs the other profile folders.

c:\users
\Administrator
\DeskAlerts_{6BD65F13-DD7D-436b-A232-CD8AF199B58B}
\All Users
\Default
\Default User
\desktop.ini
\jdoe
\MSSQL$ADK
\Public

If I remove the second folder code, it enumerates all of the User Profile folders just fine.

c:\users
\Administrator
\All Users
\Default
\Default User
\desktop.ini
\jdoe
\MSSQL$ADK
\Public

If I remove the index 1 option, it enumerates the first user profile correctly and then quits. It never looks at the other profile.

c:\users
\Administrator
\DeskAlerts_{6BD65F13-DD7D-436b-A232-CD8AF199B58B}
\DeskAlerts_{81EEE9F2-B5CE-458d-AA44-B6C723FB8379}
\DeskAlerts_{943F9A2C-5DB8-4030-BF2D-8F46AB738D9C}

Here is my code:

 Code:
; ===== VARS =====
$rootdir = "c:\users"
$rootdir ?  ; log the root dir

; ===== LOOP: PROFILE FOLDER =====
$filename1 = dir($rootdir)
while $filename1 <> "" and @error = 0
   if $filename1 <> "." and $filename1 <> ".."
      "   \" + $filename1 ?   ; log the profile folder

      ; ===== LOOP: APPDATA =====
      $filename2 = dir($rootdir + "\" + $filename1 + "\appdata\roaming\deskalerts_*",1)
      while $filename2 <> "" and @error = 0
         "      \" + $filename2 ?   ; log the deskalerts_* folder

         ; insert delete_folder code here

         $filename2 = dir()
      loop

   endif
   $filename1 = dir()
loop


... and here is what the DOS dir command shows:

c:\>dir c:\users\deskalerts_* /a/s/b

C:\Users\Administrator\AppData\Roaming\DeskAlerts_{6BD65F13-DD7D-436b-A232-CD8AF199B58B}
C:\Users\Administrator\AppData\Roaming\DeskAlerts_{81EEE9F2-B5CE-458d-AA44-B6C723FB8379}
C:\Users\Administrator\AppData\Roaming\DeskAlerts_{943F9A2C-5DB8-4030-BF2D-8F46AB738D9C}
C:\Users\Default\AppData\Roaming\DeskAlerts_{6BD65F13-DD7D-436b-A232-CD8AF199B58B}
C:\Users\Default\AppData\Roaming\DeskAlerts_{81EEE9F2-B5CE-458d-AA44-B6C723FB8379}
C:\Users\Default\AppData\Roaming\DeskAlerts_{943F9A2C-5DB8-4030-BF2D-8F46AB738D9C}
C:\Users\jdoe\AppData\Roaming\DeskAlerts_{1CE89CDA-77BA-4cbd-BD52-C32F884C3ECA}
C:\Users\jdoe\AppData\Roaming\DeskAlerts_{6BD65F13-DD7D-436b-A232-CD8AF199B58B}
C:\Users\jdoe\AppData\Roaming\DeskAlerts_{81EEE9F2-B5CE-458d-AA44-B6C723FB8379}
C:\Users\jdoe\AppData\Roaming\DeskAlerts_{881F9EA6-B6B9-45c4-8F0D-5EEFAD04CFBE}
C:\Users\jdoe\AppData\Roaming\DeskAlerts_{943F9A2C-5DB8-4030-BF2D-8F46AB738D9C}
C:\Users\MSSQL$ADK\AppData\Roaming\DeskAlerts_{6BD65F13-DD7D-436b-A232-CD8AF199B58B}
C:\Users\MSSQL$ADK\AppData\Roaming\DeskAlerts_{81EEE9F2-B5CE-458d-AA44-B6C723FB8379}
C:\Users\MSSQL$ADK\AppData\Roaming\DeskAlerts_{943F9A2C-5DB8-4030-BF2D-8F46AB738D9C}

Kix ver: 4.62 and 4.64
O/S: Windows 7, SP1, 32-bit

TIA for any advice on this.

Top
#209725 - 2014-12-04 03:44 PM Re: DIR() Function - What am I doing wrong? [Re: JNK]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
I use DIRPLUS from the UDF library. It turns the files and folders into arrays which are far easier to work with.

Dirplus() -
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=82153#Post82153

How to use UDFs -
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=81943#Post81943

The rest of the UDFs are here -
http://www.kixtart.org/forums/ubbthreads.php?ubb=postlist&Board=7&page=1

for example
 Code:
for each $folder in dirplus("%userprofile%","/ad")
  ? $folder
  for each $file in dirplus($folder,"/-ad")
    ;? " " + $file
  next
next
_________________________
(... better days ahead)

Top
#209726 - 2014-12-04 03:46 PM Re: DIR() Function - What am I doing wrong? [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
BTW... Dir is a command, not a function, so it would be used like the following:

dir "directoryname" ;no parenthesis



Allen has CRS.


Edited by Allen (2014-12-04 09:32 PM)
Edit Reason: Can't Remember S#!t
_________________________
(... better days ahead)

Top
#209727 - 2014-12-04 07:25 PM Re: DIR() Function - What am I doing wrong? [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
wth????
Allen, you forgot your coffee this morning?
_________________________
!

download KiXnet

Top
#209728 - 2014-12-04 08:00 PM Re: DIR() Function - What am I doing wrong? [Re: Lonkero]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
Pay no attention to the "dir is a command" silliness above.. we'll all chip in a few bitcoins and buy Allen a coffee! \:D

Try changing
 Code:
$filename2 = dir()
to ()
 Code:
$filename2 = dir("",1)
in your code - you need to match the enumeration indices.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#209729 - 2014-12-04 09:29 PM Re: DIR() Function - What am I doing wrong? [Re: Glenn Barnas]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
K... well that just goes to show how long its been since I used the builtin dir... lol

Post edited above.
_________________________
(... better days ahead)

Top
#209733 - 2014-12-09 10:15 PM Re: DIR() Function - What am I doing wrong? [Re: Allen]
JNK Offline
Fresh Scripter

Registered: 2006-04-11
Posts: 33
Loc: USA
Sorry for the delay in replying; busy week.

adding dir(,1)
...
dir("",1)

worked!!

I will probably settle on the UDF option above (or Powershell), but another option I figured out was pipe the output from the DOS dir command to a text file and then use open() to read the contents of the file. Add in some instr() logic and a call to deltree and ! It is a long way around but is functional.

... appreciate the tip for the syntax for using the 1 index. Proper use of that isn't documented very well. Now I know!

Thanks!

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.066 seconds in which 0.034 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org