将文件从各种目录移动到一个目录

时间:2014-03-26 18:01:15

标签: windows batch-file powershell

我是StackOverflow发布新手,但之前已将其用作参考。我已经搜索了我的问题的合适答案,但我似乎找不到任何东西。

我试图在Win 7中创建批处理文件,该文件会将所有照片和视频从可能位于多个用户配置文件中的某个文件夹移动到共享驱动器上的单个文件夹。

我想出了这个......

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命令也可能有效,如果目的地不可用,则说不要完成批处理。

非常感谢任何帮助。

由于

更新

根据以下Preet Sangha的评论,我现在正在使用......

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
if exist "\\media\goflex home public\photos\dropbox camera uploads\" 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

....这个解决方案似乎正确地完成了工作,但任何其他解决方案都会很有用。

1 个答案:

答案 0 :(得分:0)

的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详细指南的地方,并且在互联网上有足够的内容。确保更改变量以满足您的需求。此外,如果您在问题中添加“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" }