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

时间:2022-03-24 02:06:56

I am not a coder and have absolutely zero coding/computing knowledge. I have tried my best to follow the solutions given by others in response to similar questions to mine but am getting nowhere fast.

我不是一个编码器,并且绝对没有编码/计算知识。我已经尽力遵循其他人给出的解决方案,以回应类似的问题,但我无法快速完成任务。

Basically I am looking for a solution that will let me batch move some wav files from one folder, into a subfolder (that I have already set up in the same place), based on a list of filenames that I have in a text file.

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

I have a script, but it isn't working. It could be the script, or even something else really stupid I'm overlooking in the process I'm going through as this is the first time I have ever tried to do something like this. I will explain exactly what I've done and if anyone can enlighten me as to where I am going wrong that would be amazing...

我有一个脚本,但它不起作用。它可能是剧本,甚至是其他我真正愚蠢的东西,我正在经历这个过程,因为这是我第一次尝试做这样的事情。我会准确地解释一下我做了什么,如果有人能够告诉我哪里出错了那就太棒了......

I have set the execution policy in Powershell so it will allow me to run scripts.

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

I have made a file called filemovescript.ps1 which contains the following script:

我创建了一个名为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

All my wav files (about 10k of them) are in the folder 'combined set'. The text file Barbastelle_list.txt contains a list of the filenames of a subset of those wav files, like this:

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

TL_20170531_034316.wav

TL_20170514_012440.wav

TL_20170531_034717.wav

TL_20170524_215307.wav

I want the script to move the files specified in the text file into the subfolder 'Barbastelle'.

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

I have opened a Powershell window (powershell.exe - there are also options to open powershell_ise - I don't know what the difference is) from the start menu and pasted in this script text above. Powershell then gives me back the following:

我从开始菜单打开了一个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

I took this script from another question on this site, and it seemed to work for them so am really unsure where I'm going wrong - and I understand none of the above feedback from Powershell! How can I make this work?

我从这个网站上的另一个问题中获取了这个脚本,它似乎对他们有效,所以我真的不确定我哪里出错了 - 我不理解Powershell的上述反馈!我怎样才能做到这一点?

1 个解决方案

#1


3  

As mentioned in the comments of your question, you cant put batch into a Powershell console.

正如您的问题评论中所提到的,您无法将批处理放入Powershell控制台。

A quick PowerShell sample i made:

我制作的快速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

This only works if the console is working from the specified directory, Navigate to it with 'cd "Path/to/directory"' or run the script directly from the folder with the 'Run with Powershell' option.

这仅在控制台使用指定目录工作时才有效,使用'cd“Path / to / directory”'导航到它,或直接从带有'Run with Powershell'选项的文件夹运行脚本。

#1


3  

As mentioned in the comments of your question, you cant put batch into a Powershell console.

正如您的问题评论中所提到的,您无法将批处理放入Powershell控制台。

A quick PowerShell sample i made:

我制作的快速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

This only works if the console is working from the specified directory, Navigate to it with 'cd "Path/to/directory"' or run the script directly from the folder with the 'Run with Powershell' option.

这仅在控制台使用指定目录工作时才有效,使用'cd“Path / to / directory”'导航到它,或直接从带有'Run with Powershell'选项的文件夹运行脚本。