
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
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 a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND