Batch file or powershell script help

Soldato
Joined
26 May 2009
Posts
22,101
Hi, I want to write either a batch file or a powershell script that when executed will delete the content of a folder then copy the newest file in another folder into it. Anyone got any ideas? Google doesn't seem to be working so maybe I'm describing it wrong /shrug.
 

maj

maj

Soldato
Joined
19 Jul 2010
Posts
2,602
Location
Durham
Taken from google and mixing together two other people's codes but this works for me:

Code:
REM Empty destination folder and then copy the most recent file from source directory to it
@echo off

set folder="C:\New folder"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)

setlocal
set srcDir=c:\New folder2
set destdir=C:\New folder
set lastmod=
pushd %srcDir%
for /f "tokens=*" %%a in ('dir *.* /b /o-d /a-d 2^>NUL') do set lastmod=%%a & goto :xx
:xx
if "%lastmod%"=="" echo Could not locate files.&goto :eof
:::
copy "%lastmod%" "%destDir%"

Save this a <filename>.bat
 
Soldato
Joined
18 Oct 2002
Posts
8,123
Location
The Land of Roundabouts
powershell

wasnt sure which folder you wanted to delete from

Code:
$SourceFolder = "u:\Folder1"
$DesinationFolder = "u:\Folder2"

get-childitem $DesinationFolder -recurse | remove-item
Copy (Get-ChildItem -File $SourceFolder | Sort-Object -Descending CreationTime |  select -first 1).fullname $DesinationFolder
 
Last edited:
Back
Top Bottom