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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How I can capture the last file in a directory in windows
# 1  
Old 04-06-2010
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

Code:
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
Code:
 
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

Last edited by pludi; 04-07-2010 at 02:03 AM..
# 2  
Old 04-06-2010
Hammer & Screwdriver last file

Code:
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.
# 3  
Old 04-06-2010
Do you mean the file with the latest filetime (most recent file)?
Try one of these:
Code:
lastfile=$(ls -rt | tail -1)
# or
lastfile=$(ls -t | head -1)

Assuming I got what you wanted...
# 4  
Old 04-07-2010
Well, I am getting the following:
Here is the directory in unix
N:\orgs\Financial Aid\CSSIDOCLoad (explore)
here is my piece of code:

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

It is not finding the directory
Code:
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
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


Last edited by pludi; 04-08-2010 at 01:51 AM..
# 5  
Old 04-07-2010
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).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Samba 3.6 on AIX 7.1 - Windows 10 Access to AIX file shares using Active Directory authentication

I am running AIX 7.1 and currently we have samba 3.6.25 installed on the server. As it stands some AIX folders are shared that can be accessed by certain Windows users. The problem is that since Windows 10 the guest feature no longer works so users have to manually type in their Windows login/pwd... (14 Replies)
Discussion started by: linuxsnake
14 Replies

2. Solaris

Copy directory from solaris to windows.

Hi All I am new to Unix/sloaris. I want to copy a directory (which conatins further sub directories and files)from sloaris to my windows.I am using PuTTy for it. please any one guide me how to do this or share with me some piece of script. It will be very regarded Best Wishes MIS. (3 Replies)
Discussion started by: MIS
3 Replies

3. Programming

Copy directory from solaris to windows.

Hi All I am new to Unix/sloaris. I want to copy a directory (which conatins further sub directories and files)from sloaris to my windows.I am using PuTTy for it. please any one guide me how to do this or share with me some piece of script. It will be very regarded Best Wishes MIS. (1 Reply)
Discussion started by: MIS
1 Replies

4. UNIX for Dummies Questions & Answers

Copy directory from Linux to Windows

Hola a todos, soy realmente nuevo programando en bash, y aśn estoy encontrando bastantes baches :o, mi pregunta es simple, supongo, alguien podria decirme si existe una manera facil de copiar un directorio de un server a otro?, sucede que en un server(server1) tengo los backup de las bases de... (7 Replies)
Discussion started by: yeestrada
7 Replies

5. Shell Programming and Scripting

Move a file from windows directory to unix directory

Move a file from windows directory to unix directory, is this possible? if it is, can someone help me on this? Thanks! God bless! (1 Reply)
Discussion started by: kingpeejay
1 Replies

6. Shell Programming and Scripting

capture output of file and send last string thereof to new file

Hello, If I run a program from within shell, the output is displayed in the command line terminal. Is there a way I can capture that output and choose only the very last string in it to send it to a new file? Thank you (6 Replies)
Discussion started by: Lorna
6 Replies

7. Shell Programming and Scripting

Script to capture new lines in a file and copy it to new file

Hi All, I have a file that gives me new line/output every 5 minutes. I need to create a script that capture new line/output besides "IN CRON_STATUS", in this case the new output is "begin ------ cron_status.sh - -----------". I want this script to capture the line starting from "begin ------... (0 Replies)
Discussion started by: fara_aris
0 Replies

8. Shell Programming and Scripting

capture the file name

I am trying to capture the file name(which is not of fixed length) and put it in a variable. It is working in unix whereas when I am running the same script in Informatica it is not giving me the desired output. But when I comment the option(find the file name) then it is working fine. It can... (4 Replies)
Discussion started by: Mandab
4 Replies

9. UNIX for Advanced & Expert Users

Capture Value from file

I have a file in the following Format Fundid: 100-BankA AccountNumber Balance 1 200 2 300 3 400 FundId:123321-BankB AccountNumber Balance 1 200 3 100 ........... I can have N number of funds. (1 Reply)
Discussion started by: kris01752
1 Replies

10. UNIX for Dummies Questions & Answers

moving files from a unix directory to a windows directory

Any body any ideas i'm failry new to this so any help would be appreciated. Cheers Steve (2 Replies)
Discussion started by: gleads
2 Replies
Login or Register to Ask a Question