Jump to content
Sign in to follow this  
alish

How to find a words within filenames

Recommended Posts

alish

Hello

Im noob here and will appreciate your help.

This is what I need to do :

1. Find files based on their names - Not content inside the files, just words that conclude the file names. i.e. "Excel2007.exe" -> I need to find the word "Word"

2. add random letters\words to their names

3. Move them to another location, each file should be moved according to its name.

I couldn't find how to find a word within a filename.

Thanks ahead guys!

Share this post


Link to post
Share on other sites
Melba23

alish,

Welcome to the AutoIt forum. :mellow:

You can use StringInStr to search for partial matches within strings - although the example you gave would fail :lol: :

$iRetValue = StringInStr("Excel2007.exe", "Word") ; This will fail and return 0
$iRetValue = StringInStr("Excel2007.exe", "Excel") ; This will succeed and return 1

There is no rename function in AutoIt, so you will have to do the other 2 operations at the same time using FileMove:

$sFileName = "Excel2007.exe"

; Break the name down
$sFileNameOnly = StringRegExpReplace($sFileName, "^.*\\|\..*$", "")
$sFileDotExt = StringRegExpReplace($sFileName, "^.*\.", ".$1")

; Add what you want to the file name
$sNewFileNameOnly = $sFileNameOnly & "_001" ; for example, here we add _001

; Recontrwuct the whole
$sNewFileName = $sNewFileNameOnly & $sFileDotExt ; It will read "Excel2007_001.exe"

; And finally move the file
FileMove($sFileName, $sNewFileName) ; you will have to add path info here to make it work

Right, over to you! You know where we are if you run into problems. :(

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
alish

Thank you very much for taking the time to reply, I greatly appreciate it.

I think I dropped certain details.

I have a folder that contains a lot of files.

I need these files to be distributed to their correspondent folders, based on their names.

i.e.

if I have a file that named 2010-36-72-NN.ISO I want it to be renamed and moved to a folder called 36-72.

The problem is that there are a lot of fies i.e.

2010-36-72-NN3.ISO

2010-36-72-NN7.ISO

2010-36-72-NN8.ISO

2010-22-44-NN0.ISO

2010-22-44-NN1.ISO

2010-22-44-NN7.ISO

In the example you provided, you showed how to find a word within a file you already know its name.

I hope it helps you to understand my needs, and thanks in advance

Share this post


Link to post
Share on other sites
Melba23

alish,

Yes, it would have helped if you had explained what you wanted to do at the beginning and saved my fingers from all that typing! :mellow:

To extract the 36-72 from 2010-36-72-NN?.ISO is not too difficult - we will have to use StringRegExp. How would you want to rename the file? Can you give some examples like this:

original name          New name     Move to folder
2010-22-44-NN0.ISO     001.fil      22-44
2010-36-72-NN7.ISO     002.fil      36-72

I do hope there is a pattern to the renaming! :(

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
dani

I'll just contribute some code to move files to new locations, you can edit this where needed. I trust Melba will help you along.

Excuse me for the indent size of 2, Ruby's standards taking over :mellow:

MoveFiles(@ScriptDir, "ISO")

Func MoveFiles($hDir, $ext)
  FileChangeDir($hDir)
  $hSearch = FileFindFirstFile("*.*")
  $hFile = FileFindNextFile($hSearch)
  While Not @error
    If @extended Then ; $hFile is a directory
      MoveFiles($hDir & "\" & $hFile, $ext) ; Scan the subdirectory. Remove this if you want to exclude subdirs, of course
    Else ; $hFile is a file
      FileChangeDir($hDir)
      If StringRegExp($hFile, "(?i)\." & $ext & "\z") Then
        $hFile = "SomeOtherName." & $ext ; Rename $hFile here if needed
        $target = "C:\TargetDirectory" ; Set $target (= directory) to something useful here
        FileMove($hFile, $target, 8+1) ; Move $hFile to $target (create directory if needed and overwrite existing files -- flag 8+1)
    EndIf
    $hFile = FileFindNextFile($hSearch)
  WEnd
  FileClose($hSearch)
EndFunc
Edited by dani

Share this post


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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.