Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2011
    Posts
    4

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

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    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

  3. #3
    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 ...

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    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

  5. #5
    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.

  6. #6
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    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

  7. #7
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    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

  8. #8
    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 ?

  9. #9
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •