问题
I've spent a good amount of time searching this (and other) websites trying to figure this out, but I finally admit that I'm stuck. I'm a beginner user, so I apologize in advance if my terminology / explanation is confusing.
OS: Windows 7
I'm working on a single batch file that will create a .csv file with attributes and paths for all folders and files (including subfolders) of the batch file's current folder location. I'm iterating this for all possible scenarios of the attributes. Below is an explanation of my code:
**create a heading in the csv file:
@echo on
echo Folder vs. File,Hidden,System Folder,Read Only,Path > FoldersAndFilesWithAttributes.csv
**copy file and folder names with specific attributes to a temporary .txt file
dir %~dp0 /s /n /a:dhsr /b >> FoldersAndFiles.txt
**append the attributes to the folder / file path and place into a csv file
for /f "tokens=*" %%i in (FoldersAndFiles.txt) do echo Folder,Hidden,System,ReadOnly,%%i >> FoldersAndFilesWithAttributes.csv
**delete temporary .txt file
del FoldersAndFiles.txt
The Problem(s) I'm not exactly sure where the problem(s) are because - for some reason - it works in certain folders, but not others. Sometimes I only get the heading when files with known attributes are present, sometimes the full path is not present, and sometimes I get nothing at all. I thought maybe I was overwriting the temporary file too quickly, so I created unique .txt files for each scenario, but that still didn't work.
**Below is my full code:
@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
Thanks for your help, Sean
回答1:
@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
回答2:
> FoldersAndFilesWithAttributes.csv (for /f "delims=" %%I in ('
dir /S /B /A "%~dp0*.*"') do echo %%~aI,"%%I")
As per Command Line arguments (Parameters):
Use
%~aI
to display the Extended Attributes of a file.FOR
's%%~aI
recognizes 9 NTFS file attributes. The expansion of a file attribute produces a series of 9 dashes, with each recognized attribute replacing a dash with a letter. A file with no recognized attributes or with none set will expand to 9 dashes like this:---------
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 ---------
You could make an accurate csv
with headers as follows (with filemask modified to narrow down output to reasonable size):
@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
Output
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>
来源:https://stackoverflow.com/questions/31079628/batch-directory-with-attributes-into-csv