【发布时间】:2014-11-05 17:40:46
【问题描述】:
我一直在编写一个脚本,它将所有内容从我的下载文件夹移动到外部驱动器上的另一个文件夹(我们现在将其称为“备份驱动器”),按扩展名将其分类到文件夹中(最初是在 StackOverflow 上找到的)开始 --Thanks to Nicola Cossu--)。每隔一段时间,我需要使用脚本才能将内容从另一个驱动器拉到备份驱动器。
我想要一个脚本来询问我的源驱动器和目标驱动器,但我希望它能够预定义我的下载文件夹和备份驱动器文件夹。
我认为可行的方法之一是弹出对话框,让每个问题的位置都已填写,以便我仅在需要时对其进行编辑。我不确定如何执行此操作。
如果它需要硬编码为变量 $MySRC 和 $MyDST,那很好。 提前谢谢!!!
这是我的代码:
# Get Start Time of the Script
$startDTM = (Get-Date)
# Get Source and Destination -- this is where I am trying to get the predetermine variables to be
$source = Read-Host "Enter for Source"
$dest = Read-Host "Enter for Destination"
# This could be where my variables are located
# $MySRC = "C:\Users\ME\Downloads"
# $MyDST = "E:\Sorted Downloads"
# Logging information for personal reasons
echo ""
echo "This script is starting"
echo ""
echo "The source is "
$source
echo ""
echo "The destination is "
$dest
echo ""
echo ""
# Actual Script to Run
$file = gci -Recurse $source | ? {-not $_.psiscontainer}
$file | group -property extension |
% {if(!(test-path(join-path $dest -child $_.name.replace('.','')))) { new-item -type directory $(join-path $dest -child $_.name.replace('.','')).toupper() }}
$file | % { move-item $_.fullname -destination $(join-path $dest -child $_.extension.replace(".",""))}
# End of Script Information -- Personal Reasons
echo "This Script is Done!"
echo 'Source: ' $source ' Destination: ' $dest
echo ""
# Get End Time:
$endDTM = (Get-Date)
# Echo Time Elapsed for the Script to Run:
"Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
【问题讨论】:
-
这个问题有严重的TL;DR!我建议您编辑它以缩小您遇到的问题,而不是它存在的背景。还要避免在一个问题中提出多个问题。
-
已修复 TL;DR,对不起那些人!
标签: powershell input field defined