具有属性的批处理目录到csv

时间:2015-06-26 18:14:29

标签: windows csv batch-file for-loop dir

我花了很多时间搜索这个(和其他)网站试图解决这个问题,但我终于承认我被困了。我是初学者用户,所以如果我的术语/解释令人困惑,我会提前道歉。

操作系统:Windows 7

我正在处理一个批处理文件,该文件将创建一个.csv文件,其中包含批处理文件当前文件夹位置的所有文件夹和文件(包括子文件夹)的属性和路径。我为所有可能的属性场景迭代这个。以下是我的代码的解释:

**在csv文件中创建一个标题:

@echo on
echo Folder vs. File,Hidden,System Folder,Read Only,Path > FoldersAndFilesWithAttributes.csv

**将具有特定属性的文件和文件夹名称复制到临时.txt文件

dir %~dp0 /s /n /a:dhsr /b >> FoldersAndFiles.txt

**将属性附加到文件夹/文件路径并放入csv文件

for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo Folder,Hidden,System,ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

**删除临时.txt文件

del FoldersAndFiles.txt

问题 我不确定问题的位置,因为 - 出于某种原因 - 它适用于某些文件夹,但不适用于其他文件夹。有时我只在具有已知属性的文件存在时才获得标题,有时候不存在完整路径,有时我什么也得不到。我想也许我会过快地覆盖临时文件,所以我为每个场景创建了独特的.txt文件,但这仍然没有用。

**以下是我的完整代码:

@echo off

echo Folder vs. File,Hidden,System Folder,Read Only,Path > FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:dhsr /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo Folder,Hidden,System,ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:dhs-r /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo Folder,Hidden,System,Not ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:dh-sr /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo Folder,Hidden,Not System,ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:dh-s-r /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo Folder,Hidden,Not System,Not ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:d-hsr /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo Folder,Not Hidden,System,ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:d-hs-r /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo Folder,Not Hidden,System,Not ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:d-h-sr /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo Folder,Not Hidden,Not System,ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:d-h-s-r /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo Folder,Not Hidden,Not System,Not ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:-dhsr /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo File,Hidden,System,ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:-dhs-r /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo File,Hidden,System,Not ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:-dh-sr /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo File,Hidden,Not System,ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:-dh-s-r /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo File,Hidden,Not System,Not ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:-d-hsr /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo File,Not Hidden,System,ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:-d-hs-r /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo File,Not Hidden,System,Not ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:-d-h-sr /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo File,Not Hidden,Not System,ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv

dir %~dp0 /s /n /a:-d-h-s-r /b >> FoldersAndFiles.txt
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo File,Not Hidden,Not System,Not ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv


del FoldersAndFiles.txt

感谢您的帮助, 肖恩

2 个答案:

答案 0 :(得分:0)

> FoldersAndFilesWithAttributes.csv (for /f "delims=" %%I in ('
    dir /S /B /A "%~dp0*.*"') do echo %%~aI,"%%I")

根据Command Line arguments (Parameters)

  

使用%~aI显示文件的扩展属性。 FOR' %%~aI   识别9个NTFS文件属性。扩展文件属性   生成一系列9个破折号,每个破折号都有一个识别的属性   用字母替换短划线。没有可识别属性的文件   或者没有设置将扩展为9个破折号:---------

Attribute                    Expansion 
FILE_ATTRIBUTE_DIRECTORY     d-------- 
FILE_ATTRIBUTE_READONLY      -r------- 
FILE_ATTRIBUTE_ARCHIVE       --a------ 
FILE_ATTRIBUTE_HIDDEN        ---h----- 
FILE_ATTRIBUTE_SYSTEM        ----s---- 
FILE_ATTRIBUTE_COMPRESSED    -----c--- 
FILE_ATTRIBUTE_OFFLINE       ------o-- 
FILE_ATTRIBUTE_TEMPORARY     -------t- 
FILE_ATTRIBUTE_REPARSE_POINT --------l
FILE_ATTRIBUTE_NORMAL        --------- 

您可以使用标题创建准确的 csv,如下所示(修改了文件掩码以将输出范围缩小到合理的大小):

@ECHO OFF
SETLOCAL enableextensions
> files\FoldersAndFilesWithAttributes.csv (
  rem csv header
  echo "d","r","a","h","s","c","o","t","l","file"
  rem csv lines
  for /f "delims=" %%I in ('
    dir /S /B /A "%~1*.*" 2^>Nul
                           ') do call :display "%%~aI" "%%I"
)

ENDLOCAL
goto :eof

:display
:: subroutine to display file attributes as csv line
:: %1 file attributes ---------
:: %2 file name
set "attr=%~1"
set "out="%attr:~0,1%""
set "out=%out%,"%attr:~1,1%""
set "out=%out%,"%attr:~2,1%""
set "out=%out%,"%attr:~3,1%""
set "out=%out%,"%attr:~4,1%""
set "out=%out%,"%attr:~5,1%""
set "out=%out%,"%attr:~6,1%""
set "out=%out%,"%attr:~7,1%""
set "out=%out%,"%attr:~8,1%""
set "out=%out:-=%"
echo %out%,"%~2"
goto :eof

<强>输出

d:\bat>D:\bat\SO\31079628new.bat *new

d:\bat>type files\FoldersAndFilesWithAttributes.csv
"d","r","a","h","s","c","o","t","l","file"
"","","a","","","","","","","d:\bat\files\pathnew.txt"
"","r","a","","","","","","","d:\bat\files\bubu\New Text Document.txt"
"","","a","h","","","","","","d:\bat\files\numeric\New Text Document.txt"
"","","a","","","","","","","d:\bat\SO\28526273new"
"","","a","","","","","","","d:\bat\SO\31079628new.bat"
"d","","","","","","","","","d:\bat\SO\New folder"
"","","a","","","c","","","","d:\bat\SU\New Text Document.txt"

d:\bat>

答案 1 :(得分:0)

@echo off
setlocal EnableDelayedExpansion

(
echo Folder vs. File,Hidden,System,Read Only,Path
for /F "delims=" %%a in ('dir /S /B /A') do (
   set "attribs=%%~Aa"
   if "!attribs:D=!" neq "!attribs!" (set "type=Folder") else set "type=File"
   set "hidden=Not Hidden"
   if "!attribs:H=!" neq "!attribs!" set "hidden=Hidden"
   set "system=Not System"
   if "!attribs:S=!" neq "!attribs!" set "system=System"
   set "readOnly=Not Read Only"
   if "!attribs:R=!" neq "!attribs!" set "readOnly=Read Only"
   echo !type!,!hidden!,!system!,!readOnly!,%%~Fa
)
) > FoldersAndFilesWithAttributes.csv