Tech Off Thread

12 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

add the remote folder i'm in to the filename, batchfile

Back to Forum: Tech Off
  • User profile image
    Jaz

    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
    
    )

     

    using the code above, I want to insert the folder i'm currently in in %%G into the filename that is put in to destination in the xcopy. 

     

    So it might be coming from: i:\w3svc98435783475\ex110430.log and i want it to end up as D:\ServerLogsAndBackups\IIS\w1\w3svc98435783475_ex110430.log

     

    anyone got any ideas?

  • User profile image
    spivonious

    Could you use cd? When called without arguments it prints out the current path. Then some way to get just the folder from that.

  • User profile image
    Jaz
  • User profile image
    AndyC

    , Jaz wrote

    Is there a better way of doing this?

    Powershell? Tongue Out

  • User profile image
    Jaz

    @AndyC is powershell much easier for this kinda thing? i've no idea.

  • User profile image
    AndyC

    Pretty much anything over and above executing a simple list of commands is much, much easier in Powershell.

    1,2,3 |
    foreach {
      $count = $_ ; dir -Path { "\\my-server" + $count+ "\d$\IISLogs\*.log" } |
      Copy-Item -Destination {"D:\ServerLogsAndBackups\IIS\w" + $count +"\"   +  $_.Directory.Name+"_"+$_.Name}
    }

    Or something a bit like it ought to do the trick.

  • User profile image
    Jaz

    @AndyC:

    I tried this

     

    1,2,3 |
    foreach { 
        $count = $_; 
        $x = gci -Path \\myserver-web$count\d$\IISLogs\ -include *.log -recurse
    
        $x | Copy-Item -Destination D:\ServerLogsAndBackups\IIS\w$count\$_.DirectoryName_$_.Name -whatIf
    }

     

    however $_.DirectoryName_$_.Name aren't outputting anything... any ideas?

  • User profile image
    Jaz

    @Jaz: Halfway there with this

     

    1,2,3 |
    foreach { 
        $count = $_;
        $x = gci -Path \\myserver-web$count\d$\IISLogs\ -include *.log -recurse
        $x | ForEach-Object {
            Copy-Item $x -Destination $("D:\ServerLogsAndBackups\IIS\w"+$count+"\"+$_.Directoryname+"_"+$_.Name) -whatIf
        }
            
    }

     

    however it's appending too much to the destination: D:\ServerLogsAndBackups\IIS\w1\uktv-web1\d$\IISLogs\W3SVC1135081669_ex110330.log

    when i want it to be: D:\ServerLogsAndBackups\IIS\w1\W3SVC1135081669_ex110330.log

     

    any tips?

  • User profile image
    Jaz

    @Jaz:

     

    1,2,3 |
    foreach { 
        $count = $_;
        $x = gci -Path \\uktv-web$count\d$\IISLogs\ -include *.log -recurse
        $x | ForEach-Object {
            Copy-Item $x -Destination $("D:\ServerLogsAndBackups\IIS\w"+$count+"\"+$_.Directory.Name+"_"+$_.Name) -whatIf
        }
            
    }

     

    cheers @AndyC

  • User profile image
    AndyC

    @Jaz:Glad to see you got it working. Smiley

  • User profile image
    Jaz

    @AndyC: I don't think i have.  

     

    really struggling with this...

    I missed the output before, i am looping over the files, copying 100 files named after the 1st file, then onto the 2nd file copying 100 files named after the 2nd, and so forth... make it stop

  • User profile image
    Jaz

    @Jaz:FECK ME

     

    1,2,3 |
    foreach { 
        $count = $_;
        gci -Path \\myserver-web$count\d$\IISLogs\ -include *.log -recurse | % { $dirName = $_.directoryname; $logname = $_.Name; $_ | Copy-Item -Destination $("D:\ServerLogsAndBackups\IIS\w"+$count+"\"+$dirname+"_"+$logName) -whatif }
    }

     

     

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.