0

Sorry if it's asked before, I found out other Solutions too complicated for me.. Anyway, i am trying to search an image via cmd in visual basic code, and save the image path to string, but i cant seem to capture the output from cmd right. Any help will be appreciated, thanks!.

Code:

    Dim imageLocation As String
    Dim cmd As New Process
    Dim SR As System.IO.StreamReader
    cmd.StartInfo.FileName = "cmd.exe"
    cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    cmd.StartInfo.Arguments = "/C dir /b/s Roey.png"
    cmd.Start()
    SR = cmd.StandardOutput
    imageLocation = SR.ReadLine

UPDATED So i found out saving the output to txt file and then read it can be more simple, so i wrote the following code:

        Dim cmd As New Process
        cmd.StartInfo.FileName = "cmd.exe"
        cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        cmd.StartInfo.Arguments = "/C dir /b/s Roey.png > 
        C:\Users\ירין\Desktop\Roeyyy\path.txt"
        cmd.Start()
        cmd.WaitForExit()

when i run the

    "dir /b/s Roey.png > 
    C:\Users\ירין\Desktop\Roeyyy\path.txt"

on CMD it wors perfectly, so why isnt it working here? :(

yarinc
  • 41
  • 2
  • 9
  • There are a lot of problems here. `"/C dir /b/s Roey.png"` => this should be `"/C Roey.png dir /B /S"`. `.StartInfo.WorkingDirectory = [SomePath]` => Specify the Directory from where to start the enumeration. `.StartInfo.RedirectStandardOutput = True` => You need to redirect `StdOut` => to do this `.StartInfo.UseShellExecute = False`, because the default is `True` and it won't work. `Dim SR As StreamReader = cmd.StandardOutput` `While (imageLocation = String.Empty) imageLocation = SR.ReadLine End While`. Error prone. Use `Directory.GetFiles()` or `Directory.EnumeratetFiles()`. – Jimi Oct 07 '18 at 20:58
  • idk why but when i run the following command on cmd, it works fine. '"dir /b/s Roey.png > C:\Users\ירין\Desktop\Roeyyy\path.txt"', but when i am writing it, it wont work, updating the post now.. – yarinc Oct 07 '18 at 21:04
  • `CMD.EXE` is a **Program**. It interprets the commands and executes the procedure. What you should do. A single line to replace this all: `Dim MyFilePath As String = Directory.GetFiles([SomePath], "*.png", SearchOption.AllDirectories).Where(Function(f) f.Contains("Roey.png")).FirstOrDefault()` where `[SomePath]` is the base directory for the enumeration. ==> **There's an error in the first comment:** It should be : "/C dir Roey.png /B /S" – Jimi Oct 07 '18 at 21:06

2 Answers2

1

I found this:

Dim MyFilePath As String = Directory.GetFiles([SomePath], "*.png", SearchOption.AllDirectories).
     Where(Function(f) f.Contains("Roey.png")).FirstOrDefault()

Solved!

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
yarinc
  • 41
  • 2
  • 9
0

You are a programmer so you search for files.

Imports System.Runtime.InteropServices
Sub Main
'On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")

ProcessFolder DirName

End Sub

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files

    For Each thing in Fls
         msgbox Thing.Name & " " & Thing.path 
         'fso.copyfile thing.path, "C:\backup"
    Next

    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        ProcessFolder thing.path
    Next

End Sub
CatCat
  • 483
  • 4
  • 5