How to move downloaded file to another directory

I download a csv file and have it renamed via onDownload command. Next, I want to move the file I just downloaded to a specific directory.

The file has a variable name:
{dateToday} some fixed text.csv where {dateToday} = yyyy/mm/dd
e.g. file name
2020/10/17 some fixed text.csv

It saves in the ‘downloads’ folder, and I want to move it to another directory. I have read in the forums you can do this with powershell or python or a batch file though I really don’t know how to use any of those. What would the code look like? Would I write a .ps1 file and save it, then call XRun to that ps1 file? And what do the contents of the ps1 file need to be? The file name to move is variable - is there a way to say move ‘most recent’ file? Or is a better idea to store the file name in a variable like this:

store | {dateToday} some fixed text.csv | filename onDownload | {filename} | true

Then if I do XRun on a powershell file, can I reference the ui.vision ${filename} when telling it which file to move? Is that possible? I’m just trying to figure out 1. how to move a file and 2. what the best way is.

I came up with this simple powershell script to move the most recently created file. It works but if anyone has any better ideas I’d be happy to learn from them.

$source = “C:…”
$destination = “D:…”

@(Get-ChildItem source | Sort CreationTime -Descending)[0] | % { Move-Item -path _.FullName -destination $("$destination") -force}

You did it correct, I have a few macros that do the same thing. If it’s a simple move I’ll just do move /Y in a batch file or if I need to do something more complicated or with logic I’ll do a script like you just did.

1 Like

You can use batch file with MOVE command

1 Like

The below code will identify the most recent file created or downloaded in your source path and will move the file to the destination path. Only the sourcePath and destinationPath you have to change in the below code.

@echo off
set “sourcePath= Ex: C:\Users\biswa\Downloads”
set “destinationPath= Ex: D:\RPA\Udemy”

rem Get the most recently created or downloaded file
for /f “delims=” %%a in (‘dir /b /a-d /o-d /tw “%sourcePath%”’) do (
set “mostRecentFile=%%a”
goto :MoveFile
)

:MoveFile
if defined mostRecentFile (
rem Create the destination folder if it doesn’t exist
if not exist “%destinationPath%” mkdir “%destinationPath%”

rem Move the most recently created or downloaded file to the destination
move "%sourcePath%\%mostRecentFile%" "%destinationPath%"

echo File "%mostRecentFile%" moved successfully to "%destinationPath%"

) else (
echo No files found in “%sourcePath%”
)

Copy the above code and paste it into a notepad. Save the file as “.bat” with the File type “All Files”

Then user XRun to run the .bat file. Below is a sample example.
Command: XRun
Target: Ex: D:\RPA\Udemy\MoveFileBatFile.bat