Move all files - except ...

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
Bingwu
Posts: 114
Joined: 26 Feb 2016 10:26

Move all files - except ...

Post by Bingwu » 31 Aug 2017 10:08

Hello!

I need help moving files (Action: "Move Files").
I want to move all the files of a directory (except one specific file) to another directory.

In principle, therefore:
Source Files -> /storage/emulated/0/source/*.* (Different file names, file name lengths, file extensions)
Target -> /storage/emulated/0/target

Exception: The file "backup.prefs" must not be moved.

My knowledge of regular expressions is, unfortunately, too limited. It would be great if someone could show me a solution.

Thank you!
Peter
------------------------------------------------------------------------------------------------------------------------
Hallo!

Ich brauche Hilfe beim Verschieben von Dateien (Aktion: "Dateien verschieben").
Ich möchte alle Dateien eines Verzeichnisses (ausser einer bestimmten Datei) in ein anderes Verzeichnis verschieben.

Grundsätzlich also:
Quelldateien -> /storage/emulated/0/quelle/*.* (unterschiedlichste Dateinamen, Dateinamenlängen, Dateierweiterungen)
Ziel -> /storage/emulated/0/ziel

Ausnahme: Die Datei "backup.prefs" darf nicht verschoben werden.

Mein Wissen über reguläre Ausdrücke ist leider zu begrenzt. Es wäre grossartig, wenn mir jemand einen Lösungsweg zeigen könnte

Danke!
Peter

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Move all files - except ...

Post by anuraag » 31 Aug 2017 13:45

Created a quick flow. You need to edit source and target folders.
Attachments
flow_Move_files_with_exception_20170831_191632.xml
(2.16 KiB) Downloaded 957 times

User avatar
Bingwu
Posts: 114
Joined: 26 Feb 2016 10:26

Re: Move all files - except ...

Post by Bingwu » 31 Aug 2017 14:06

Hello anuraag!

I thank you for your efforts and the quick help!
Excellent! Create a list of existing files and then remove the single file. :-)

And ... :shock:
I'm so stupid! The same thing I do when transferring flows.

Best regards
Peter

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Move all files - except ...

Post by Desmanto » 31 Aug 2017 19:22

Do the flow/script works? I tried, and it will give error because of the "[" (left bracket)
Putting list variable at the source seems to have trouble with the bracket.

I know it can be done with single line script as

Code: Select all

removeElementValue(files, "/storage/emulated/0/source/backup.prefs")
But it doesn't work. Strange. If I copy the list manually and create the variable list from it in another place, that single line can remove the backup.prefs.
Seems that {files} from init variable file list is not a real list. copyList() to another list also doesn't work, can't remove using removeElementValue().

So next level is to remove it using index,

Code: Select all

removeElement(files, indexOfElement(files, "/storage/emulated/0/source/backup.prefs"));
It works fine. indexOfElement() will get the index of the backup.prefs. removeElement() will remove that index.

However, this still give problem with the bracket. So we need to remove that bracket, by turning that list into string, separated by comma. Since the option specify multiple files can be separated by comma. Adding the join()

Code: Select all

removeElement(files, indexOfElement(files, "/storage/emulated/0/source/backup.prefs"));
files = join(files,",")
This will remove the backup.prefs and join the list {files} to a single string that can be passed to the source.

However, another problem arise (again). The same problem with SSID handling, where comma in the SSID name will destroy the logic of the flow. Same thing happens here, as it is using the same comma splitting for multiple items.
If your file name has comma inside, the flow will be error. So we have to double quote each of the file name. Using "{files}" won't work, as it is different with SSID solution, the {files} is a list.
And remember, it should be double quote, as file name can have single quote also (but not double quote).

Considering all above, there is no easy way by just 2 line of script. We have to double quote each element, so it is easier to loop the list. I just take the same script structure from anuraag, modify it a bit.

Code: Select all

files_move = newList();
for(i in [0 to length(files)-1])
{
  if(!contains(files[i], "backup.prefs"))
    addElement(files_move, "\"{files[i]}\"")
}
files_move = join(files_move,",")
This should work, even you have comma in your file name.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Move all files - except ...

Post by anuraag » 01 Sep 2017 00:44

@Desmanto
You are correct. I haven't tested flow. Thanks for checking.

User avatar
Bingwu
Posts: 114
Joined: 26 Feb 2016 10:26

Re: Move all files - except ...

Post by Bingwu » 01 Sep 2017 05:48

Hello!

I have changed (Action "Move Files") {files_move} to {files_move,listformat,comma}.
Is working!

greetings
Peter

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Move all files - except ...

Post by Desmanto » 01 Sep 2017 09:28

Another enlightment :!: Thanks for the info, never knew we can use that listformat. I probably should reread the documentation.
When using list format, any name with special character or single quote, will be automagically double quoted. So no problem with the special character anymore.

So we only need the single line script

Code: Select all

removeElement(files, indexOfElement(files, "/storage/emulated/0/source/backup.prefs"));
Then use {files,listformat,comma} at the Action Move Files.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply