batch file to copy newest file, then delete the original file
I'm trying to create a batch file that finds the newest file in a directory, copies it to a new directory, then deletes the original. Everything works except for the delete portion. Here's what I have. Any help would be greatly appreciated. #echo off set source="C:\BLEND2\Results\LCBlend" set target="z:\Blenders" set NewestFile= FOR /F "delims=" %%I IN ('DIR %source%\*.* /A:-D /O:-D /B') DO set NewestFile=%%I COPY %source%\"%NewestFile%" %target% del %source%\%NewestFile%
Your DO needs to be on the same line as your closing parenthesis, also your code will retrieve the oldest file not the newest. Try this altered version which uses Move instead of Copy followed by Delete: #Echo Off Set "source=C:\BLEND2\Results\LCBlend" Set "target=Z:\Blenders" For /F "Delims=" %%A In ('Dir/B/A-D/OD "%source%"') Do Set "NewestFile=%%A" Move "%source%\%NewestFile%" "%target%"
merge lines from several files
detect two files in a folder in a batch file
dos batch file find missing sequential filenames
How to strip first 3 characters off filename in batch file
Can someone please explain this CMD batch file?
How to do Winrar in a batch file
DOS Batch Script - Renaming directories to the date of their most recent file
Why does calling a nested batch file without prepending “call” to the line exit the parent batch file?
Removing lines from a txt file in batch
Batch - How do I read the output of another command line function in a script?
Opening a URL with POST Parameters
How to store output of command in variable in cmd?
Batch Addition From File (+1) Rewrite
Batch Script: Column of output from program to variable?
How to solve this error in code of batch programming?
Mapping a network drive only when connected to a Domain