问题
I've written a batch file to copy files from one server to another, however, i need to be able to rename the file just copied to contain the folder path. The code i have come up with to do the job is:
ECHO OFF
SETLOCAL EnableDelayedExpansion
set include=*.log
FOR /L %%i IN (1,2,3) DO (
net use i: \\my-server%%i\d$\IISLogs
FOR /R i:\ %%G IN (%include%) DO (
XCOPY %%G D:\ServerLogsAndBackups\IIS\w%%i\
)
7z a -t7z D:\ServerLogsAndBackups\IIS\w%%i\files%%i.7z *.log -mx9
net use i: /delete
)
The file would be coming from something like:
i:\w3svc98435783475\ex110430.log
And what I want to do is copy it into D:\ServerLogsAndBackups\IIS\w1\w3svc98435783475_ex110430.log. I'm unsure how to get the directory path on the remote to put into the filename.
many thanks
回答1:
If you know the depth of the files are only 1 folder in, you can use the following
ECHO OFF
SETLOCAL EnableDelayedExpansion
set include=*.log
FOR /L %%i IN (1,2,3) DO (
net use i: \\my-server%%i\d$\IISLogs
FOR /R i:\ %%G IN (%include%) DO (
FOR /F "tokens=1-2 delims=\" %%H IN ("%%~pnxG") DO (
XCOPY %%G D:\ServerLogsAndBackups\IIS\w%%i\%%H_%%I
)
)
7z a -t7z D:\ServerLogsAndBackups\IIS\w%%i\files%%i.7z *.log -mx9
net use i: /delete
)
If the files are a set number of folders deep, you can adjust the tokens as required and add additional letters to the end of the XCOPY command (i.e. 5 folders deep: tokens=6 and in the XCOPY command it will be %%H_%%I_%%J_%%K_%%L_%%M)
However, if there is a mix of folder depths, you may be better off looking into using something other than Batch scripting to accomplish this.
来源:https://stackoverflow.com/questions/6357046/batch-file-to-copy-files-from-one-server-to-other-adding-remote-directory-to-fil