How I can capture the last file in a directory in windows

If I have a series of file like this in a windows directory

3306 Dec 16 13:16 3526_334101.dat.processed
-rw-r----- 1 banjobs banner 6612 Dec 16 13:18 3526_334898.dat 
-rw-r----- 1 banjobs banner 23142 Dec 16 13:20 3526_334690.dat 
-rw-r----- 1 banjobs banner 19836 Dec 16 13:23 3526_337092.dat 
-rw-r----- 1 rechever banner 19836 Apr 6 18:28 3526_337933.dat 

I want to write a piece of code just to capture the last one in that directory, so I can put in a variable to do my FTP
something like

 
CDRemoteDir=$dir
## Grab the file 
UpLoadFileName=ls 3526* ????
echo "UpLoadFileName" $UpLoadFileName

I know that the file ALWAYS start with 3526 but I don't know the other numbers again I want to capture the last file put in the directory

lastfile=`ls -1 myfile* | tail -1`
echo $lastfile

Note that this will take the last file alphabetically that begins with myfile. You could substitute any other filter and/or use other logic to sort the list.

Do you mean the file with the latest filetime (most recent file)?
Try one of these:

lastfile=$(ls -rt | tail -1)
# or
lastfile=$(ls -t | head -1)

Assuming I got what you wanted...

Well, I am getting the following:
Here is the directory in unix
N:\orgs\Financial Aid\CSSIDOCLoad (explore)
here is my piece of code:

CDRemoteDir= 'cd N:\orgs\"Financial Aid"\"CSSIDOCLoad" ';
UpLoadFileName== '$(ls -rt | tail -1)';
echo $UpLoadFileName;

It is not finding the directory

spinel:/u02/sct/banner/bandev2/xxxxx/shl$ sh idoc_ftp_finaid.shl 'DEV2' 
idoc_ftp_finaid.shl[39]: cd N:\orgs\"Financial Aid"\"CSSIDOCLoad" :  not found
idoc_ftp_finaid.shl[40]: $(ls -rt | tail -1):  not found

I changed the real name for xxxx
Again I have files in that directory that are like 3526_419184.dat 3526_ is is a constant
but the rest like 419184 are different I want to capture the last file that was move to that directory in UpLoadFileName, so I can FTP later

here is the rest of the code

 
# Initiate the FTP process

# Loop through remaining parameters to create ftp commands.
(

# Enter user-name and password in host machine
echo "user $RemoteUser $RemotePass"

# Set transfer mode
echo $TMode

# Change directory in remote machine
echo ${CDRemoteDir}

# Change local directory in local machine
### echo lcd $LocalDir

# Transfer file
echo get $UpLoadFileName

# End ftp session
echo quit

) | ftp -vin $RemoteHost >>${LF1} 2>>${LF2}

# End of FTP Process

if test $? -eq 0
then
  echo "Successfully executed FTP"  >>${LF1} 2>>${LF2}
else
  echo "Error: could not execute FTP" >>${LF1} 2>>${LF2}
  echo " " >>${LF1} 2>>${LF2}
  echo "Script Ended." >>${LF1} 2>>${LF2}
  exit 1
fi

Please clarify a few things.

1) Do you only have ftp access to the remote "Windows" machine?
Please bear in mind that the ftp is not shell and that the command set is very limited. Some ftp commands are quite similar to basic shell commands.

2) What exact version of Windows is involved? Please be very precise.

3) Is this a M$ ftp server or something else?

4) Do you you have administrator access to the Windows system to a level where you could set global environment variables? I ask this because it is notoriously difficult to get a M$ Windows system to display a DIR listing in a predicatable manner through ftp without setting a DIRCMD environment variable (see "help DIR"). Even then large directories may be displayed out of order due to underlying limitations of M$ code.

5) Your examples make it clear that the file timestamp is the only way of finding the most recent file. Your example directory when sorted by filename comes out in a different order from when sorted by file timestamp.

6) Footnote: If this is an application under development it is well worth considering including a sortable numeric timestamp in the filename (yyyymmddhhmmssfff).