Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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. '' would need both '' AND '' 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%
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 )

share|improve this question
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


foreach ($file in (get-childitem $folder_source))
    foreach ($ext in $twin_files)
        if (!(Test-Path $filetocheck))
            write-Output "$filetocheck not exist"
    if ($move -eq $true)
        write-output "files are being moved for $($file.FullName)"
        move-Item $file.FullName $folder_Dest
        foreach ($ext in $twin_files)
              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


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.