由于脚本应该从外部驱动器在不同的Windows计算机上运行,到目前为止我已经有了这个可执行的批处理文件: create_list.bat
:: http://www.howtogeek.com/204088/how-to-use-a-batch-file-to-make-powershell-scripts-easier-to-run/
@ECHO OFF
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%~dpn0.ps1'"
echo+
echo Done! list.txt was created.
echo+
PAUSE
在powershell中执行这一行: create_list.ps1
Get-ChildItem | tree /F /A > list.txt
这非常有效。 现在我想排除所有文件夹“VIDEO_TS”和“subs”。我怎样才能做到这一点? 我没有修复PowerShell / cmd,但它应该只需双击大多数较新的Windows系统(因此.exe也适合)。
我没有使用纯批次,因为它弄乱了所有特殊字符,如变形金刚(äöü)。树命令本身不支持更多参数,我完全不了解PowerShell。 我为powershell尝试了-Exclude参数,但要么它不起作用,要么搞砸了。
是否可以通过这种方式排除目录或文件(batch + powershell),还是需要其他方法?
使用PowerShell社区扩展(PSCX)中的show-tree cmdlet
. "R:\Filme\show-tree.ps1" show-tree "R:\Filme" -MaxDepth 5 >> "R:\Filme\testlist.txt"
在循环结束时出现此错误(我已翻译):
Error calling method, because [System.Collections.ObjectModel.Collection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]] doesn't have a method named "SubString". In R:\Filme\show-tree.ps1:136 Char:9 + $RootRelativePath = $ProviderPath.SubString($d.PSDrive.Root.Length) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
$psversiontable
Name Value ---- ----- PSVersion 4.0 WSManStackVersion 3.0 SerializationVersion 1.1.0.1 CLRVersion 4.0.30319.42000 BuildVersion 6.3.9600.17400 PSCompatibleVersions {1.0, 2.0, 3.0, 4.0} PSRemotingProtocolVersion 2.2
从aschipfl's answer调用另一个批处理文件中的脚本:
chcp 1252
CALL aschipfl.bat >> list.txt
答案 0 :(得分:1)
这是一个使用递归的纯batch-file解决方案:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Comma-separated list of quoted exclusions for directories:
set _DEXCL="VIDEO_TS","subs"
rem // Comma-separated list of quoted exclusions for files:
set _FEXCL=
set "_FILES=" & rem // (set to non-empty value to include files as well)
rem // Global ariable indicating current directory level used for indent:
set /A "$LEVEL=0"
set "ITEM=%~1" & rem // (first command line argument specifies root directory)
rem // No root directory has been specified, so use current directory:
if not defined ITEM set "ITEM=."
rem // Check given root directory for existence and quit script if not found:
if not exist "%ITEM%\" exit /B 1 & rem (trailing "\" to not find files)
rem // Check given root directory against wild-cards and quit if some occur:
set "FLAG=#" & for %%J in ("%ITEM%") do if "%%~J"=="%ITEM%" set "FLAG="
if defined FLAG exit /B 1
rem // Process root directory in sub-routine:
call :PROCESS "%ITEM%"
endlocal
exit /B
:PROCESS val_dir_path
rem // Display name of provided directory:
set "PTH=%~nx1"
setlocal EnableDelayedExpansion
call :INDENT PAD %$LEVEL%
echo(!PAD!!PTH!
endlocal
rem // Increase indent:
set /A "$LEVEL+=1"
rem // Process all sub-directories:
for /D %%D in ("%~1\*") do (
rem // Check iterated directory against exclusion list:
set "FLAG="
for %%E in (%_DEXCL%) do (
if /I "%%~nxD"=="%%~E" set "FLAG=#"
)
rem // Process iterated directory in sub-routine recursively:
if not defined FLAG (
call :PROCESS "%%~D"
)
)
rem // Process all files in case they are to be included:
if defined _FILES (
for %%F in ("%~1\*.*") do (
rem // Check iterated file against exclusion list:
set "FLAG="
for %%E in (%_FEXCL%) do (
if /I "%%~nxF"=="%%~E" set "FLAG=#"
)
rem // Display name of iterated file:
if not defined FLAG (
set "FILE=%%~nxF"
setlocal EnableDelayedExpansion
call :INDENT PAD %$LEVEL%
echo(!PAD!!FILE!
endlocal
)
)
)
rem // Decrease indent:
set /A "$LEVEL-=1"
exit /B
:INDENT rtn_string val_number
rem // Build indent string consisting of spaces:
setlocal EnableDelayedExpansion
set "IND="
for /L %%I in (1,1,%~2) do set "IND=!IND! "
endlocal & set "%~1=%IND%"
exit /B
答案 1 :(得分:0)
你必须在 Get-ChildItem
中使用Exclude参数在这种情况下使用以下代码:
Get-ChildItem E:\Source_Test -Exclude "subs","video_ts" | tree /F /A > list.txt
这将按要求完成您的工作。
希望它有所帮助。