Help with batch script

March 30, 2010 at 05:49:10
Specs: Windows XP
Hello,

I'm trying to copy the latest 10 files from one folder to other using a batch script. I'm new to batch scripting and having trouble doing this.

My requirement is to look for the latest 10 files (I want to ignore the folders, just the files) in folder A and copy it to folder B.

Any help on this would highly be appreciated. Thanks in advance..!


See More: Help with batch script

Report •


#1
March 30, 2010 at 08:25:00
Set FolderA and FolderB with your source and target folders without double quotes and the script is ready to run.

@echo off & setlocal EnableDelayedExpansion

set FolderA=Here your Source Folder
set FolderB=Here your Target Folder

echo.  Copying from "%FolderA%" to "%FolderB%"...
set cnt=0
pushd %FolderA%
for /F "delims=" %%j in ('dir /B /O-D /A-D') do (
  copy "%%j" "%FolderB%" > nul
  echo.  "%%j"
  set /A cnt+=1
  if !cnt! equ 10 goto :DONE
)
:DONE
popd
:: End_Of_Batch


Report •

#2
March 31, 2010 at 05:26:19
Ivo, Thanks for the reply,that was fast. I tried the script, I got like 1000 files in the source folder and the script copies all of them.

I don't understand why it doesn't stop when cnt reaches 10. Please advice.

Thanks again..!


Report •

#3
March 31, 2010 at 06:46:42
Sorry, I caused your script malfunction as suddenly after posting the code I edited one line due to a mistake and you probably just copied and pasted the wrong version. Here the correct one.

@echo off & setlocal EnableDelayedExpansion

set FolderA=Here your Source Folder
set FolderB=Here your Target Folder

echo.& echo.  Copying from "%FolderA%" to "%FolderB%"...
set cnt=0
pushd %FolderA%
for /F "delims=" %%j in ('dir /B /O-D /A-D') do (
  copy "%%j" "%FolderB%" > nul
  echo.  "%%j"
  set /A cnt+=1
  if !cnt! equ 10 goto :DONE
)
:DONE
popd
:: End_Of_Batch


Report •

Related Solutions

#4
March 31, 2010 at 08:01:44
That worked like a champ..! Thanks again..!

Report •


Ask Question