batch file to copy files from one server to other adding remote directory to filename in process

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 04:30:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!