{"id":4543,"date":"2018-01-08T12:00:29","date_gmt":"2018-01-08T12:00:29","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=4543"},"modified":"2025-02-01T22:39:33","modified_gmt":"2025-02-01T22:39:33","slug":"syncing-missing-modified-files-to-a-specific-version-in-perforce","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/syncing-missing-modified-files-to-a-specific-version-in-perforce\/","title":{"rendered":"Syncing missing\/modified files to a specific version in Perforce"},"content":{"rendered":"<p>I think this is the 5th time I&#8217;ve had to figure this out in the past few years, so this time I&#8217;m going to preserve it here for posterity\u00a0<img decoding=\"async\" src=\"http:\/\/www.andrewgrant.org\/wordpress\/wp-includes\/images\/smilies\/icon_smile.gif\" alt=\":)\" \/><\/p>\n<p>The problem is usually one or more of:<\/p>\n<ul>\n<li>You have files missing from your workspace that need replaced.<\/li>\n<li>You have locally modified (i.e. not opened for edit) files that need replaced.<\/li>\n<li>These files are large in either size or number, and on a slow or remote server, so a complete force-sync is not an appealing option.<\/li>\n<\/ul>\n<p>Normally this could easily be solved with something like\u00a0<em>p4 diff -sd\/-se \/\/depot\/path\/.. | p4 -x \u2013 sync -f<\/em>, which finds all the missing\/outdated files and then force-syncs them.<\/p>\n<p>The problem with this approach comes when you need the files from a specific revision or label. While you can pass in a label\/revision to &#8216;p4 diff&#8217;, the sync part will always retrieve the latest version of a file. To fix this we need to save the list of files we need to a temporary location, then postfix each line with the version we want, then feed that new list back to p4 sync.<\/p>\n<p>But there&#8217;s one final catch. If your filenames are local paths instead of Perforce paths, you cannot specify a label, only revisions. E.g. c:\\temp\\foo.dat@SOME_LABEL will not work, but c:\\temp\\foo.dat#999 will. Fortunately we can use the pseudo revision &#8216;have&#8217; for our purposes.<\/p>\n<p><em>@echo off<\/em><\/p>\n<p><em>REM Could optionally pass these as %1 and %2 from the cmd line<br \/>\nset LABEL=@SOME_LABEL<br \/>\nset BASE_DIR=\/\/depot\/path\/\u2026<\/em><\/p>\n<p><em>del p4sync.txt<\/em><\/p>\n<p><em>echo Setting &#8216;have&#8217; to %LABEL% for %BASE_DIR%<br \/>\nREM do a nop sync that sets our &#8216;have&#8217; version to the label we want<br \/>\np4 sync -k %BASE_DIR%%LABEL%<\/em><\/p>\n<p><em>echo Finding modified files..<br \/>\np4 diff -se %BASE_DIR%%LABEL% &gt; p4diff.txt<\/em><\/p>\n<p><em>echo Finding missing files..<br \/>\np4 diff -sd %BASE_DIR%%LABEL% &gt;&gt; p4diff.txt<\/em><\/p>\n<p><em>echo Building sync commands<br \/>\nREM Append #have to each filename and put them all in a list of files to sync<br \/>\nfor \/f %%i in (p4diff.txt) Do echo %%i#have&gt;&gt; p4sync.txt<\/em><\/p>\n<p><em>REM Now sync everything<br \/>\nif exist p4sync.txt (<br \/>\necho Syncing..<br \/>\np4 -x p4sync.txt sync -f<br \/>\n)else (<br \/>\necho It seems no files need updated!<br \/>\n)<\/em><\/p>\n<p>Voila!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I think this is the 5th time I&#8217;ve had to figure this out in the past few years, so this time I&#8217;m going to preserve it here for posterity\u00a0 The problem is usually one or more of: You have files missing from your workspace that need replaced. You have locally modified (i.e. not opened for&#8230;<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[370],"tags":[273,832,2916,4471,472,373,1912],"class_list":["post-4543","post","type-post","status-publish","format-standard","hentry","category-perforce","tag-files","tag-find","tag-missing","tag-missing-modified-files","tag-modified","tag-perforce","tag-sync"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/4543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=4543"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/4543\/revisions"}],"predecessor-version":[{"id":4544,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/4543\/revisions\/4544"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=4543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=4543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=4543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}