This script will:
โ
Login to the Perforce server
โ
Sync the latest source code to a specific local directory
โ
Automatically handle errors and logging
1. Prerequisites
Before running the script, ensure:
โ Perforce CLI (p4.exe) is installed and accessible in PATH
โ You have a Perforce username, server address, and workspace
โ You have the correct permissions to sync files
2. Create the Batch Script
1๏ธโฃ Open Notepad
2๏ธโฃ Copy and paste the following script:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:: Set Perforce Environment Variables
SET P4PORT=perforce.company.com:1666
SET P4USER=your_username
SET P4CLIENT=your_workspace
SET LOCAL_SYNC_DIR=C:\Perforce\workspace REM Change this to your desired sync location
SET LOG_FILE=C:\Perforce\sync_log.txt REM Log file for output
:: Clear the log file
echo Perforce Sync Log - %DATE% %TIME% > %LOG_FILE%
:: Login to Perforce (store ticket)
echo Logging in to Perforce...
echo your_password | p4 login >> %LOG_FILE% 2>&1
:: Check if login was successful
p4 -s login | find "User" >nul
IF %ERRORLEVEL% NEQ 0 (
echo ERROR: Perforce login failed! Check your credentials. >> %LOG_FILE%
echo Login failed. Please check the log file.
EXIT /B 1
)
:: Syncing source code to specific location
echo Syncing files to %LOCAL_SYNC_DIR%...
p4 sync %LOCAL_SYNC_DIR%/... >> %LOG_FILE% 2>&1
IF %ERRORLEVEL% NEQ 0 (
echo ERROR: Sync failed! Check the log file for details. >> %LOG_FILE%
echo Sync failed. Please check the log file.
EXIT /B 1
)
echo Sync completed successfully! >> %LOG_FILE%
echo Sync completed successfully!
ENDLOCAL
EXIT /B 0
Code language: PHP (php)
3. Save and Run the Script
1๏ธโฃ Save the file as p4_sync.bat
- Choose All Files (
*.*) as the file type.
2๏ธโฃ Edit the script to replace: perforce.company.com:1666โ Your Perforce serveryour_usernameโ Your Perforce usernameyour_workspaceโ Your Perforce workspace nameC:\Perforce\workspaceโ Your desired sync location
3๏ธโฃ Run the script- Double-click
p4_sync.bat - It will log in, sync files, and save a log in
C:\Perforce\sync_log.txt
4. Optional Enhancements
โ Secure Login (Avoid Plaintext Passwords)
- Instead of storing the password, use:
p4 login -s >nul 2>&1 || p4 login
โ Schedule Automatic Sync
- Use Windows Task Scheduler to run the script at specific intervals.
5. Conclusion
This script automates Perforce login and syncs source code to a specified directory, handling errors and logging progress.
Would you like help with automating builds, checking out files, or submitting changes via CLI? Let me know! ๐
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals