Page 1 of 1

Move all files - except ...

Posted: 31 Aug 2017 10:08
by Bingwu
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

Re: Move all files - except ...

Posted: 31 Aug 2017 13:45
by anuraag
Created a quick flow. You need to edit source and target folders.

Re: Move all files - except ...

Posted: 31 Aug 2017 14:06
by Bingwu
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

Re: Move all files - except ...

Posted: 31 Aug 2017 19:22
by Desmanto
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.

Re: Move all files - except ...

Posted: 01 Sep 2017 00:44
by anuraag
@Desmanto
You are correct. I haven't tested flow. Thanks for checking.

Re: Move all files - except ...

Posted: 01 Sep 2017 05:48
by Bingwu
Hello!

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

greetings
Peter

Re: Move all files - except ...

Posted: 01 Sep 2017 09:28
by Desmanto
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.