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

Good Day Al

I have the following batch script that loops through a folder containing .sql files. When it finds a .sql file that has today's date timestamp on it, it copies that file to a new directory.

@echo off
setlocal enableextensions enabledelayedexpansion

set "currentDate=%date:~0,10%"
    for %%g in ("c:\mfa\*.sql") do (
     set "fileDate=%%~tg"
     set "fileDate=!fileDate:~0,10!"
     if "!fileDate!"=="%currentDate%" (
        copy "%%~fg" "c:\newLocation"
     )
    )

My PROBLEM:

This works great on Windows 7, but not on Windows Server 2008. When I echo the filedate variable on Win7, it gives me the timestamp saved in the !fileDate! value. But when I echo !fileDate! in Windows Server 2008, it returns: ECHO is off.

This still does not work even if I remove delayedexpansion.

Why is it not working on Server 2008?

==================== UPDATE -

Powershell Error

The term 'test.ps1' is not recognized as the name of a cmdlet, function, script file, or operable pro
elling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:9
+ test.ps1 <<<<
    + CategoryInfo          : ObjectNotFound: (test.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
share|improve this question
1  
Is this 2008 or 2008 R2? Also, why batch and not PowerShell at this point? –  MDMarra Feb 3 at 12:41
    
Because I do not know how to do this in Powershell... –  DextrousDave Feb 3 at 12:42

1 Answer 1

up vote 3 down vote accepted

I agree with MDMarra's comment about using Powershell.

We are in 2014 and your plateform is Windows 2008 Server...so i strongly advice you to consider Batch a bit "obsolete"...

Using Powershell, something like this :

$today=Get-Date -format d
$files=Get-ChildItem "C:\mfa" -Filter *.sql
foreach ($file in $files)
{
  $filename=$file.FullName
  $dateCreated=$file.CreationTime | Get-Date -format d
  $dateModified=$file.LastWriteTime | Get-Date -format d
  write-output "Today is : $today"
  write-output "Scanning file : $filename"
  write-output "Modified : $dateModified"
  write-output "Created : $dateCreated"
  if($dateCreated -eq $today)
  {
    copy-item $filename c:\newLocation
  }
  write-output "---------------------------------------------"
}

Assuming comparison on Creation Date. To compare against Last Modified Date, use LastWriteTime instead of CreationTime.

share|improve this answer
    
thank you, but I cannot get it to work. Also, how do I check the output of the Powershell console that flashes by in milliseconds so that I can see why it is not working(like 'pause' in cmd? All I see is a powershell screen with red text on it... –  DextrousDave Feb 3 at 13:29
    
@DextrousDave You can add Read-host 'finished' at the end of the script. It will wait for you to press Enter –  krisFR Feb 3 at 13:32
    
nope does not work.Still closes. I saved the code as a .ps1 file and ran it: right click on file > Run with Powershell –  DextrousDave Feb 3 at 13:34
    
Open a Powershell console and run the ps1 script from the console. What happens then ? –  krisFR Feb 3 at 13:36
    
OK I get an error: I posted it at part of my answer... –  DextrousDave Feb 3 at 13:42

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.