Copy Latest revision - windows

I wat to make a bat file that will only copy the file with the heighest number.
File is like
xxxx,0.xml
xxxx,1.xml
xxxx,2.xml.
I only want to xxxx,2.xml. in this folders are more files like that.
Thank you very much for your help

sort files by name (exclude directories):
dir /b /a-d /on "xxxx,*.xml"
get the last of them:
for "tokens=*"/f %%i in ('dir /b /a-d /on "xxx,*.xml"') do set last=%%i
echo %last%

Related

Batch File delete newest file

How do you all delete the newest file in a folder using batch script?
All results I have found ONLY show how to delete the oldest files or delete them after N days.
Links are : Batch file to delete files older than N days and Batch Script to delete oldest folder in a given folder
Thank you
for /F %%a in ('dir /B /O-D /A-D %the_directory%') do echo("%the_directory%\%%a"&goto deldone
:deldone
would be my approach. It rudely escapes from the for loop having deleted the first filename encountered in a sorted-in-reverse-date-order (/o-d) list.
This saves having to read the entire list. Not such a problem with a few files, but can be a pain with thousands.
The required DEL command is merely ECHOed for testing purposes. After you've verified that the command is correct, change ECHO(DEL to DEL to actually delete the files.
I would do this:
#echo off
set latest=
set the_directory=your_directory
for /F %%a in ('dir /B /OD /A-D %the_directory%') do set latest=%%a
del %the_directory%\%latest%
run dir on files only, with sorting on modification date. Then loop and keep last echoed file. Delete it (replace by echo to test!)
The following code snippet does what you want:
set "FILE="
pushd "\path\to\folder" || exit /B 1
for /F "eol=| delims=" %%F in ('
dir /B /A:-D /O:D /T:C "*.*"
') do (
set "FILE=%%F"
)
if defined FILE del "%FILE%"
popd
The dir command returns files only (/A:-D) sorted by date in ascending order (/O:D) using the creation date (/T:C), which are located in \path\to\folder and match the pattern *.*.
The for /F loop walks through all the files returned by dir and assigns its name to variable FILE, overwriting it in each iteration, hence the final value is the name of the newest file.
The del command finally deletes the found file. The if query covers the case when no files are found in the given location.
Here is a slightly modified variant as recommended by Magoo's comment, which might be a bit more performant than the aforementioned one:
set "FILE="
pushd "\path\to\folder" || exit /B 1
for /F "eol=| delims=" %%F in ('
dir /B /A:-D /O:-D /T:C "*.*"
') do (
if not defined FILE set "FILE=%%F"
)
if defined FILE del "%FILE%"
popd
This avoids multiplicately overwriting of variable FILE by querying whether it has already been defined. Note the reversed sort order of dir here.
However, the approach shown in Magoo's answer is still better in performance.

Display directory structure using relative paths

I want to generate a file that contains the directory structure of a given directory, as relative paths. Currently I have the following batch file:
#echo off
pushd "C:\TEST"
dir /AD /B /ON /S
popd
Its current output is this:
C:\TEST\one
C:\TEST\three
C:\TEST\two
C:\TEST\one\a
C:\TEST\three\d
C:\TEST\three\e
C:\TEST\three\f
C:\TEST\two\b
C:\TEST\two\c
I want the output to be:
one
three
two
one\a
three\d
three\e
three\f
two\b
two\c
EDIT: this question is not a duplicate. The other question shows files exclusively, now I need to get directories exclusively (not mixed with files).
#echo OFF
SETLOCAL enabledelayedexpansion
SET "targetdir=c:\106x"
pushd "%targetdir%"
FOR /f "delims=" %%a IN ('dir /AD /B /ON /S') DO (SET "dirname=%%a"&ECHO(!dirname:%targetdir%\=!)
popd
GOTO :EOF
This should work for you provided the directoryname does not contain !
The directorynames are applied to %%a and transferred to dirname for manipulation. The target directoryname + a closing \ are then replaced by nothing for display.

BATCH - Find most recent file and concatenate it into another file

I have a directory with a list of files, I would like that my batch finds the most recently added file and then copies the content in another file in another directory. So far I did like this:
for /f "delims=" %%x in ('dir /od /a-d /b c:\dir1\*.log') do type %%X >> c\dir2\all.log
The problem is that in this way it outputs for example
ex150422.log 1>>c:\dir2.log
but it doesn't copy the content..
What am I doing wrong?
Thanks!
are you shure it can find the file? add the directory name like you did in dir-command
for /f "delims=" %%x in ('dir /od /a-d /b c:\dir1\*.log') do (
type c:\dir1\%%x>>c:\dir2\all.log
)

copy the recently modified file from source to destination in Batch script

How to copy the recently modified file from source to destination.
Constraint: All the files starting with same name in the source folder.
Example: Source - C:\Source and is having files like sys1239_5241.KNL, sys1234_8741.KNL
So how to copy latest modified files in batch file and modification will happen in minutes not in dates.
for /f "delims=" %%i in ('dir /b /a-d /od "C:\Source\*.KNL"') do set "LatestModifiedFile=%%~i"
echo copy "C:\Source\%LatestModifiedFile%" "X:\destination\path"
..remove echo if it looks good.
The following works well.
for /f "delims=" %%i in ('dir /b /a-d /od "C:\Teste_1\*"') do set "LatestModifiedFile=%%~i"
copy "C:\Teste_1\%LatestModifiedFile%" "C:\Teste_2\"
pause

List only files from subfolders of specified folder

This question might have a simple solution, but I just cannot find it.
Let's say I'm using the DIR command to produce a list of .txt files in the folder "E:\Documents". How can I make sure that only the .txt files one level below "E:\Documents" are listed (i.e. in a direct subfolder) and not the files in "E:\Documents" itself. "E:\Documents\\" or "E:\Documents\*\" doesn't seem to do the trick.
Thanks in advance!
FOR /f "delims=" %%i IN ('dir /b/ad') DO IF EXIST ".\%%i\*.txt" DIR ".\%%i\*.txt"
That's if you want the output in DIR format - with headers
FOR /f "delims=" %%i IN ('dir /b /ad') DO IF EXIST ".\%%i\*.txt" (
FOR /f "delims=" %%q IN ('DIR /b ".\%%i\*.txt" ') DO ECHO ".\%%i\%%q"
)
if you want just the filenames.
This can be achieved without dir:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~ff
If you need not only the file names, but also detail information about the files, add the respective qualifiers, e.g.:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~azff
a → attributes of the file
t → timestamp of the file
z → size of the file
See help for for details.

Resources