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%"
Use environment variable set by a batch script in the next batch script run
Batch script to rename file with timestamp on FTP server after upload
Batch script to remove “duplicate” files
Break a folder with many files to multiple subfolders (Windows 10)
How to open an HTML file from a batch file in default browser with a specific destination anchor?
Q: IF EXIST/IF NOT EXIST in batch script
A command in a .bat file is unrecognized when the .bat file is called from an Inno Setup but works fine when I run the bat file manually
WinSCP automate file transfer- Should the batch and script file locate in virtual or local directory?
How to make a series of random letters and numbers with a certain character length in batch?
Batch file to update iTunes depending on version
Windows batch: Extract strings between brackets
How to “press a key” in a minimized window
How to use special characters in choice command - Batch File
Replace characters in filename through .bat
How to use a string inside “for” loop as variable name in a batch command
Batch file to copy list of files including subdirectories