0

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
DextrousDave
  • 315
  • 2
  • 4
  • 13

1 Answers1

3

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.

krisFR
  • 13,280
  • 4
  • 36
  • 42
  • 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 03 '14 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 03 '14 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 03 '14 at 13:34
  • Open a Powershell console and run the ps1 script from the console. What happens then ? – krisFR Feb 03 '14 at 13:36
  • OK I get an error: I posted it at part of my answer... – DextrousDave Feb 03 '14 at 13:42
  • @DextrousDave from a Powershell console run `.\test.ps1` – krisFR Feb 03 '14 at 14:09
  • Yip, did that, and it says the following: "...the execution of scripts is disabled on this system". If that is the case, why can I run batch files? – DextrousDave Feb 03 '14 at 14:25
  • @DextrousDave Yes, this is a PowerShell security feature. Run `Set-ExecutionPolicy RemoteSigned` – krisFR Feb 03 '14 at 14:28
  • thank you, eventially got it to run, but the code does not work - i have a file with todays datetime stamp on it, and it did not copy is to the new folder. Did you test your code? – DextrousDave Feb 04 '14 at 08:41
  • @DextrousDave Yes i have tested it of course, and this kind of code runs on many of my live servers without any issues. I have updated my code (adding "echos") to help you debug. You can update your question with a small part of the script output to help us debug. – krisFR Feb 04 '14 at 23:40