I want to create a .bat script to copy only one random file from each folder (also subfolders, so recursively) whilst also keeping the folder structure. I've tried the following code which comes close to what I want but doesn't copy the folder structure and one file per folder.
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
SET Destination=H:\Temp
SET FileFilter=.ape
SET SubDirectories=/S
SET Source=%~dp1
SET FileList1Name=FileList1.%RANDOM%.txt
SET FileList1="%TEMP%\%FileList1Name%"
SET FileList2="%TEMP%\FileList2.%RANDOM%.txt"
ECHO Source: %Source%
IF /I {%SubDirectories%}=={/S} ECHO + Sub-Directories
IF NOT {"%FileFilter%"}=={""} ECHO File Filter: %FileFilter%
ECHO.
ECHO Destination: %Destination%
ECHO.
ECHO.
ECHO Building file list...
CD /D "%Source%"
DIR %FileFilter% /A:-D-H-S /B %SubDirectories% > %FileList1%
FOR /F "tokens=1,2,3 delims=:" %%A IN ('FIND /C ":" %FileList1%') DO SET TotalFiles=%%C
SET TotalFiles=%TotalFiles:~1%
ECHO The source has %TotalFiles% total files.
ECHO Enter the number of random files to copy to the destination.
SET /P FilesToCopy=
ECHO.
IF /I %TotalFiles% LSS %FilesToCopy% SET %FilesToCopy%=%TotalFiles%
SET Destination="%Destination%"
IF NOT EXIST %Destination% MKDIR %Destination%
SET ProgressTitle=Copying Random Files...
FOR /L %%A IN (1,1,%FilesToCopy%) DO (
TITLE %ProgressTitle% %%A / %FilesToCopy%
REM Pick a random file.
SET /A RandomLine=!RANDOM! %% !TotalFiles!
REM Go to the random file's line.
SET Line=0
FOR /F "usebackq tokens=*" %%F IN (%FileList1%) DO (
IF !Line!==!RandomLine! (
REM Found the line. Copy the file to the destination.
XCOPY /V /Y "%%F" %Destination%
) ELSE (
REM Not the random file, build the new list without this file included.
ECHO %%F>> %FileList2%
)
SET /A Line=!Line! + 1
)
SET /A TotalFiles=!TotalFiles! - 1
REM Update the master file list with the new list without the last file.
DEL /F /Q %FileList1%
RENAME %FileList2% %FileList1Name%
)
IF EXIST %FileList1% DEL /F /Q %FileList1%
IF EXIST %FileList2% DEL /F /Q %FileList2%
ENDLOCAL
The destination should be set in the .bat code like the code above. Can anybody please help me with this? Thanks in advance!
Copying a directory tree structure (folders only) is trivial with XCOPY.
Selecting a random file from a given folder is not too difficult. First you need the count of files, using DIR /B to list them and FIND /C to count them. Then use the modulo operator to select a random number in the range. Finally use DIR /B to list them again, FINDSTR /N to number them, and another FINDSTR to select the Nth file.
Perhaps the trickiest bit is dealing with relative paths. FOR /R can walk a directory tree, but it provides a full absolute path, which is great for the source, but doesn't do any good when trying to specify the destination.
There are a few things you could do. You can get the string length of the root source path, and then use substring operations to derive the relative path. See How do you get the string length in a batch file? for methods to compute string length.
Another option is to use FORFILES to walk the source tree and get relative paths directly, but it is extremely slow.
But perhaps the simplest solution is to map unused drive letters to the root of your source and destination folders. This enables you to use the absolute paths directly (after removing the drive letter). This is the option I chose. The only negative aspect of this solution is you must know two unused drive letters for your system, so the script cannot be simply copied from one system to another. I suppose you could programatically
discover unused drive letters, but I didn't bother.
Note: It is critical that the source tree does not contain the destination
#echo off
setlocal
:: Define source and destination
set "source=c:\mySource"
set "destination=c:\test2\myDestination"
:: Replicate empty directory structure
xcopy /s /t /e /i "%source%" "%destination%"
:: Map unused drive letters to source and destination. Change letters as needed
subst y: "%source%"
subst z: "%destination%"
:: Walk the source tree, calling :processFolder for each directory.
for /r y:\ %%D in (.) do call :processFolder "%%~fD"
:: Cleanup and exit
subst y: /d
subst z: /d
exit /b
:processFolder
:: Count the files
for /f %%N in ('dir /a-d /b %1 2^>nul^|find /c /v ""') do set "cnt=%%N"
:: Nothing to do if folder is empty
if %cnt% equ 0 exit /b
:: Select a random number within the range
set /a N=%random% %% cnt + 1
:: copy the Nth file
for /f "delims=: tokens=2" %%F in (
'dir /a-d /b %1^|findstr /n .^|findstr "^%N%:"'
) do copy "%%D\%%F" "z:%%~pnxD" >nul
exit /b
EDIT
I fixed an obscure bug in the above code. The original COPY line read as follows:
copy "%%~1\%%F" "z:%%~pnx1" >nul
That version fails if any of the folders within the source tree contain %D or %F in their name. This type of problem always exists within a FOR loop if you expand a variable with %var% or expand a :subroutine parameter with %1.
The problem is easily fixed by using %%D instead of %1. It is counter-intuitive, but FOR variables are global in scope as long as any FOR loop is currently active. The %%D is inaccessible throughout most of the :processFolder routine, but it is available within the FOR loops.
The "natural" way to process a directory tree is via a recursive subroutine; this method minimize the problems inherent to this process. As I said at this post: "You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory". I taken the code at this answer, that duplicate a tree, and slightly modified it in order to solve this problem.
#echo off
setlocal
set "Destination=H:\Temp"
set "FileFilter=*.ape"
rem Enter to source folder and process it
cd /D "%~dp1"
call :processFolder
goto :EOF
:processFolder
setlocal EnableDelayedExpansion
rem For each folder in this level
for /D %%a in (*) do (
rem Enter into it, process it and go back to original
cd "%%a"
set "Destination=%Destination%\%%a"
if not exist "!Destination!" md "!Destination!"
rem Get the files in this folder and copy a random one
set "n=0"
for %%b in (%FileFilter%) do (
set /A n+=1
set "file[!n!]=%%b"
)
if !n! gtr 0 (
set /A "rnd=!random! %% n + 1"
for %%i in (!rnd!) do copy "!file[%%i]!" "!Destination!"
)
call :processFolder
cd ..
)
exit /B
Here is anther approach using xcopy /L to walk through all files in the source directory, which does not actually copy anything due to /L but returns paths relative to the source directory. For explanation of the code see all the remarks:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define source and destination directories here:
set "SOURCE=%dp~1"
set "DESTIN=H:\Temp"
rem Change to source directory:
cd /D "%SOURCE%"
rem Reset index number:
set /A "INDEX=0"
rem Walk through output of `xcopy /L`, which returns
rem all files in source directory as relative paths;
rem `find` filters out the summary line; `echo` appends one more line
rem with invalid path, just to process the last item as well:
for /F "delims=" %%F in ('
2^> nul xcopy /L /S /I /Y "." "%TEMP%" ^
^| find ".\" ^
^& echo^(C:\^^^|\^^^|
') do (
rem Store path to parent directory of current item:
set "CURRPATH=%%~dpF"
setlocal EnableDelayedExpansion
if !INDEX! EQU 0 (
rem First item, so build empty directory tree:
xcopy /T /E /Y "." "%DESTIN%"
endlocal
rem Set index and first array element, holding
rem all files present in the current directory:
set /A "INDEX=1"
set "ITEMS_1=%%F"
) else if "!CURRPATH!"=="!PREVPATH!" (
rem Previous parent directory equals current one,
rem so increment index and store current file:
set /A "INDEX+=1"
for %%I in (!INDEX!) do (
endlocal
set /A "INDEX=%%I"
set "ITEMS_%%I=%%F"
)
) else (
rem Current parent directory is not the previous one,
rem so generate random number from 1 to recent index
rem to select a file in the previous parent directory,
rem perform copying task, then reset index and store
rem the parent directory of the current (next) item:
set /A "INDEX=!RANDOM!%%!INDEX!+1"
for %%I in (!INDEX!) do (
xcopy /Y "!ITEMS_%%I!" "%DESTIN%\!ITEMS_%%I!"
endlocal
set /A "INDEX=1"
set "ITEMS_1=%%F"
)
)
rem Store path to parent directory of previous item:
set "PREVPATH=%%~dpF"
)
endlocal
exit /B
For this approach the destination directory can also be located within the source directory tree.
Related
I have to copy the first 100 files with a specific file extension to another folder daily.
The source folder looks like this:
sourcefolder\ParentFolderA
├───folder1
│ └──────file_a.dat
├───folder2
│ └──────file_b.dat
└───folder3
└──────file_c.dat
I need to grab the newest 100 .dat files – there is only one .dat file in each folder UNDER the ParentFolderA – and copy them to a new folder.
This is what I have so far:
#echo off
setlocal enableextensions enabledelayedexpansion
set /a "index = 0"
set /a "count = 99"
set "source=sourcefolder\ParentFolderA\"
set "destination=destinationfolder\somerandomFoldername"
:whileloop
if %index% leq %count% (
echo %index%
for /R "%source%" %%f in (*.dat) do copy %%f "%destination%"
set /a "index = index + 1"
goto :whileloop
)
endlocal
timeout 10
Some of it will be taken out as I just have it in there to help me while I am writing it. The end behavior is to get those 100 newest .dat files. Currently this is grabbing ALL the .dat files in every subfolder, but it never terminates because it doesn´t have a chance to get to the incrementing portion of code. (There are thousands of folders it would have to go through before it got to that step).
Can someone give me some tips or an advice on where/how I can achieve the desired effect?
You need a list of all your files which is sortable by date/time. There isn't a command capable to do this recursive in cmd. But it's possible with a trick, I used in this answer: Temporarily set the date format to a sortable format, get the list and set it back to the original format:
#echo off
setlocal EnableDelayedExpansion
set /a "count=100"
set "source=sourcefolder\ParentFolderA\"
set "destination=destinationfolder\somerandomFoldername"
REM get current short date format:
for /f "tokens=2,*" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate') do set orig-format=%%b
REM set short format to yyyy.MM.dd:
reg add "HKCU\Control Panel\International" /v sShortDate /d "yyyy.MM.dd" /f >nul
REM get a recursive listing with format "YYYY.MM.DD hh:mm <full qualified file name>":
(for /F "delims=" %%a in ('dir /a-d /T:W /S /B "%source%\*"') do #echo %%~Ta "%%a")|sort /r>report.csv
REM set short date format back to original settings:
reg add "HKCU\Control Panel\International" /v sShortDate /d "%orig-format%" /f >nul
REM copy the first %count% files:
set n=0
for /f "tokens=2,*" %%a in (report.csv) do (
set /a n+=1
if !n! gtr %count% goto :done
ECHO copy %%b "%destination%\"
)
:done
echo done.
Adapt /T:W to your needs.
Note: I "disarmed" the copy command for security reasons. When it works as you want it to, just remove the ECHO.
(sorry to quote the filename within the for loop. I know it's bad habit, but it
is necessary because sort manages to add a space to the end of each line.)
#ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
:: I used 20 files for my test
SET /a maxfiles=20
:: I chose to process all the .T* files
FOR /r "%sourcedir%" %%b IN (*.t*) DO COPY "%%b" "%destdir%\" >NUL
FOR /f "skip=%maxfiles%delims=" %%b IN ('DIR /b /a-d /o-d "%destdir%\*.t*" ') DO ECHO DEL "%destdir%\%%b"
GOTO :EOF
Since you seem to have no objections to building a complete copy of your target files, all that is needed is to delete all of the files from a directory listing in reverse-date order, skipping the required number of retentions.
The echo del is present for verification, to make sure the directory is correct. Change echo del to del to execute the deletions.
I would like to append recursively folder (and parent folder) names to each *.txt files that folder contains. After that I want to move all files to base folder and delete all folders. I need to achieve this in Windows BATCH script. For examle:
\BaseFolder\A01\B01\EX1.TXT
\BaseFolder\C01\EX2.TXT
\BaseFolder\EX3.TXT
To:
\BaseFolder\A01-B01-EX1.TXT
\BaseFolder\C01-EX2.TXT
\BaseFolder\EX3.TXT
To do this i've found this solution thanks to JosefZ:
Recursively append folder name to the files in Windows batch file
#echo OFF
SETLOCAL EnableExtensions
for /F "delims=" %%G in ('dir /B /S "C:\Source\*.txt"') do (
for %%g in ("%%~dpG.") do rename "%%~fG" "%%~nxg_%%~nxG"
)
pause
where the FOR loops are:
outer %%G loop creates a static list of .txt files (recursively), and
inner %%g loop gets the parent folder of every particular file.
But this solve only a part of my goal. Can anyone help?
Here's a 'fun' idea:
#Set "BaseFolder=C:\Users\Mustafa\BaseFolder"
#ForFiles /P "%BaseFolder%" /S /M *.txt /C "Cmd /C If #IsDir==FALSE For /F 0x22Tokens=*Delims=.\0x22 %%# In (#RelPath)Do #If Not 0x22%%#0x22==#File Set 0x22_=%%#0x22&Call Move /Y 0x22%BaseFolder%\%%#0x22 0x22%BaseFolder%\%%_:\=-%%0x22>NUL"
Please note that this untested solution is very likely to have command line length limitations. I would therefore avoid it if your initial base folder is deep within the volume and/or its tree is deep or carries long file and directory names. Given that cautionary information, please remember to adjust the full path value on line one as necessary.
The following script should accomplish what you want, namely moving and renaming the files as predefined and deleting the remaining empty sub-directories (see all the explanatory rem remarks):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=.\BaseFolder" & rem // (target base directory)
set "_MASK=*.txt" & rem // (file search pattern)
set "_CHAR=-" & rem // (separator character)
rem // Switch to target directory:
pushd "%_ROOT%" && (
rem // Loop through list of relative paths of matching files:
for /F "tokens=1* delims=:" %%E in ('
xcopy /L /S /I "%_MASK%" "%TEMP%" ^| find ":"
') do (
rem // Store current relative file path, initialise variables:
set "FILE=%%F" & set "NAME=" & set /A "NUM=0"
rem // Toggle delayed expansion to avoid trouble with `!` and `^`:
setlocal EnableDelayedExpansion
rem // Loop through all individual elements of file relative path:
for %%I in ("!FILE:\=" "!") do (
endlocal
rem // Store current path element and count them:
set "ITEM=%%~I" & set /A "NUM+=1"
setlocal EnableDelayedExpansion
rem // Build new file name and pass it over `endlocal` barrier:
for /F "delims=" %%N in ("!NAME!%_CHAR%!ITEM!") do (
endlocal
set "NAME=%%N"
setlocal EnableDelayedExpansion
)
)
rem // Finalise new file name:
if defined _CHAR set "NAME=!NAME:*%_CHAR%=!"
rem // Actually move and rename the current file:
> nul move "!FILE!" "!NAME!"
rem // Switch to parent directory of current file:
pushd "!FILE!\.." && (
rem // Loop through parent directory elements:
for /L %%N in (2,1,!NUM!) do (
rem // Try to remove parent directory when empty, go one up:
set "DD=!CD!" & cd ".." & 2> nul rd "!DD!"
)
rem // Return to previous working directory:
popd
)
endlocal
)
rem // return to original working directory:
popd
)
endlocal
exit /B
i'm looking for a way on Windows to find all files with a specific file extension length and copy them to another location while preserving the folder structure.
For example lets say that I want to copy all files on my D: drive, with a file extension length of excatly six (*.******) and copy them to another location while also keeping the folder structure.
Is this possible in CMD?.
C:\Users\PeterR>copy C:\Users\PeterR\Documents\cmd\???.txt C:\Users\PeterR\Documents\cmd1\
--use ? to specify the lentgh. for six characters it's ??????.extension
What about the following code snippet:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_SOURCE=D:\"
set "_DESTIN=E:\"
pushd "%_SOURCE%" || exit /B 1
for /F "delims=" %%F in ('
xcopy /L /E /I ".\*.*" "E:\" ^| find ".\"
') do (
for /R "D:\" %%F in ("*.*") do (
set "EXT=%%~xF"
setlocal EnableDelayedExpansion
if "!EXT:~6!"=="" if not "!EXT!"=="!EXT:~5!" (
endlocal
move /Y "%%~F" "%_DESTIN%\%%~F"
) else endlocal
)
popd
endlocal
exit /B
I have a folder named G:\Project\Projects
This folder has some(about 20) unknown subfolders. Some of the subfolders have a folder named SSS and a file called deleteme.doc
I want to delete all these SSS folders(with contents) and deleteme.doc files using a batch file or powershell file. I do not want to delete these files or folder deeply nested into the subfolders, just those who are directly under subfolder.
For example I want to delete this folder
G:\Project\Projects\proj 1\sss
but not these 3-
G:\Project\Projects\proj 1\other\sss
G:\Project\Projects\sss
G:\Project\Projects\proj 1\sss (it is a file)
The thing is I want to simply double click-run the batch/powershell file from anywhere, possibly from another partition or drive (or even network computer).
I tried RMDIR G:\Project\Projects\*\SSS it didn't seem to be working.
Please explain your answer if possible, also show me commands for both recycle bin delete and parma delete. Though I think sending to recycle bin is not possible without powershell.
Administrator privilege is not necessary to delete folders in non-system partition right?
Windows 10. powershell version 5
#ECHO Off
SETLOCAL
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
:: set defaults
SET "$dirname=sss"
SET "$filename=deleteme.doc"
SET "$from=c:\wherever\you\like"
:: analyse command line
:: syntax file="filename" " dir=dirname" from="dirname" (/switches)
:: each is optional. default is as established above
:: /d - delete directory only if it is completely empty
:: /dn - delete directory only if it is not completely empty
:: /ds - delete directory only if it is completely empty or if no files in its subdirectory tree
:: /fe - delete file if empty
:: /fn - delete file if not empty
:: /b - both file and directory must exist to make deletions
::
FOR %%a IN (%*) DO IF DEFINED $twopart (CALL :parttwo %%a) ELSE (
SET "$processed="
IF /i "%%a"=="dir" SET "$processed=Y"&SET "$twopart=$dirname"
IF /i "%%a"=="file" SET "$processed=Y"&SET "$twopart=$filename"
IF /i "%%a"=="from" SET "$processed=Y"&SET "$twopart=$from"
IF /i "%%a"=="/b" CALL :setswitch b
IF /i "%%a"=="/d" CALL :setswitch d
IF /i "%%a"=="/dn" CALL :setswitch dn
IF /i "%%a"=="/ds" CALL :setswitch ds
IF /i "%%a"=="/fe" CALL :setswitch fe
IF /i "%%a"=="/fn" CALL :setswitch fn
IF NOT DEFINED $processed CALL :extras %%a
)
IF DEFINED $extras ECHO(%$extras% NOT understood)&pause&GOTO :EOF
:: resolve switch compatibility
IF DEFINED $d IF DEFINED $dn SET "$incompatible=/d /dn"
IF DEFINED $fe IF DEFINED $fn SET "$incompatible=%$incompatible% /f /fn"
IF DEFINED $incompatible ECHO(%$incompatible% incompatible)&pause&GOTO :EOF
:: if $from is set, make it quoted.
IF DEFINED $from SET "$from="%$from:"=%""
:: Now search for the directory
FOR /d /r %$from% %%a IN ("%$dirname%") DO IF EXIST "%%a\." (
SET "$victim=%%a"
CALL :deldir
)
IF NOT DEFINED $b FOR /r %$from% %%a IN ("%$filename%") DO IF EXIST "%%a" (
SET "$victim=%%~dpa%$filename%"
CALL :delfile
)
GOTO :eof
:: delete the victim directory if all other conditions are met
:: take care of file as well if required.
:deldir
SET "$victim=%$victim:"=%"
IF DEFINED $b IF NOT EXIST "%$victim%\..\%$filename%" GOTO :eof
:: if the directory has any contents and "/d" was specified, skip deletion
IF DEFINED $d (
FOR /f %%z IN ('dir /b "%$victim%" 2^>nul') DO GOTO :eof
)
:: if the directory has files in its subtree and "/ds" was specified, skip deletion
IF DEFINED $ds (
FOR /f %%z IN ('dir /s /b /a-d "%$victim%" 2^>nul') DO GOTO :eof
)
:: if the directory has no files and none in its subtree and "/dn" was specified, don't skip deletion
IF DEFINED $dn (
FOR /f %%z IN ('dir /b "%$victim%" 2^>nul') DO GOTO deldir1
GOTO :eof
)
:: delete the directory
:deldir1
ECHO(DEL /s /f /q "%$victim%"
SET "$victim=%$victim%\..\%$filename%"
IF EXIST "%$victim%" GOTO delfile
GOTO :eof
:delfile
FOR %%z IN ("%$victim%") DO (
IF DEFINED $fn IF "%%~zz"=="0" GOTO :EOF
IF DEFINED $fe IF "%%~zz" neq "0" GOTO :EOF
)
ECHO(DEL /f /q "%$victim%"
GOTO :eof
:parttwo
SET "%$twopart%=%~1"
SET "$twopart="
GOTO :eof
:extras
SET "$extras=%$extras% %~1"
GOTO :eof
:setswitch
SET "$processed=Y"
SET "$%1=Y"
SHIFT
IF "%1" neq "" GOTO setswitch
GOTO :EOF
Here's a general solution which you'd need to test before unleashing.
The required RD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(RD to RD to actually delete the directories.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL to DEL to actually delete the files.
The first section analyses the parameters ans switches supplied to the program.
The defaults are shown and need to be adjusted to suit your circumstances.
The command may be run as
*thisname* from=c:\new\location dir=xxx file=hello.txt /d
where from specifies where the tree-walk begins, fie the filename to be located and dir the directoryname to delete.
The switches are also listed.
The program simply examines the command line for elements of interest and sets values as required.
We then traverse the tree, looking for the directoryname and then make decisions depending on the switch-settings.
Then traverse the tree again, looking for the filename.
Something like
for /f %A in ('dir "c:\somewhere\deleteme.doc"') Do echo rd "%~dpA"
Administrator privilege is not necessary to delete folders in non-system partition right?
How would we know what permissions you have. Type icacls c:\somewhere to see.
You need to know your problem.
I created a short powershell solution using this-
$targetName='ss s';
foreach ($file in Get-Childitem "G:\Project\Projects\" )
{
$fname=$file.name;
if (Test-Path "G:\Project\Projects\$fname\$targetName\")
{
$shell = new-object -comobject "Shell.Application"
$item = $shell.Namespace(0).ParseName("G:\Project\Projects\$fname\$targetName")
$item.InvokeVerb("delete")
}
}
This powershell script sends that folder to recycle bin after confirmation popup. (this won't send any file named 'ss s')
this seems to be working for batch file script-
set "b=ss s"
for /d %%a in (*) do IF EXIST "%%a\%b%\*" (rmdir /s "%%a\%b%")
if the targetfolder was named "$ss s" then you have to set variables as "b=/$ss s"
I would like to keep the X latest files from a folder and delete the rest. Is this possible with FORFILES? If it's not I can fallback to another solution I seen here. Thanks for help.
I did this but it takes by dates: EDIT
forfiles /p [path] /s /d -5 /c "cmd /c echo #file"
(echo file for testing purpose)
#ECHO OFF
SETLOCAL
SET "targetdir=U:\destdir"
SET /a retain=10
FOR /f "skip=%retain%delims=" %%a IN (
'dir /b /a-d /o-d "%targetdir%\*" '
) DO ECHO (DEL "%targetdir%\%%a"
GOTO :EOF
You would need to change the setting of targetdir to suit your circumstances. Equally, this procedure targets all files - change the filemask to suit.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL to DEL to actually delete the files.
method is to simply execute a dir in basic form without directories, sorted in reverse-date order.
Skip the first 10 entries, and delete the rest.
With forfiles I see no chance to accomplish your task of returning the newest (most recent) number of files.
So my idea for this approach is this:
to use dir /B /A:-D /T:C /O:-D to retrieve a bare list (/B) of files (no directories, /A:-D), sorted by creation date (/T:C; if you want to use the last modification date, simply remove the /T:C portion) in decending order (/O:-D), meaning newest items first;
to put over a for /F "eol=| delims=" loop to gather and parse the dir output line by line, meaning file by file, not excluding file names beginning with ; (eol=|, | is illegal for file names) and not splitting file names containing white-spaces like SPACE or TAB (delims=);
to establish a variable that constitutes a counter, incremented per each loop iteration;
to place an if condition inside of the loop to check if the counter reached the desired limit number and in case it is fulfilled, to break the for /F loop by goto;
Here is the related code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define global constants here:
set "TARGETPATH=\path\to\files\*.*"
set /A "TOTAL=10"
set /A "COUNT=0"
for /F "eol=| delims=" %%F in ('
dir /B /A:-D /T:C /O:-D "%TARGETPATH%"
') do (
echo(%%F
set /A COUNT+=1
setlocal EnableDelayedExpansion
if !COUNT! GEQ %TOTAL% (
endlocal
goto :NEXT
) else (
endlocal
)
)
:NEXT
endlocal
exit /B
I toggled the delayed variable expansion within the for /F loop to avoid trouble in case file names contain exclamation marks !, which would get lost in the line echo(%%F in case it is on.
Update
The following code accomplishes the original task of your question, namely to delete files in a given directory but to keep the most recent number of files:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define global constants here:
set "TARGETPATH=\path\to\files\*.*"
set /A "TOTAL=10"
set "SKIPOPT=" & if %TOTAL% GTR 0 set "SKIPOPT=skip=%TOTAL% "
for /F "%SKIPOPT%eol=| delims=" %%F in ('
dir /B /A:-D /T:C /O:-D "%TARGETPATH%"
') do (
del /P "%%F"
)
endlocal
exit /B
Since for /F supports a skip= to skip the given number of lines, and so files in our situation, let us make use of it. It is given indirectly here via variable SKIPOPT, which holds the entire option string like skip=10 (given that TOTAL is set to 10). The if %TOTAL% GTR 0 query is implemented for the script not to fail in case TOTAL is 0, because for /F does not accept the option skip=0.
The /P switch at the del command lets appear a prompt Delete (Y/N)? for testing purposes. If you do not want any prompts, simply remove it.