2012-08-03 99 views
9

嗨我想创建一个批处理文件来获取所有的子文件夹。任何人都可以帮我解决这个问题吗?通过子文件夹循环

+2

下定决心......你想要一个批处理文件或PowerShell脚本? – Joey 2012-08-03 12:30:57

回答

9

这是在这两个批处理文件或PowerShell中微不足道的:

批次:

for /r %%x in (*.sql) do (
    rem "(or whatever)" 
    sqlcmd "%%x" 
) 

的PowerShell:

Get-ChildItem -Recurse -Filter *.sql | 
    ForEach-Object { 
     sqlcmd $_.FullName # or whatever 
    } 
+0

嗨@Joey,是否有任何方法使用sqlcmd循环所有的子文件夹,而没有硬编码的子文件夹路径?并为PowerShell脚本最后一行,请你提供这个“sqlcmd $ _#或其他”的例子吗?非常感谢 – 2012-08-06 01:37:19

1

这里是一个PowerShell脚本我用得到的大小“学校“文件夹中的每个用户的家庭文件夹的子文件夹。 IE浏览器。 N:\用户名\ UserNameOSXProfile \学校

SizeOfSchoolFolder.ps1

$schoolFolderTotalSize=0 

$foundChildren = get-childitem *\*OSXProfile\School 
foreach($file in $foundChildren){ 
    $schoolFolderTotalSize = $schoolFolderTotalSize + [long](ls -r $file.FullName | measure -s Length).Sum 
} 

switch($schoolFolderTotalSize) { 
    {$_ -gt 1GB} { 
    '{0:0.0} GiB' -f ($_/1GB) 
    break 
    } 
    {$_ -gt 1MB} { 
    '{0:0.0} MiB' -f ($_/1MB) 
    break 
    } 
    {$_ -gt 1KB} { 
    '{0:0.0} KiB' -f ($_/1KB) 
    break 
    } 
    default { "$_ bytes" } 
} 

我从拉添加数字: Adding up numbers in PowerShell

而且制作文件夹的大小看起来很漂亮: Get Folder Size from Windows Command Line

而且\ * \ * OSXProfile \ School作为搜索特定的子文件夹。 Limit Get-ChildItem recursion depth

0

要更换所有的文件名“XYZ”用“ABC”:

Get-ChildItem -Recurse -Filter *.mp3 | Rename-Item –NewName { $_.name –replace 'xyz','abc' }