How to programmatically assign (attach) a file to file field while creating/saving the node?
I had a similar situation. My situation is attach a file to a node in the creation of a different content type node. Done this in hook_node_insert 


Initially i uploaded all files into a temp directory in /default/files/.
Then for each node, i created a new file object with my file attributes.
Then moved that particular file into the destination where i want the files to be placed. This results a clean complete file object with fid.
Then simply assign this file object attributes to file field and save the node.


The code used is follows..

$source_node = node_load($nid);
$filename = $bio_file_name; //your specific filename 
$dir = 'public://temp-path/'.$filename; //path to specific file with name
$file_size = filesize($dir);

//New file object
$file = new stdClass;
$file-&gt;uid = $source_node-&gt;uid;
$file-&gt;filename = $filename;
$file-&gt;uri = $dir;
$file-&gt;filemime = file_get_mimetype($dir);
// Make it permanent, otherwise it will get deleted later.
$file-&gt;status = FILE_STATUS_PERMANENT;
// Save file object to make it a 'managed file'.
$file_obj = file_move($file, 'public://newpath', FILE_EXISTS_REPLACE);

$source_node-&gt;your-file-field-name['und'][0] = array(
 'fid'  =&gt; $file_obj-&gt;fid,
 'uid'  =&gt; $file_obj-&gt;uid,
 'filename'  =&gt; $file_obj-&gt;filename,
 'uri'  =&gt; $file_obj-&gt;uri,
 'filemime'  =&gt; $file_obj-&gt;filemime,
 'filesize'  =&gt; $file_obj-&gt;filesize,
 'status'  =&gt; 1,
 'display'  =&gt; 1,
 'description'  =&gt; '', 
 );

 node_save($source_node); 


Hope this will help you..