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%"
Find a substring (filepath) from a string containing variable order arguments
Batch file goto not working in for loop
How can you open a non-specific file using a program called through a batch file?
Batch file: sorting folders and files alphabetically
Run .bat script on server start up and keep console
Set output of a command as a variable (with pipes)
Batch sendemail automatically without clicking send
Batch File to restart the service after reading value
Reading From a Text File and Executing What You Find
How do I exclude specific file names from an MS DOS dir list?
A Batch Script that copies a file do a network computer then logs if it was successful
Batch to delete all files in a folder with a wildcard in the path
How do I replace a string variable within a string variable in my cmd batch file?
sqlcmd or .bat file to pull from file until blank line
relative path in BAT script
Use Dos commands to copy file and preserve date in file name