Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: copy and move files via batch file  (Read 3792 times)

0 Members and 1 Guest are viewing this topic.

Allochthonous

    Topic Starter


    Intermediate
    copy and move files via batch file
    « on: September 16, 2009, 09:05:44 AM »
    Here is the situation:  I have a group of files that are dropped daily into a network folder. Lets call them DataFile_091509_A.xls, DataFile_091509_B.xls, and DataFile_091509_C.xls.  I need to copy these files into an archive folder within the folder where these files are dropped, then move them to a local folder, while renaming them to a standard filename, ex DataFile_A.xls, DataFile_B.xls, DataFile_C.xls. I will overwrite the destination file names each day so that it inlcudes the most recent data.

    Here is what I have so far:

    REM Copy all datafiles into the Archive folder
    copy "\\PATH\*A.xls" "\\PATH\Archive" /y
    copy "\\PATH\*B.xls" "\\PATH\Archive" /y
    copy "\\PATH\*C.xls" "\\PATH\Archive" /y
    REM Move A
    move /y "\PATH\*A.xls" "c:\DataFile_A.xls"
    REM
    REM Move B
    move /y "\PATH\*B.xls" "c:\DataFile_B.xls"
    REM
    REM Move C
    move /y "\PATH\*C.xls" "c:\DataFile_C.xls"
    REM
    REM

    Now, this works just fine, but have I allowed enough room for error?  Should I include something in my MOVE that will only move the most recent version of the file in case something goes wrong the day before and files are  not moved properly?

    Any other alternatives?


    PK

    JenniC



      Rookie

      Re: copy and move files via batch file
      « Reply #1 on: October 18, 2009, 10:46:11 AM »

      For more control, you may want to do this with a scripting language. Here is a script.


      Code: [Select]
      # MoveData.txt

      # Collect a list of files \\PATH\DataFile_*_*.xls.
      var str list ; lf -n "DataFile_*_*.xls" "\\PATH" > $list

      # Go thru files one by one.
      while ($list <> "")
      do

          # Get the next file.
          var str file ; lex "1" $list > $file

          # Copy this file to \\PATH\Archive.
          system -s "copy /Y " ("\""+$file+"\"") "\\PATH\Archive"

          # Create the new file name by removing the date (between the two _).
          var str newname ; stex -p "^/^l[" $file > $newname
          # Replace _..._ with just _.
          sal -r "^_&_^" "_" $newname > null

          # Move file to C:\$newname.
          system -s "move /Y" ("\""+$file+"\"") ("\"C:\\"+$newname+"\"")
         
      done


      Script is in biterscripting ( http://www.biterscripting.com ). Save the script as C:/Scripts/MoveData.txt. Start biterscripting and to run the script, enter the following command.

      Code: [Select]
      script MoveData.txt