我正在尝试在Win7中创建一个批处理文件,它会将所有照片和视频从可能位于多个用户配置文件中的某个文件夹移动到共享驱动器上的单个文件夹。
我想出了这个……
for /d %%u in (C:\Users\*) do for %%x in (jpg jpeg bmp png gif raw jfif mov mp4 3gp) do xcopy "C:\Users\%%~nu\dropbox\camera uploads\*.%%x" "\\media\goflex home public\photos\dropbox camera uploads\" /c /i /y /s /d
for /d %%u in (C:\Users\*) do for %%x in (jpg jpeg bmp png gif raw jfif mov mp4 3gp) do erase "C:\Users\%%~nu\dropbox\camera uploads\*.%%x" /f但是,如果我没有连接到共享驱动器,这些文件将不会复制,但会被删除。因此,希望使用移动命令而不是复制。
或者,if命令也可能起作用,如果目标不可用,则不完成批处理。
发布于 2014-03-27 18:48:06
PowerShell
既然你同意了PowerShell解决方案--作为基础--你知道,.ps1文件“或多或少”等同于.bat文件。Powershell预装在较新版本的Windows中。它的目标是完全取代经典的cmd,因为那个处理器现在真的已经过时了。目前PowerShell的稳定版本是4.0 -你必须从MSDN站点下载更多的包。但是,我发布的脚本应该可以工作,因为它只使用最基本的命令。我不太确定这一点,但你必须试一试。我已经在脚本周围添加了注释,试图解释每一行。Technet网站上也有一个非常适合学习者的博客:Hey, Scripting Guy!如果你根本没有在机器上运行PowerShell,我假设你必须将执行策略设置为RemoteSigned。以管理员身份打开PowerShell并写入-
Set-ExecutionPolicy Unrestricted现在来看实际的代码。将其保存到.ps1文件或从ISE运行它。我认为stackoverflow不是为初学者编写Powershell详细指南的合适地方,而且在Internet上已经足够了。确保更改变量以满足您的需要。此外,如果您在问题中添加" Powershell“标签,以便其他用户看到Powershell解决方案,也会很好。如果您对自动化感兴趣,那么Powershell是不错的选择,因为它在设计时就考虑到了自动化。
#The 'array' in square brackets determine the type of variable. Variable is initialized by '$' sign. As we are looking for many types of objects, the array is required.
[array]$Extensions = "*.jpg","*.jpeg","*.bmp","*.png","*.gif","*.raw","*.jfif","*.mov","*.mp4","*.3gp"
#At '$DestinationDir' you point the directory to be searched
$DestinationDir = "C:\Users\"
#At 'SourceFiles' we are listing recursively all the files that match either of the extensions listed in the array (-include paramter). Get-ChildItem cmdlet is somehow equivalent of "dir" in batch. -recurse option determines that we are to look for data in each subdirectory
$SourceFiles = Get-ChildItem -recurse $DestinationDir -Include $Extensions
#The destination directory
$MediaDir = "\\media\someshare\"
#Here goes the conditional statement. Test-Path checks if the '$MediaDir' (above) exists, or is reachable. If it returns $true (the path exists), the script proceeds to perform actions in curly brackets.
if (Test-Path $MediaDir)
{
#Here goes the actual routine. '$SourceFiles' lists all the files that matched the expression. Then, the output is piped to the second part of function. The function takes one object each from '$SourceFiles', copies them to the destination and removes the item from its' original directory. The '$_' denotes a current object that is being processed in the "forEach-Object" loop.
$SourceFiles | ForEach-Object
{
Copy-Item $_ -Destination $MediaDir
Remove-Item $_
}
#The script finishes after all elements are processed. If the condition at the 'if' statement above is false. the script skips the Copy/Remove routine and prints to the host (console) that media is not reachable and it's aborting.
}
else {Write-Host "Media is not reachable, aborting any operation" }https://stackoverflow.com/questions/22669363
复制相似问题