Move excel file (with file names contain...) to another folder

FileName
Hi, I would like to move excel files from one folder to another.
But i only want to move some of the files instead of all the files.
For eg, i want to move A_310123 and D310123 only, but how do i write the condition with the file name contains A_* and D_*, as the date changes every month…

Much thanks!

Hi @Joanna2

=> Assign → ListFiles (List Datatype) = Directory.GetFiles(“Folder Path”,“*.xlsx”)
=> Iterate the ListFiles variable to iterate with for each activity.
=> Inside for each place the Assign activity and store the file name in variable
=> Assign → EachFile = System.IO.Path.GetFileNameWithoutExtension(currentItem)
=> condition in If condition → EachFile.StartsWith(“A”) or EachFile.StartsWith(“C”)
=> In then block place the move file to move files from one folder to another foler.

Check the below image for better understanding

Hope it helps!!

  1. Assign sourceFolderPath = “C:\Path\To\Source\Folder”

  2. Assign destinationFolderPath = “C:\Path\To\Destination\Folder”

  3. Assign filePattern1 = “A_*”

  4. Assign filePattern2 = “D_*”

  5. Get the list of Excel files from the source folder:

    • Assign excelFiles = Directory.GetFiles(sourceFolderPath, “*.xlsx”)
  6. For Each file in excelFiles:

    • If file name starts with “A_” or “D_”:
      • Use an If activity with the condition: Path.GetFileName(file).StartsWith(filePattern1) Or Path.GetFileName(file).StartsWith(filePattern2)

      • If the condition is True:

        • Assign destinationFilePath = Path.Combine(destinationFolderPath, Path.GetFileName(file))
        • Use the File.Move activity to move the file:
          • Source: file
          • Destination: destinationFilePath

@Joanna2

  1. Assign - sourceFolderPath = “C:\SourceFolder” // Replace with your actual source folder path

  2. Assign - destinationFolderPath = “C:\DestinationFolder” // Replace with your actual destination folder path

  3. Assign - filePatterns = {“A_", "D_”} // An array containing the file patterns to match

  4. Assign - filesToMove = Directory.GetFiles(sourceFolderPath, “*.xlsx”) // Get all Excel files in the source folder

  5. For Each - TypeArgument: String, Values: filesToMove
    // Loop through each file in the source folder

    1. For Each - TypeArgument: String, Values: filePattern in filePatterns
      // Loop through each file pattern (“A_" and "D_”)

      1. If - Condition: Path.GetFileNameWithoutExtension(item).StartsWith(filePattern)
        // Check if the file name without extension starts with “A_” or “D_”

        1. Assign - destinationFilePath = Path.Combine(destinationFolderPath, Path.GetFileName(item))
          // Combine destination folder path with the file name to get the destination file path

        2. File.Move(item, destinationFilePath)
          // Move the file to the destination folder
          End If
          End For
          End For

@Joanna2

Sequence
|
|-- Assign (sourceFolderPath = "Your_Source_Folder_Path")
|
|-- Assign (destinationFolderPath = "Your_Destination_Folder_Path")
|
|-- Assign (filesToMove = Directory.GetFiles(sourceFolderPath, "A_*.*").Concat(Directory.GetFiles(sourceFolderPath, "D_*.*")).ToArray())
|
|-- For Each (TypeArgument: String, Values: filesToMove)
|   |-- Assign (fileName = Path.GetFileName(file))
|   |-- If (Condition: fileName.StartsWith("A_") Or fileName.StartsWith("D_"))
|   |   |-- File.Move (From: file, To: Path.Combine(destinationFolderPath, fileName))

Hi @Joanna2

Try this

Hope it helps!!

Hi @Joanna2
Try this

FileName.StartsWith("A_") Or FileName.StartsWith("D_")

System.IO.Path.GetFileNameWithoutExtension(CurrentFile.ToString)

I hope it helps!!

@Joanna2

Try this

Directory.GetFiles("Folderpath","*.xlsx").Where(function(x) {"A_","D_"}.Any(function(y) Path.GetFileName(x).StartsWith(y)))

This would give you list of files starting with A_ and D_…can add more in list if you want {"A_","D_"}

Hope this helps

Update: Added Path.Getfilename thanks @ppr

cheers

@Joanna2

Use for each file in folder and indicate the files folder path
Use and if condition and in if use the syntax as CurrentFile.Name.StartsWith("A") or CurrentFile.Name.StartsWith("D")
And within this use the move file activity.

Use the below sequence

Hope it works!!

We can model it compact with the help of LINQ

Assign Activity:
arrOfFilePaths | String() - String array =
(From fi In New DirectoryInfo(“Path”).GetFiles(“*.xlsx”)
Where New String() {“A”,“D”}.Any(Function (x) fi.Name.StartsWith(x))
Select fp = fi.FullName).toArray

Then loop over arrOfFilePaths e.g. with a for each actvitiy

we should keep in mind:
grafik
So StartsWith looks at the fullpath

@ppr

Just now corrected it thanks for the update…added getfilename

cheers

Hi,

Can anyone advise why this didn’t work? Thank you!

@Joanna2
In the filter expression give"*.csv"
Check the filepath by sending a message box whether displaying correctly or not of the move file activity

Hope it helps!!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.