基于Windows Batch脚本实现SFTP上传文件后移至远程上级目录的技术方案咨询
Got it, let's work through this problem together. Since you're stuck with Windows Batch and the native SFTP client (no WinSCP), here are two reliable approaches to ensure files are only moved to the target directory after full upload—preventing premature consumption by the vendor's system.
Approach 1: Batch Rename with Wildcards (Simplest, If Supported)
Most modern SFTP servers (like OpenSSH) support wildcard patterns in the rename command, which lets you move all matching files in one go. This is the easiest solution if your vendor's server allows it.
Step 1: Update Your SFTP Script (myfile.scr)
Modify your existing script to add a wildcard rename command right after uploading:
cd /from_client/payments/upload lcd E:/uploadfiles mput uploadfiles.xml.* # Move all uploaded files to the parent directory rename uploadfiles.xml.* ../ quit
Step 2: Run the Script via Batch
Call this updated script from your Batch file as usual:
sftp -i your_private_key.pem sftp_user@vendor_server -b myfile.scr
Note: Replace your_private_key.pem and sftp_user@vendor_server with your actual SFTP credentials. If you use password auth, consider using PuTTY's psftp instead (native Windows SFTP doesn't support plaintext password input safely).
Approach 2: Per-File Rename (For Servers That Don't Support Wildcards)
If the vendor's SFTP server doesn't allow wildcards in rename, we'll fetch the list of uploaded files and generate individual rename commands.
Step 1: Create an Upload Script (upload.scr)
First, a dedicated script to handle just the upload:
cd /from_client/payments/upload lcd E:/uploadfiles mput uploadfiles.xml.* quit
Step 2: Build the Batch Script (upload_and_move.bat)
This Batch file will handle upload, fetch the file list, generate move commands, and execute them:
@echo off setlocal enabledelayedexpansion :: -------------------------- :: Step 1: Upload files to temp directory :: -------------------------- sftp -i your_private_key.pem sftp_user@vendor_server -b upload.scr :: -------------------------- :: Step 2: Fetch list of uploaded files from remote server :: -------------------------- echo cd /from_client/payments/upload > get_list.scr echo ls uploadfiles.xml.* >> get_list.scr echo quit >> get_list.scr sftp -i your_private_key.pem sftp_user@vendor_server -b get_list.scr > remote_files.txt :: -------------------------- :: Step 3: Generate move commands for each file :: -------------------------- echo cd /from_client/payments/upload > move_files.scr :: Skip the first line of ls output (server prompt) and extract filenames for /f "tokens=* skip=1" %%f in (remote_files.txt) do ( if not "%%f"=="" ( :: Handle cases where ls output includes extra metadata (adjust tokens if needed) :: If ls shows permissions/size, use tokens=8 (or last token) instead of tokens=* echo rename "%%f" "../%%f" >> move_files.scr ) ) echo quit >> move_files.scr :: -------------------------- :: Step 4: Execute the move commands :: -------------------------- sftp -i your_private_key.pem sftp_user@vendor_server -b move_files.scr :: -------------------------- :: Clean up temporary files (optional) :: -------------------------- del get_list.scr move_files.scr remote_files.txt endlocal
Key Notes for Approach 2:
- Adjust
tokensin thefor /floop: If your SFTP server'slsoutput includes extra data (like permissions, file size, or timestamps), you'll need to tweak thetokensvalue to grab only the filename. For example, if output looks like-rw-r--r-- 1 user group 1024 uploadfiles.xml.001, usetokens=8to get the last value (the filename). - Test first: Comment out the
delcommand initially to check if the generatedmove_files.scrhas correct rename commands before running it.
Both methods ensure files are only moved to the vendor's target directory after they're fully uploaded to the temporary upload subdirectory, eliminating the risk of premature processing.
内容的提问来源于stack exchange,提问作者Alex Noble

