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

Find specific file's full path with for loop
How to make “type” not to echo file names when joining multiple files in Windows CMD
IF statement breaks batch script
Add to a file name
Writing a BATCH File IF EXIST not working
Reading Data from a text file using Batch
In a batch file ran from Windows, select latest application version using directory name
How to get string from each line between specified characters?
Removing a network printer via login script
Batch file left string trim not working
bat-script: Open a program and execute a command in that program
Getting a permission denied error when running _zipit.vbs from a batch file
Layout of command output executed automatically in PuTTY from a batch file is broken
Batch script executed from AutoIt causes infinite loop
comparing reg keys with old backup CMD line
BIRT runtime 4.6.0 batch file not running

Categories

HOME
google-app-engine
awk
amp-html
gradient
google-apps
mips
telegram-bot
android-json
google-admin-sdk
crystal-reports-2013
opengl-3
augeas
pcl-crypto
dkim
siddhi
java.util.logging
jsonpath
spring-social
intercom
pybossa
keytool
easyphp
sse
nodemcu
charles
symbols
ag
hta
gmock
definitelytyped
asymptotic-complexity
mat
underscore.js
handler
spotipy
quantlib
errbot
openmdao
nsurl
alm
livelink
weather
turfjs
jwplayer6
css-position
chef-solo
setup.py
pom.xml
dynamic-linq
lampp
ngmaterial
google-photos
filenet
slackware
simplepie
pytables
hadoop-2.7.2
tooleap
cortex-m
fink
false-sharing
sciruby
dock
toolkit
sapscript
slash
typemock
entity-framework-4.3
jquery-forms-plugin
xmltype
android-cookiemanager
boost-program-options
ghcjs
universal-image-loader
raygun
unix-socket
personalization
alternate
dct
mutual-authentication
android-framework
synapse
mt4j
sphinxql
script#
actiondispatch
fb.ui
tree-balancing
fragmenttransaction
dajaxice
isa-swizzling
expression-blend-4
automationelement
timthumb
quotation-marks
svk
commerceserver2007
duplex
fdf
unmanagedresources
leader

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