{"id":85,"date":"2025-02-01T22:31:39","date_gmt":"2025-02-01T22:31:39","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2008\/11\/24\/batch-script-to-login-to-perforce-and-sync-the-source-code-to-specific-location\/"},"modified":"2025-02-01T22:39:29","modified_gmt":"2025-02-01T22:39:29","slug":"batch-script-to-login-to-perforce-and-sync-the-source-code-to-specific-location","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/batch-script-to-login-to-perforce-and-sync-the-source-code-to-specific-location\/","title":{"rendered":"Batch Script to Login &amp; Sync Source Code from Perforce Server to a Specific Location"},"content":{"rendered":"\n<p>This script will:<br>\u2705 <strong>Login<\/strong> to the Perforce server<br>\u2705 <strong>Sync the latest source code<\/strong> to a <strong>specific local directory<\/strong><br>\u2705 <strong>Automatically handle errors and logging<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Prerequisites<\/strong><\/h2>\n\n\n\n<p>Before running the script, ensure:<br>\u2714 <strong>Perforce CLI (<code>p4.exe<\/code>)<\/strong> is installed and accessible in <code>PATH<\/code><br>\u2714 You have a <strong>Perforce username, server address, and workspace<\/strong><br>\u2714 You have the correct <strong>permissions<\/strong> to sync files<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Create the Batch Script<\/strong><\/h2>\n\n\n\n<p>1\ufe0f\u20e3 <strong>Open Notepad<\/strong><br>2\ufe0f\u20e3 <strong>Copy and paste the following script:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">@<span class=\"hljs-keyword\">echo<\/span> off\nSETLOCAL ENABLEDELAYEDEXPANSION\n\n:: Set Perforce Environment Variables\nSET P4PORT=perforce.company.com:<span class=\"hljs-number\">1666<\/span>\nSET P4USER=your_username\nSET P4CLIENT=your_workspace\nSET LOCAL_SYNC_DIR=C:\\Perforce\\workspace  REM Change this to your desired sync location\nSET LOG_FILE=C:\\Perforce\\sync_log.txt      REM Log file <span class=\"hljs-keyword\">for<\/span> output\n\n:: Clear the log file\n<span class=\"hljs-keyword\">echo<\/span> Perforce Sync Log - %DATE% %TIME% &gt; %LOG_FILE%\n\n:: Login to Perforce (store ticket)\n<span class=\"hljs-keyword\">echo<\/span> Logging in to Perforce...\n<span class=\"hljs-keyword\">echo<\/span> your_password | p4 login &gt;&gt; %LOG_FILE% <span class=\"hljs-number\">2<\/span>&gt;&amp;<span class=\"hljs-number\">1<\/span>\n\n:: Check <span class=\"hljs-keyword\">if<\/span> login was successful\np4 -s login | find <span class=\"hljs-string\">\"User\"<\/span> &gt;nul\n<span class=\"hljs-keyword\">IF<\/span> %ERRORLEVEL% NEQ <span class=\"hljs-number\">0<\/span> (\n    <span class=\"hljs-keyword\">echo<\/span> ERROR: Perforce login failed! Check your credentials. &gt;&gt; %LOG_FILE%\n    <span class=\"hljs-keyword\">echo<\/span> Login failed. Please check the log file.\n    <span class=\"hljs-keyword\">EXIT<\/span> \/B <span class=\"hljs-number\">1<\/span>\n)\n\n:: Syncing source code to specific location\n<span class=\"hljs-keyword\">echo<\/span> Syncing files to %LOCAL_SYNC_DIR%...\np4 sync %LOCAL_SYNC_DIR%\/... &gt;&gt; %LOG_FILE% <span class=\"hljs-number\">2<\/span>&gt;&amp;<span class=\"hljs-number\">1<\/span>\n\n<span class=\"hljs-keyword\">IF<\/span> %ERRORLEVEL% NEQ <span class=\"hljs-number\">0<\/span> (\n    <span class=\"hljs-keyword\">echo<\/span> ERROR: Sync failed! Check the log file <span class=\"hljs-keyword\">for<\/span> details. &gt;&gt; %LOG_FILE%\n    <span class=\"hljs-keyword\">echo<\/span> Sync failed. Please check the log file.\n    <span class=\"hljs-keyword\">EXIT<\/span> \/B <span class=\"hljs-number\">1<\/span>\n)\n\n<span class=\"hljs-keyword\">echo<\/span> Sync completed successfully! &gt;&gt; %LOG_FILE%\n<span class=\"hljs-keyword\">echo<\/span> Sync completed successfully!\n\nENDLOCAL\n<span class=\"hljs-keyword\">EXIT<\/span> \/B <span class=\"hljs-number\">0<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Save and Run the Script<\/strong><\/h2>\n\n\n\n<p>1\ufe0f\u20e3 Save the file as <strong><code>p4_sync.bat<\/code><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Choose <strong>All Files (<code>*.*<\/code>)<\/strong> as the file type.<br>2\ufe0f\u20e3 <strong>Edit the script<\/strong> to replace:<\/li>\n\n\n\n<li><code>perforce.company.com:1666<\/code> \u2192 Your Perforce server<\/li>\n\n\n\n<li><code>your_username<\/code> \u2192 Your Perforce username<\/li>\n\n\n\n<li><code>your_workspace<\/code> \u2192 Your Perforce workspace name<\/li>\n\n\n\n<li><code>C:\\Perforce\\workspace<\/code> \u2192 Your desired sync location<br>3\ufe0f\u20e3 <strong>Run the script<\/strong><\/li>\n\n\n\n<li>Double-click <code>p4_sync.bat<\/code><\/li>\n\n\n\n<li>It will log in, sync files, and save a log in <code>C:\\Perforce\\sync_log.txt<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Optional Enhancements<\/strong><\/h2>\n\n\n\n<p>\u2714 <strong>Secure Login (Avoid Plaintext Passwords)<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Instead of storing the password, use: <code>p4 login -s >nul 2>&amp;1 || p4 login<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u2714 <strong>Schedule Automatic Sync<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <strong>Windows Task Scheduler<\/strong> to run the script at specific intervals.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. Conclusion<\/strong><\/h2>\n\n\n\n<p>This script <strong>automates Perforce login and syncs source code<\/strong> to a specified directory, handling errors and logging progress.<\/p>\n\n\n\n<p>Would you like help with <strong>automating builds, checking out files, or submitting changes via CLI<\/strong>? Let me know! \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This script will:\u2705 Login to the Perforce server\u2705 Sync the latest source code to a specific local directory\u2705 Automatically handle errors and logging 1. Prerequisites Before running the script, ensure:\u2714 Perforce CLI (p4.exe) is installed and accessible in PATH\u2714 You have a Perforce username, server address, and workspace\u2714 You have the correct permissions to sync&#8230;<\/p>\n","protected":false},"author":1,"featured_media":3619,"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":[276,1916,1918,1919,1122,545,330,897,1915,373,1917,593,278,1913,1914,1920,1912],"class_list":["post-85","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-perforce","tag-batch","tag-batch-script","tag-batch-script-reference","tag-batch-script-tutorial","tag-guide","tag-how","tag-location","tag-login","tag-particular","tag-perforce","tag-perforce-login","tag-process","tag-script","tag-source-code","tag-specific","tag-specific-location","tag-sync"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/85","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":48320,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/85\/revisions\/48320"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/3619"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}