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

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

Categories

HOME
service-worker
phpword
microsoftgraph
rdbms
youtube-data-api-v3
apiblueprint
shiro
aws-cli
dojo
3d-reconstruction
regular-language
mapbox
data-visualization
webix
flume
vimdiff
why3
emoji
plink
scrolltop
gruntfile
powershell-v5.0
job-scheduling
dimensional-modeling
symfony-console
babel
libigl
pagespeed
inno-download-plugin
roslyn
mongoid6
jquery-callback
xcode-ui-testing
moses
custom-post-type
delimiter
subclass
uitapgesturerecognizer
finagle
google-cast
dst
deltaspike
angularjs-material
fstream
dsx-desktop
contacts
health-monitoring
kill
plaintext
watchface
logback-groovy
placeholder
android-checkbox
show-hide
apache-spark-dataset
double-click
bluez
zenhub
boxing
oracleclient
git-flow
type-theory
gpg-signature
trigger.io
reporting-services-2012
async.js
firebase-security
simevents
project-organization
redis-cluster
beanstalk
state-restoration
ess
text-search
sony-lifelog-api
lasso
vacuum
sql-server-data-tools
pyobjc
alloy-ui
snoop
mraid
shopizer
delayed-execution
boost-program-options
iiop
with-statement
lnk
ios8-extension
android-framework
maven-scm
square-bracket
away3d
twisted.web
intersect
tree-balancing
veracity
thttpd
jsr286
node-redis
screen-size
fsevents
xui
tmx
diagrams
eclipse-marketplace
sifr
dsl-tools
cuda.net
java1.4

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