If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Move txt file to with current date appended to filename

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-07-11, 21:56
johannd johannd is offline
Registered User
 
Join Date: Dec 2011
Posts: 4
Move txt file to with current date appended to filename

I have multiple txt files which begin with the word "orders" in folder C:\source. I need to move the files to folder C:\dest and rename them to "process_<date>_<count>"

So for example , if there are 3 files ordersa.txt , ordersb.txt and ordersc.txt in C:\source , after running the script I want these files to be in C:\dest with filenames process_12072011_01.txt , process_12072011_02.txt , process_12072011_03.txt
Reply With Quote
  #2 (permalink)  
Old 12-08-11, 09:59
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Thumbs down

Quote:
Originally Posted by johannd View Post
I have multiple txt files which begin with the word "orders" in folder C:\source. I need to move the files to folder C:\dest and rename them to "process_<date>_<count>"

So for example , if there are 3 files ordersa.txt , ordersb.txt and ordersc.txt in C:\source , after running the script I want these files to be in C:\dest with filenames process_12072011_01.txt , process_12072011_02.txt , process_12072011_03.txt
This does NOT look like Unix solution unless you wish to use Cygwin, is this the case?
Otherwise I suggest you move the question to the WinDoze forum.
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 12-08-11, 10:19
johannd johannd is offline
Registered User
 
Join Date: Dec 2011
Posts: 4
you right



yup , i am using cygwin , since i do not have a unix system right now. could you please help me out on the script. I have written other scripts which run fine in cygwin, and im able to run mv command to move single files between folders, but im stumped with moving multiple files and putting in the dates and running counter to the target files.

FYI,
12072011 - current date MMDDYYYY format
01 - running counter starting from 01, 02 ...
Reply With Quote
  #4 (permalink)  
Old 12-08-11, 13:14
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
Code:
list=`ls order*`
count=101
for file in $list
do
   mv $file ../dest/process_`date +"%m%d%y"`_`echo $count|cut -c2-`.txt
   expr count = $count + 1
done
Initialize count to 1001 if there are more than 100 entries in the original directory.
I didn't test it
Reply With Quote
  #5 (permalink)  
Old 12-08-11, 13:26
johannd johannd is offline
Registered User
 
Join Date: Dec 2011
Posts: 4
Thank you,

Move.sh: line 4: syntax error near unexpected token `$'do\r''

Is there something i need to install ? I am using cygwin on windows.
Reply With Quote
  #6 (permalink)  
Old 12-08-11, 14:42
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool

Quote:
Originally Posted by johannd View Post
Thank you,

Move.sh: line 4: syntax error near unexpected token `$'do\r''

Is there something i need to install ? I am using cygwin on windows.
You need to save the script in "unix" format (not winDoze).
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #7 (permalink)  
Old 12-08-11, 20:27
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
Something I thought of afterwards. If you want to run this more than once per day,

Code:
today=`date +"%m%d%y"`
if [ -r $today ]
then 
   read count <$today
else
   count=101
fi
list=`ls order*`
for file in $list
do
   mv $file ../dest/process_`date +"%m%d%y"`_`echo $count|cut -c2-`.txt
   count=`expr $count + 1`
done
echo $count >$today
Reply With Quote
  #8 (permalink)  
Old 12-11-11, 18:02
johannd johannd is offline
Registered User
 
Join Date: Dec 2011
Posts: 4
thanks

worked great ! It didnt work in cygwin , but it worked great on a unix box.
so is cygwin not a true unix environement for windows ? if there is some command that does not work in cygwin , is it possible to switch it on / install ?
Reply With Quote
  #9 (permalink)  
Old 12-11-11, 20:36
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
I got it to run in cygwin, but....
In line one, there must not be spaces on either side of the equal sign.
In line 10, there appears to be a carriage return in the text if you copy and paste from the post above.
Vi just didn't seem to work at all, so I used Notepad++ to create the file, and then ran the following line of code to remove any carriage returns.
Code:
tr -d "\r" <inputfile >outputfile
You can set the line endings in Notepad++ to either DOS, Unix or Mac files.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On