0

Using this as am example For statement in batch script from SO

tried the following

for %%f in (c:\username\desktop\Test\*.log) do (
    echo %%~nf
)

I get the following error: %%f was unexpected at this time

My goal is to move [n] files from one dir to another, wait [x] seconds and then move the next [n] files

I can do the time wait by doing a ping for an unknown site and force the delay or use timeout [not yet tested as this would need to be done without any user intervention]

If there is a better way to do this, i am open as this is my first dab at this type of scripting.

Community
  • 1
  • 1
  • 1
    Your problem "%%f was unexpected..." is caused by attempting to execute a batch command directly from the prompt.You need to save the text in a file named somethingorother.bat - the *.bat* being the key. You then execute *somethingorother.bat* by entering *somethingorother* at the prompt; the **.bat** file is then selected and run. Certainly easier than perpetually re-typing the command line. If you expressly want to run directly from the prompt, reduce any `%%x` in a *metavariable* (the loop-control variable) to `%x` – Magoo Apr 29 '14 at 03:52

1 Answers1

1

Try this :

@echo off
setlocal EnableDelayedExpansion

::The number of files to copy
set [n]=10
::The time to wait after copying [n] files
set $tempo=5
::Your source path
set $sourceDir="C:\your\source\path"
::Your destination path
set $DestDir="C:\your\destination\path"

set $c=1
for /f "delims=" %%f in ('dir /b/a-d %$SourceDir% *.log') do (
    if !$c! equ %[n]% (
        timeout %$tempo%
        set $c=1
    )
    copy "%$SourceDir:~1,-1%\%%~nxf" %$Destdir%
    set /a $c+=1
)
echo Done...

Assuming that the extension to test is .log. If not you have to change it.

unclemeat
  • 5,029
  • 5
  • 28
  • 52
SachaDee
  • 9,245
  • 3
  • 23
  • 33