cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Kyle7989
Regular Visitor

Move File To Right Folder

I'm a new to PAD and trying to setup a process to move files.

I have files named as:  123 acb, 456 xyz, 678 kus  and folders named 123, 456, 789
How can I automatically move those files to right folder with the name?

Thanks a lot

@Michael 

1 ACCEPTED SOLUTION

Accepted Solutions
Anonymous
Not applicable

Hello @Kyle7989 

 

Below is a working example flow of your scenario.

 

shindomo_0-1632477713140.png

 

Yon can copy & paste below code snippet to your Flow Designer window.

 

 

 

SET BaseFolderPath TO $'''C:\\Users\\User01\\Documents\\Move File To Right Folder'''
Folder.GetFiles Folder: BaseFolderPath FileFilter: $'''*.txt''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files
LOOP FOREACH CurrentItem IN Files
    Text.Split Text: CurrentItem.Name StandardDelimiter: Text.StandardDelimiter.Space DelimiterTimes: 1 Result=> TextList
    SET TargetFolder TO $'''%BaseFolderPath%\\%TextList[0]%'''
    File.Move Files: CurrentItem.FullName Destination: TargetFolder IfFileExists: File.IfExists.DoNothing MovedFiles=> MovedFiles
    Display.ShowMessage Title: $'''File Moved''' Message: $'''[Source]
%CurrentItem.FullName%

[Destination]
%TargetFolder%''' Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: False ButtonPressed=> ButtonPressed
END

 

 

 

The result of running the above flow is as follows:

 

[BEFORE]

C:\USERS\USER01\DOCUMENTS\MOVE FILE TO RIGHT FOLDER
│ 123 acb.txt
│ 456 xyz.txt
│ 789 kus.txt

├─123
├─456
└─789

 

[AFTER]

C:\USERS\USER01\DOCUMENTS\MOVE FILE TO RIGHT FOLDER
├─123
│ 123 acb.txt

├─456
│ 456 xyz.txt

└─789
789 kus.txt

 

Thank you.

View solution in original post

5 REPLIES 5
MichaelAnnis
Super User
Super User

  • Set Variable:  %MainFolderPath%
  • Get Files to %Files%
  • For each %CurrentItem% in %Files%
    • Split Text %CurrentItem.Name% by Standard Space (1 time) to %FolderName%
    • Move %CurrentItem% to %MainFolderPath%\%FolderName%
  • End For Each

 

Best of Luck!

Sorry...one technicality missing here.  Split text is going to go to a list of text.  Each Text on a separate row.  You can decide what to call the variable, let's call it %ListText%.  So, after Split Text, you need:

 

Set Variable %FolderName% to %ListText[0]%

 

This will return the first text in that list which will be those 3 folder numbers that occurred before the "space" delimiter.

Kyle7989
Regular Visitor

Thanks Michael. 

I am trying to understand your instruction, but I can not.

Maybe I skipped some steps. I need to start all over again.

Do you have any document or video to learn PAD from zero?

Thank you so much for your time and wish you all the best.

Anonymous
Not applicable

Hello @Kyle7989 

 

Below is a working example flow of your scenario.

 

shindomo_0-1632477713140.png

 

Yon can copy & paste below code snippet to your Flow Designer window.

 

 

 

SET BaseFolderPath TO $'''C:\\Users\\User01\\Documents\\Move File To Right Folder'''
Folder.GetFiles Folder: BaseFolderPath FileFilter: $'''*.txt''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files
LOOP FOREACH CurrentItem IN Files
    Text.Split Text: CurrentItem.Name StandardDelimiter: Text.StandardDelimiter.Space DelimiterTimes: 1 Result=> TextList
    SET TargetFolder TO $'''%BaseFolderPath%\\%TextList[0]%'''
    File.Move Files: CurrentItem.FullName Destination: TargetFolder IfFileExists: File.IfExists.DoNothing MovedFiles=> MovedFiles
    Display.ShowMessage Title: $'''File Moved''' Message: $'''[Source]
%CurrentItem.FullName%

[Destination]
%TargetFolder%''' Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: False ButtonPressed=> ButtonPressed
END

 

 

 

The result of running the above flow is as follows:

 

[BEFORE]

C:\USERS\USER01\DOCUMENTS\MOVE FILE TO RIGHT FOLDER
│ 123 acb.txt
│ 456 xyz.txt
│ 789 kus.txt

├─123
├─456
└─789

 

[AFTER]

C:\USERS\USER01\DOCUMENTS\MOVE FILE TO RIGHT FOLDER
├─123
│ 123 acb.txt

├─456
│ 456 xyz.txt

└─789
789 kus.txt

 

Thank you.

Kyle7989
Regular Visitor

@Anonymous  It WORKS. I can't understand your code but it WORKS

So nice of you. Thank you so much.

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (2,931)