簡體   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