Alvin Movida
Alvin Movida

Reputation: 11

batch file to copy the Most Recent File created

I'm new to scripting and I've this script over the internet, can anyone please explain exactly how the following code works, line by line?

@echo off

set "source=C:\temp"
set "dest=c:\paste"

pushd "%source%" ||(
   echo.Source does not exist&pause&goto EOF)

for /f "tokens=*" %%f in (
   'dir /A-D /OD /B') Do set "file=%%f"

popd

xcopy /d /i "%source%\%file%" "%dest%\"

Thank you very much for your support.

Upvotes: 1

Views: 7686

Answers (1)

Monacraft
Monacraft

Reputation: 6630

Ok, its not to complicated:

@echo off: Prevents the user from seeing what commands are bein inputed from the batch files (only output can be seen)

set "var=value": Creates a varaible called var with the value of value

pushd "%source%" ||(echo.Source does not exist&pause&goto EOF):

Changes the current directory to that of the value of varaible source and if there is any output (i.e If there is an error) it will pause and exit with the given erroro message

for /f "tokens=*" %%f in ('dir /A-D /OD /B') Do set "file=%%f":

Will go through every folder in the current directory, and set the value of varaible file to its name. It will do this in alphabetical order, so the directory last in this order will be the value of file

popd: Sets the current directory to what it was before the last pushd command

xcopy /d /i "%source%\%file%" "%dest%\": Copies whatever is the value of variable file in the direcotory which has been set to the value of source, and copies it to the the path of the variable dest

And thats it. If you want a better understanding of how to use these commands look for a tutorial.

Upvotes: 1

Related Questions