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

Categories

HOME
jmeter
pda
sh
sublimetext
nuget
entity-framework-core
cq5
vsm
title
joomla3.0
arangodb
syntaxnet
android-securityexception
zerobrane
selectize.js
sse
jquery-ias
roslyn
jquery-callback
expressionengine3
jasmine-node
mat
winexe
twiml
kudu
catch-unit-test
autolisp
spring-security4
code-snippets
github3.py
slacktextviewcontroller
processwire
android-softkeyboard
lpsolve
show-hide
apache-spark-dataset
modelsim
cup
monkey
stax
actor-platform
gogs
observablecollection
nservicebus5
phpwebsocket
themoviedb-api
fiware-monitoring
zen-cart
rubaxa-sortable
visual-sourcesafe-2005
eoferror
kubuntu
entity-framework-4.3
viewer
psr-4
ignite-ui
wxformbuilder
spring-remoting
authlogic
sbcl
android-audiorecord
jammer
mbunit
observium
mvc-editor-templates
dolby-audio-api
windows-98
robotlegs
saleslogix
thttpd
oracle-enterprise-linux
failing-tests
apache-commons-dbutils
uipagecontrol
xui
addchild
scrollbars
data-mapping
glui
feasibility
fault-tolerance
fdf
sqlobject
ifilter

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