如何修改批处理脚本,实现向列表内电脑批量复制文件并检查双目标路径
Got it, let's tackle this. The core issue with your original script is the mutually exclusive IF ELSE structure — it only checks the 64-bit path if the 32-bit path doesn't exist. To make it run both copies when both paths are present, we just need to split the two checks into separate, independent IF statements instead of chaining them with ELSE.
Here's the revised, optimized script:
For /F "usebackq tokens=*" %%a in ("C:\Test\PCList.txt") Do ( :: Check for 32-bit target path and run copy if it exists IF Exist "\\%%a\%Target32%" ( robocopy "%source32%" "\\%%a\%Target32%" /tbd /s /is /ndl /nfl /log+:"%logdir%\%%a_x86_%date:~10,4%.%date:~7,2%.%date:~4,2%.log" ) :: Check for 64-bit target path and run copy if it exists (no ELSE, so runs independently) IF Exist "\\%%a\%Target64%" ( robocopy "%source64%" "\\%%a\%Target64%" /tbd /s /is /ndl /nfl /log+:"%logdir%\%%a_x64_%date:~10,4%.%date:~7,2%.%date:~4,2%.log" ) )
Key Changes Explained:
- Removed the
ELSEbetween the two path checks: Now eachIFexecutes on its own. If both\\%%a\%Target32%and\\%%a\%Target64%exist on a machine, both robocopy commands will run for that device. - Added inline comments (
::) to make the script easier to maintain for your team — small touches like this save time down the line when someone needs to tweak the script.
Optional Reliability Optimization:
Your current log filename uses %date%, which depends on the system's regional date settings. If some machines use a format with slashes (like MM/DD/YYYY), those slashes will break the filename. To fix this, you can generate a consistent date string with PowerShell (built into all modern Windows systems):
:: Get a universal YYYY-MM-DD date string first For /F "delims=" %%d in ('powershell -Command "(Get-Date).ToString('yyyy-MM-dd')"') Do Set "logDate=%%d" :: Then use this consistent date in your log paths /log+:"%logdir%\%%a_x86_%logDate%.log" /log+:"%logdir%\%%a_x64_%logDate%.log"
This ensures your log files always have a valid, uniform name no matter what regional settings the target machines use.
内容的提问来源于stack exchange,提问作者MatrixMs Merlin

