0

I am creating a guy script read files in a folder, (Scripting.FileSystemObject), but I would like to relate a indice inpubox type int to determine which file in the folder I'll write on the screen.

Ex: indice = inputbox "" ← 4 grab the indice file in the folder 4 and esquever your name on the screen.   I wonder if this is possible because already tried in many ways and even by matrix, but without result.

This and my code. I do not know but where to go!

Dim sFO, NovaPasta, Folder,File, Indice          
Dim inpast(4)

'Setup

Set sFO =  CreateObject("Scripting.FileSystemObject")
Set Folder = sFo.GetFolder("C:\Users\502526523\Documents\Control")
NovaPasta = "Control"

'Development

If Not sFO.FolderExists (NovaPasta) = True Then
  sFO.CreateFolder (NovaPasta)
  Wscript.Sleep 900 
  WScript.Echo "Pasta Criada"
Else
  WScript.Echo "Pasta Existente "
End If

' Line Verificas a quantidade de inpastas dentro da pasta, se > 5
' deleta os exedentes com data mais antiga

For Each file In folder.Files
  If Folder.Files.Count > 5 And (DateDiff("d", file.DateLastModified, Now) > 7) Then
    WScript.Echo  (file.Name & vbLf)
    WScript.Echo ("Total files :" & Folder.Files.Count)
    File.Delete
  End If
Next

For Each file In folder.Files
  inpast(0) = (file.Name)
  inpast(1) = (file.Name)
  inpast(2) = (file.Name)
  inpast(3) = (file.Name)
  inpast(4) = (file.Name)

  Indice = Inputbox ("Digite o valor do Indice de 0...30")

  Select Case Indice
    Case 0
      WScript.Echo inpast(0)
    Case 1
      WScript.Echo inpast(1)
    Case 2
      WScript.Echo inpast(2)
    Case 3
      WScript.Echo inpast(3)
    Case 4
      WScript.Echo inpast(4)
  End Select
Next
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Your question is incomprehensible. – Ansgar Wiechers May 21 '16 at 00:43
  • Google translator ------, I need to get the name in the folder however intuitively, if I type a number in imputbox this has to search the file corresponding to the number of imputbox, ie co-relation. inputbox = 7 -------- msgbox = file name (7) inside the folder I tried to use an array with a case but does not work. – Inslley Roberth May 21 '16 at 01:07

2 Answers2

0
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")
'Searchterm = Inputbox("Enter search term")
ProcessFolder DirName

Sub ProcessFolder(FolderPath)
'    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)
    msgbox fls.count
    Msgbox fls.item("computerlist.txt") 
End Sub

To do the 7th

    Set Fls = fldr.files

    For Each thing in Fls
        Count = Count + 1
        If count = 7 then msgbox Thing.Name & " " & Thing.DateLastModified 
    Next
  • Good Morning Dear! Sorry for the delay in responding .... I appreciate the help, now the first thing to fix and stop responding to you guys, because if not I would not again !!! Noodles - Your solution I can not completely test she is giving problem on line 9 required object 'folios' cod: 800ª01A8 will also test more and send one soon answer. – Inslley Roberth May 24 '16 at 13:13
  • Enter a full directory name like `c:\windows` or change `Sub ProcessFolder(FolderPath)` to `Sub ProcessFolder("c:\windows")` –  May 24 '16 at 13:28
  • Typing the name of the directory C: \ Users \ Simatic \ Desktop \ Control fails line 9 Object required: 'fls' Cod: 800A01A8   Replacing Sub ProcessFolder ( "C: \ Users \ Simatic \ Desktop \ Control") Displays Error Line 6 expected identifier Cod: 800A03F2 – Inslley Roberth May 24 '16 at 15:53
  • Why all the spaces in the file path. –  May 26 '16 at 01:12
0

Still not sure if I understand your question correctly. You mean you have a list of filenames and you want to display the filename corresponding to the number the user entered via an InputBox? If that's what you want you should change your second For Each loop like this:

i = 0
For Each file In folder.Files
  inpast(i) = file.Name
  i = i + 1
Next

Indice = InputBox("Digite o valor do Indice de 0...30")
WScript.Echo inpast(CInt(Indice))

Note, however, that the condition in your first For Each loop does not guarantee you'll only ever have 5 files left after the loop. If for some reason the folder contains more than 5 files that were modified within the past 7 days the second loop would fail with a "subscript out of range" error.

There are several ways you could handle this:

  • Dynamically resize the inpast array so it can hold more than 5 items.
  • Sort the files in the folder by last modification date (e.g. like this) and delete everything except the 5 most recent files.
  • Cut off the second For Each loop after the 5th iteration (Exit For).

Note also, that you should sanitize your input. (What happens when users enter text, an invalid number, or press "Cancel"?)

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Good Morning Dear! Sorry for the delay in responding .... I appreciate the help, now the first thing to fix and stop responding to you guys, because if not I would not again !!! Ansgar Wiechers - His solution was just what I needed, I had emcontrado pareceda one solution, but I could not format the question of Ciint conversion because I was trying to convert the file.count and did not work, but with his cod. It worked perfectly on your perguna part delete, it's working, I tested a folder with 54 old and new files and it worked well. I am grateful for the great help ... – Inslley Roberth May 24 '16 at 13:14