Windows – Scheduled service/script/batch file to move files on condition of other files with similar filenames in same directory on windows

batch-filewindowswindows-server-2008

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

Best Answer

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
        }
    }
}