Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

Copy and Rename powershell script

I am working on a script that will find the most recently written file and then copy it to a different folder and rename it. I have the script. For some odd reason when I don't choose a name to rename it to, it will just copy the file with the same name to the destination folder. When I do put a name in it creates a blank .file file. I Am not sure why this is happening. Any suggestions? HEre is the Code

# set the source directory
$sourceDir = 'C:Users
wrigDesktopSavefolder1'
# set the destination directory
$destDir = 'C:Users
wrigDesktopSavefolder2'

# get the most recent file in the source path
$fileToCopy = $fileToCopy = Get-ChildItem -Path $sourceDir | Sort-Object -Property LastWriteTime | 
Select-Object -Last 1

# copy the file to the other folder
Copy-Item -Path  $fileToCopy.FullName -Destination $destDir

# rename the file at the new dir
Rename-Item -LiteralPath ($destDir + '' +  $fileToCopy.Name) -NewName ''

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...