1

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

Jarede
  • 3,310
  • 4
  • 44
  • 68

1 Answers1

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.

Anthony Miller
  • 15,101
  • 28
  • 69
  • 98
  • I get this: Does destination specify a file name or directory name on the target (F = file, D = directory)? I obviously want it to be a File.. Do you know how I can suppress this? – Jarede Jun 15 '11 at 16:09
  • I added .* onto the end of the xcopy which seemed to do the trick (though my files now end in .log.log) so if you have a neater solution to that could you edit your post? Thank you Mechaflash – Jarede Jun 15 '11 at 16:21
  • When expanding variables via %~G where G = your FOR temporary variable, you can use a number of arguments before G. The arguments include f=full path name (i.e. C:\Path\File.extension), d=drive (i.e. C:\) p=path (i.e. folder\folder), n=filename W/O the extension, x=extention of file. For example we have a file located at C:\Documents and Settings\User\desktop\file.txt, %%~nxG means only the filename and extension of G (file.txt) %%~pnxG means the path and filename and extension (Documents and Settings\User\desktop\file.txt) – Anthony Miller Jun 15 '11 at 17:14
  • If you have CD to the directory where the file is being stored, you can just use a filename, but the destination will have to be the full path name including the name of the file. – Anthony Miller Jun 15 '11 at 17:20
  • Not sure why your files are showing up as .log.log. I don't see anything I put in that would do that. NOTE: you are doing this on test files right? lol – Anthony Miller Jun 15 '11 at 17:34
  • nope not test, but since i'm not altering the original files it should be ok... will look at this in the morning now. – Jarede Jun 15 '11 at 17:55