Jump to content

Search Folder with Underscore in Directory


Recommended Posts

Hi Experts,

I've been checking around the forum and search engine about how to get folders with underscore as their extension (like "ABC1891123_avxnam1231") of their folder name but sadly could not find any that has this scenario. I tried using some stringInString() but no success. Is there anyone here knows something like this? Please guide me how. Still no code yet could not compose one😅, sorry...

 

 

Thanks in Advance, Expert.

KS15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to post
Share on other sites

@Subz Thank you so much, It returns the folders with underscores. One more thing, is there a way to compare them in the original folder name? like the original foldername is BQ100360 but it was duplicated with the one with underscores. Is that possible to do?

 

image.png.39fbc35ec3c551ba314167bb0350df83.png

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to post
Share on other sites

Maybe something like:

#include <Array.au3>
#include <File.au3>

$g_aFolderList = _FolderList(@ScriptDir & "\BQ100360", "BQ100360_*")
_ArrayDisplay($g_aFolderList)

Func _FolderList($_sFolderPath, $_sFilter = "*_*")
    Local $aFolderList = _FileListToArrayRec($_sFolderPath, $_sFilter, 2, 1, 0, 2)
    If @error Then Return MsgBox(4096, "Error", "No folders found")
    Return $aFolderList
EndFunc

 

Link to post
Share on other sites

@Subz Thanks, but it will just return as "No folders found" even if the folder already exist in the path.

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to post
Share on other sites

Tested before I posted and it works fine for me, check the folderpath is correct, this was just an simple example, to give you an idea on how to search for partial names i.e. "BQ100360_*" but you will need to modify for your own needs.

Link to post
Share on other sites

@Subz I tried changing something but still the same prompt. I even expanded my path just to make sure it is correct. I'm a bit confused on this code how to work, apologies Subz😞 if I did not get it right.

image.png.c44b44c5c53a6b853b30360382c965b1.png

image.png.34ed7312c63ed8dde803dc66450cb000.png

These files already exist in that path.

image.png.6e335fd3fb6e764f2f2b716cdd42cdb7.png

 

image.png

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to post
Share on other sites

@KickStarter15
You are looking in the folder "...\Get Files in folder\BGQ100360\", and not in the folder "...\Get Files in folder\", so you'll never find the folder with underscore in there;then, when you post the script, please don't post an image of it, but the code itself in the <> Code Tag.
Just pay attention at what you're doing :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to post
Share on other sites

@FrancescoDiMuro, Yes thanks for the info about the path. But I need to compare the original foldername if there is duplicate folder from the parent folder. As for the code in image, it is not for the image code but for the prompt message saying "No folders found" its just a snippet of the messagebox, apologies if it was misunderstood😅.

Anyways, is this possible to check inside the parent folder if both exist? BGQ100360 and BGQ100360_31150ed7fb5840e1852fda7b65b0b411? these folders are duplicate and I need to get them for removal (if incase) and checking.

This is the parent folder "...\Get Files in folder\" and both folders with the same foldername are inside this parent folder.

These codes below are from @Subz and both has the same output. They display only this BGQ100360_31150ed7fb5840e1852fda7b65b0b411 but not comparing if folder already exist.

_FileListToArrayRec($Server, "_*", 2, 1, 0, 0)

And the new one.

#include <Array.au3>
#include <File.au3>

$g_aFolderList = _FolderList(@ScriptDir & "\", "BGQ100360_*")
_ArrayDisplay($g_aFolderList)

Func _FolderList($_sFolderPath, $_sFilter = "*_*")
    Local $aFolderList = _FileListToArrayRec($_sFolderPath, $_sFilter, 2, 1, 0, 2)
    If @error Then Return MsgBox(4096, "Error", "No folders found")
    Return $aFolderList
EndFunc

 

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to post
Share on other sites

Just change the filter to "BGQ100360*" then loop through the array and remove any "BGQ100360_" folders example:

#include <Array.au3>
#include <File.au3>

Global $g_aFolderList = _FolderList("D:\Programs\My Programs\Mine\Get Files in folder", "BQ100360*")
_ArrayDisplay($g_aFolderList)

Func _FolderList($_sFolderPath, $_sFilter = "*_*")
    Local $aFolderList = _FileListToArrayRec($_sFolderPath, $_sFilter, 2, 1, 1, 2)
    If @error Then Return MsgBox(4096, "Error", "No folders found")
    If $aFolderList[0] <= 1 Then Return $aFolderList
    For $i = $aFolderList[0] To 2 Step - 1
;~      Uncomment line below to delete the folder
;~      DirRemove($aFolderList[$i])
        _ArrayDelete($aFolderList, $i)
    Next
    $aFolderList[0] = UBound($aFolderList) - 1
    Return $aFolderList
EndFunc

 

Link to post
Share on other sites

@Subz thanks but I think I've found another way by using below code found in help file and it shows what I expected😂.

$dir = @ScriptDir & "\BGQ100360_*"
$dir_parent = StringLeft($dir,StringInStr($dir,"_",0,-1)-1)
MsgBox(0,"",$dir_parent)

if FileExists($dir) Then
   MsgBox("","","Match")
Else
   MsgBox("","","Not Match")
EndIf

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to post
Share on other sites

@Subz form this "StringLeft($dir,StringInStr($dir,"_",0,-1)-1)" all the characters from left for this folder name "BGQ100360_*" were ignored until it found the first underscore "_" and return the final string which is the "BGQ100360". Now locating this folder using fileexist from the desired path it is now found and that way I can identify the duplicate folder and generate them in my log.txt.

It is something like this: I don't know if I coded it right😅 but it works.

$File = FileOpen(@ScriptDir & "\FolderList.txt", 0) ; This is where all folder name were listed
$xFile = FileOpen(@ScriptDir & "\Log.txt", 2) ; this is my log report
FileWriteLine($xFile, @UserName & " processed " & @MON & "/" & @MDAY & "/" & @YEAR & ": " & @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC)

For $i = 1 to _FileCountLines($File)
$line = FileReadLine($File, $i)

$MainFolder  = $Server & $line ; $server is the full path where to find these folderlist

Local $aFileList = _FileListToArray($MainFolder, "*", 1)
 If @error = 4 Then
   MsgBox($MB_SYSTEMMODAL, "Error", "No file(s) were found.")
   Exit
 EndIf
 FileWrite($xFile, @CRLF & $line & @CRLF)
   _GUICtrlRichEdit_InsertText($Edit1, @CRLF & $line)
 If Not @Error Then
  _FileWriteFromArray($xFile, $aFileList, 1, Default, @CRLF)
  $dir = $Server & $line & "_*"
  $dir_parent = StringLeft($dir,StringInStr($dir,"_",0,-1)-1)
  If $aFileList = 0 Then
    _GUICtrlRichEdit_InsertText($Edit1, "............. Folder Not Found in: " & $Server & $line)
    FileWrite($xFile, "Folder Not Found in: " & $Server & $line & @CRLF)
  EndIf
  If FileExists($dir) Then
    _GUICtrlRichEdit_InsertText($Edit1, @CRLF & $line & " ............. Process this locally: " & $line)
    FileWrite($xFile, ">>>>> Duplicate folders found in BG server" & $line & "_*" & @CRLF) ; this is the log report
  EndIf
 EndIf
Next

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...