Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

On Windows Server (Data Center? 2008?), i'm trying to set up a scheduled task that will:

  1. Within a particular directory
  2. For every file in it
  3. If there exists (in the same directory) 2 files with similar names (actually the same name with extra extensions tagged on, ie. 'file1.mov' would need both 'file1.mov.flv' AND 'file1.mov.mpg' to exist), then move the file to another directory on a different disk.

Following is what i have so far for a batch file, but i'm struggling. I'm also open to another technique/mechanism.

@setlocal enableextensions enabledelayedexpansion
@echo off

SET MoveToDirectory=M:\_SourceVideosFromProduction
ECHO MoveToDirectory=%MoveToDirectory%
pause
for /r %%i in (*) do (

    REM ECHO %%i
    REM ECHO %%~nxi
    REM ECHO %%~ni
    REM ECHO filename=%filename%

    REM SET CurrentFilename=%%~ni
    REM ECHO CurrentFilename=%CurrentFilename%

    IF NOT %%~ni==__MoveSourceFiles (
        IF NOT x%%%~ni:\.=%==x%%%~ni% DO (
        REM SET HasDot=0

        REM FOR /F %%g IN %filename% do (
        REM     IF %%g==. (
                ECHO %filename%
        REM )
        )
    )
)

pause
share|improve this question

1 Answer 1

up vote 1 down vote accepted

here is the way to do it in powershell. Save as a .ps1 file. Set execution to remotesigned from powershell prompt (launch with run as administrator): Set-ExecutionPolicy RemoteSigned

I created and tested this script for you based on what you asked

You need then to create a scheduled task that call powershell.exe with the script as argument

$folder_source="c:\source"
$folder_dest="c:\dest"
$twin_files=@(".flv",".mpg")

foreach ($file in (get-childitem $folder_source))
{
    $move=$true
    foreach ($ext in $twin_files)
    {
        $filetocheck=$file.FullName+"$ext"
        if (!(Test-Path $filetocheck))
        {
            write-Output "$filetocheck not exist"
            $move=$false
        }
    }
    if ($move -eq $true)
    {
        write-output "files are being moved for $($file.FullName)"
        move-Item $file.FullName $folder_Dest
        foreach ($ext in $twin_files)
        {
              $filetocheck=$file.FullName+"$ext"
              move-Item $filetocheck $folder_Dest
        }
    }
}
share|improve this answer
    
Wow, this is fantastic, thank you so much! I've been wanting to get deeper into Power Shell and this was a perfect intro. I tweaked a few things (not moving the 2 child files, only the parent, keeping track of how many are moved and the size, etc.), and this is working beautifully! –  ilasno May 11 '12 at 18:13
    
@ilasno enjoy :) –  Mathieu Chateau May 11 '12 at 19:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.