我不是编码员,并且绝对没有编码/计算知识。我已经尽力遵循其他人给出的解决方案来回应我的类似问题但是我无法快速完成。
基本上我正在寻找一种解决方案,让我可以将一些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文件(大约10k)都在'组合集'文件夹中。文本文件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的上述反馈!我怎样才能做到这一点?
答案 0 :(得分:3)
正如您的问题评论中所提到的,您无法将批量放入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"'导航到它或者使用' Run with Powershell'直接从文件夹运行脚本。选项。