繁体   English   中英

根据文件名列表将文件批量移动到子文件夹

[英]Batch move files to a subfolder based on list of filenames

我不是编码人员,并且绝对没有零编码/计算知识。 我已经尽力遵循其他人针对我的类似问题提供的解决方案,但进展并不很快。

基本上,我正在寻找一种解决方案,该解决方案可以让我根据文本文件中包含的文件名列表,将一些wav文件从一个文件夹中批量移至一个子文件夹(该文件夹已经在同一位置设置)。

我有一个脚本,但是没有用。 这可能是脚本,甚至可能是我在经历的过程中忽略的其他一些愚蠢的事情,因为这是我第一次尝试做这样的事情。 我会确切地解释我所做的事情,并且如果有人能启发我哪里做错了,那将是惊人的……

我已经在Powershell中设置了执行策略,因此它将允许我运行脚本。

我制作了一个名为filemovescript.ps1的文件,其中包含以下脚本:

@echo off
set "Source=E:\Dissertation\My deployment data\Data to analyse\combined set"
set "Target=E:\Dissertation\My deployment data\Data to analyse\combined set\Barbastelle"
set "FileList=E:\Dissertation\My deployment data\Data to analyse\combined set\Barbastelle_list.txt"
echo.

if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
if not exist "%Target%" md "%Target%"

for /F "delims=" %%a in ('type "%FileList%"') do (
   for /f "delims=" %%b in ('dir "%Source%\%%a" /b /s /a-d ') do echo copying "%%b"&xcopy /b "%%b" "%Target%%%~pb" >nul
)
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul

我所有的wav文件(大约1万个)都在“组合集”文件夹中。 文本文件Barbastelle_list.txt包含这些wav文件的子集的文件名列表,如下所示:

TL_20170531_034316.wav

TL_20170514_012440.wav

TL_20170531_034717.wav

TL_20170524_215307.wav

我希望脚本将文本文件中指定的文件移动到子文件夹“ Barbastelle”中。

我从开始菜单中打开了一个Powershell窗口(powershell.exe-也有打开powershell_ise的选项-我不知道有什么区别)并粘贴在上面的脚本文本中。 Powershell然后给我以下信息:

At line:1 char:7
+ @echo off
+       ~~~
Unexpected token 'off' in expression or statement.
At line:7 char:3
+ if not exist "%Source%" echo Source folder "%Source%" not found & got ...
+   ~
Missing '(' after 'if' in if statement.
At line:7 char:65
+ ... ot exist "%Source%" echo Source folder "%Source%" not found & goto Ex ...
+                                                                 ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double
quotation marks ("&") to pass it as part of a string.
At line:8 char:3
+ if not exist "%FileList%" echo File list "%FileList%" not found & got ...
+   ~
Missing '(' after 'if' in if statement.
At line:8 char:65
+ ... ot exist "%FileList%" echo File list "%FileList%" not found & goto Ex ...
+                                                                 ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double
quotation marks ("&") to pass it as part of a string.
At line:9 char:3
+ if not exist "%Target%" md "%Target%"
+   ~
Missing '(' after 'if' in if statement.
At line:11 char:4
+ for /F "delims=" %%a in ('type "%FileList%"') do (
+    ~
Missing opening '(' after keyword 'for'.
At line:12 char:84
+ ...  in ('dir "%Source%\%%a" /b /s /a-d ') do echo copying "%%b"&xcopy /b ...
+                                                                 ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double
quotation marks ("&") to pass it as part of a string.
At line:1 char:1
+ @echo off
+ ~~~~~
The splatting operator '@' cannot be used to reference variables in an expression. '@echo' can be used only as an
argument to a command. To reference variables in an expression use '$echo'.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

我从该站点上的另一个问题中获取了此脚本,该脚本似乎对他们有用,因此我真的不确定我要去哪里—我不了解Powershell的上述反馈! 我该如何进行这项工作?

如您的问题注释中所述,您不能将批处理放入Powershell控制台。

我做了一个快速的PowerShell示例:

#initialisation
CLS
$ErrorActionPreference = 'Stop'
$VerbosePreference = "continue"

#Settings
$SubFolder = ".\Barbastelle"
$FileListFile = ".\Barbastelle_list.txt"

#Retrieve List with Files to move from current folder.
Try { [Array]$FilesToMove = Get-Content $FileListFile }
Catch {Write-Warning "could not load $($FileListFile)"; Start-Sleep -S 3 ; Exit}

#If subfolder does not exist then create it.
If (!(Test-Path $SubFolder)) {
    Try { New-Item $SubFolder -ItemType Directory | Out-Null}
    Catch {Write-Warning "Could not create subfolder $($SubFolder)"; Start-Sleep -S 3 ; Exit}
}

#Try to moving the files from the list to the the specified subfolder.
Foreach ($File in $FilesToMove) {

    #If File does not exist then skip.
    If (!(Test-Path $File)) {
        Write-Verbose "File $($File) Does not exist, skipping"
        Continue
    }

    # Check if files already exist in the sub folder.
    If (Test-Path (Join-Path -Path $SubFolder -ChildPath $File)){
        Write-Verbose "File $($File) exists already in the subfolder, skipping"
        Continue    
    }


    #try copying the file.
    Try {
        $File | Move-Item -Destination $SubFolder;
        Write-Verbose "File $($File) succesfully moved."
    }
    Catch {Write-Warning "Could not move file $($File), Skipping"; Continue}        
}

Write-Verbose "Script finished, waiting for 5 seconds before closing."
Start-Sleep -Seconds 5

仅当控制台在指定目录中运行时才有效,使用“ cd“ Path / to / directory””导航至该控制台,或者使用“使用Powershell运行”选项直接从该文件夹运行脚本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM