运行批处理文件时提示“系统找不到指定文件”求助
Hey there, let's dig into why your batch script is throwing that frustrating "系统找不到指定文件" error. Looking at your code snippet, here are the most likely issues and fixes to get your backup running smoothly:
Common Causes & Solutions
1. Target Directory Doesn't Exist
First things first: double-check if the folder C:\Web\FTP\A\B\ actually exists on your system. By default, xcopy won't create missing directories, so if this path isn't there, it'll throw a "file not found" error (even though it's the directory that's missing).
Fix: Either manually create the B folder, or add a check in your script to create it automatically before running xcopy:
:: Check if target directory exists; create it if not IF NOT EXIST "C:\Web\FTP\A\B\" MKDIR "C:\Web\FTP\A\B\" xcopy /Y %%f "C:\Web\FTP\A\B\"
2. Date/Time Variable Format Mismatch
Your code uses !date:~10,4!!date:~7,2!!date:~4,2! and !time:~0,2!!time:~3,2!!... to build timestamps, but Windows date/time formats vary by system locale (e.g., Chinese systems might use yyyy/mm/dd or dd/mm/yyyy, and times might have leading spaces like 9:30:00). If your substring cuts are off, this can generate invalid filenames, leading to "file not found" errors when you try to rename or use those files later.
Fix: Use a more reliable method to get a consistent timestamp, like wmic which returns a standard format:
:: Get standardized datetime using wmic for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" set "mydate=!dt:~0,8!" :: YYYYMMDD format set "mytime=!dt:~8,6!" :: HHMMSS format
3. Truncated Script Syntax
Your code snippet cuts off at SET "mytime=!time:~0,2!!time:~3,2!!...—if your actual script has an incomplete SET statement here, that'll cause syntax errors that can manifest as "file not found" messages.
Fix: Make sure all your SET lines are fully closed. For example, a complete time variable line should look like:
SET "mytime=!time:~0,2!!time:~3,2!!time:~6,2!"
4. Source Files Don't Exist
Quick sanity check: confirm there are actually .txt files in C:\Web\FTP\A\. If the folder is empty, the FOR %%f in (C:\Web\FTP\A\*.txt) loop won't run, but if you have other code relying on those files, it could throw an error.
Final Tip
Start by testing small parts of your script—echo out variables like !file!, !mydate!, and !mytime! to make sure they're generating valid values, and verify that both source and target paths are correct before running the full backup.
内容的提问来源于stack exchange,提问作者Seth Hong

