简体   繁体   English

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

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

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'. 我所有的wav文件(大约1万个)都在“组合集”文件夹中。 The text file Barbastelle_list.txt contains a list of the filenames of a subset of those wav files, like this: 文本文件Barbastelle_list.txt包含这些wav文件的子集的文件名列表,如下所示:

TL_20170531_034316.wav TL_20170531_034316.wav

TL_20170514_012440.wav TL_20170514_012440.wav

TL_20170531_034717.wav TL_20170531_034717.wav

TL_20170524_215307.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窗口(powershell.exe-也有打开powershell_ise的选项-我不知道有什么区别)并粘贴在上面的脚本文本中。 Powershell then gives me back the following: 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! 我从该站点上的另一个问题中获取了此脚本,该脚本似乎对他们有用,因此我真的不确定我要去哪里—我不了解Powershell的上述反馈! How can I make this work? 我该如何进行这项工作?

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””导航至该控制台,或者使用“使用Powershell运行”选项直接从该文件夹运行脚本。

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

相关问题 根据某些文件名模式将文件排序并将其移动到目录中 - Order and move files into directories based on some filenames pattern 如何根据文件名将文件从一个文件夹批量移动到另一个文件夹? - How can I bulk move files from one folder to another based on filenames? 根据年月LastWriteTime持久化子文件夹将文件移动到子文件夹 - Move files to sub folders based upon year and month LastWriteTime persevering subfolder 从文本文件的文件名列表将文件从一个文件夹移动到另一个文件夹 - Move files from one folder to another from a list of filenames from a text file 通过重新排列文件名来批量重命名目录中的文件 - Batch Rename Files in a Directory By Rearranging Filenames 获取特定子文件夹中的文件列表 - get files list in a specific subfolder Powershell脚本基于源列表(.txt - Powershell script to move files based on a source list (.txt 使用过滤器批量重命名部分文件名以识别具有设置前缀的文件 - Batch renaming part of filenames using filter to identify files with set prefix 使用powershell根据另一个文件夹中的文件名批量重命名文件 - batch rename files according to filenames in another folder with powershell 使用 PowerShell 将子文件夹移动到其他位置并根据父级重命名 - Move subfolder elsewhere and rename based on parent using PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM