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
Related
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.
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
)
Looking for a way to run a command, but insert the path/filenames of all mp4 files from the working directory (where the script is run) to the command.
vlc.exe c:\path\filename1.mp4 c:\path\filename2.mp4
I have the following code, but the "%%~A" is only inserting one path/filename at a time instead of adding every path filename from the folder.
set dir=C:\Users\Administrator\Desktop\1
for /f "delims=" %%A in ('dir /b "%dir%\*.*"') do ("C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "%%~A" --sout=#transcode{vcodec=mp2v,vb=800,acodec=mpga,ab=128,channels=2,samplerate=44100}:http{mux=ts,dst=:8080/abc} --sout-keep --loop)
An elementary solution: first try
set "dir=C:\Users\Administrator\Desktop\1"
dir /b /a:-d "%dir%\*.*"
The same result (filenames only) as in:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%%~A"
So add full path as follows:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%dir%\%%~A"
Eventually, replace #echo "%dir%\%%~A" with your command...
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%
I have this batch:
for /f %%a IN ('dir /b *.pdf') do call convert.exe %%a
This gets every pdf file thats in the same folder as convert.exe. I want to be able to say where the PDF resides. What do I have to change?
Thanks!
If the directory name can be hardcoded, then it will be
for /f %%a IN ('dir /b /s "Disk:\Your\Directory\Name\*.pdf"') do call convert.exe %%a
Note that this will also return all .pdf files in subdirectories of Disk:\Your\Directory\Name.