batch-file


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%"

Related Links

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

Categories

HOME
url-redirection
cygwin
azure-stream-analytics
porting
time-complexity
apache-flink
css3
ns-3
swt
jsf-2.2
sublimetext
alarm
xbee
jetty
crystal-reports-2013
joomla3.0
syntaxnet
mql5
32bit-64bit
commonjs
opengl-es-3.0
pybossa
enterprise-architect
event-sourcing
glyphicons
job-scheduling
dimensional-modeling
libigl
xunit
beautifier
has-and-belongs-to-many
fractions
comparable
finagle
gitlab-api
postback
database-comparison
card.io
keras-layer
slacktextviewcontroller
navigator
docfx
watchman
flatmap
tasm
trigonometry
avcapturedevice
berkeley-db-je
memory-mapping
opennms
aquamacs
qtranslate
yii2-user
colorbar
d3-force-directed
cd-burning
baidu
sandcastle
donations
custom-lists
xcode-server
vulcanize
beamer
rspec2
respondcms
qiime
lua-telegram-bot
grass
exim4
grgit
phonertc
finalbuilder
linked-tables
alloy-ui
diff3
nsmenu
gii
mobility
verold
buffering
xmltodict
merb
jquery-1.9
wp7test
server-administration
sqlclr
patricia-trie
pyunit
paginator
extconf.rb
pinch
role
automationelement
creole
uipagecontrol
nhprof
luajava
silent
interprocess
pagemethods
alsb
entitykey
httpcookie
lemmatization
xslcompiledtransform
mathematical-typesetting

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App