how do you get properties from a file with batch?

I need to know how to see if files(like .txt files) are empty with batch and how to create new files(like the ones you store pics and .txt files in) and how to move files(like .txt) into the files you just created

Thanks in advance for any answers!

sort by: active | newest | oldest
I also found this on dostips:

:fprop filename prop ret -- returns a file property
::                       -- filename  [in]  - file name
::                       -- prop      [in]  - property, i.e.: d p n x a f s t z
::                       -- ret       [out] - return value
:$created 20060101 :$changed 20080219 :$categories FileInfo
:$source http://www.dostips.com
SETLOCAL
set ret=
for %%a in ("%~1") do set ret=%%~%~2a
( ENDLOCAL & REM RETURN VALUES
    IF "%~3" NEQ "" (SET %~3=%ret%) ELSE echo.%ret%
)
EXIT /b


When I pick a cause, I commit. 

Tell me if you need anything else.
I know this question is a little old, but I just developed this code for you:

@echo off
setlocal enabledelayedexpansion
color 0a
title Find Attributes in Files

:start
set /p atname=Name of the file:
if not exist %atname% (
cls
echo No file of that name exists!
echo.
echo Press any key to go back
pause>nul
goto start
)
for /f %%i in (%atname%) do set attribs=%%~ai
set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
cls
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point
echo.
echo.
echo Press any key to go back
pause>nul
goto start

Hope it helps.
XD
Oh XD, when you said you wanted properties, I assumed you wanted attributes.

Ok, I'll make you another to see if a file is empty or not.

PS: you might want to add a "cls" to the start of the "start" section, otherwise things are going to get messy.

@echo off
setlocal enabledelayedexpansion
color 0a
title Determine whether a file is empty or not

:start
cls
set /p file=What is the name and directory of the file (Example: C:/Users\My_File.txt):
if not exist %file% (
cls
echo No file of that name exists...
echo.
echo Press any key to go back
pause>nul
goto start
)
cls
for /f %%i in (%text%) do set size=%%~zi
if %size% geq 1 (
echo The file contains the following:
type %file%
) ELSE (
echo The file is empty
)
echo.
echo Press any key to go back
pause>nul
goto start
orksecurity6 years ago
Testing whether a file *exists* is easy in .bat files: "IF EXISTS filename" will do it. Getting the actual file length requires some moderately obscene dancing around in circles with forcing the output of a DIR command to be executed as a batch subroutine or similar ugliness -- or writing a program in another language to return this information, at which point you might as well give up on the batchfiles and just write in that language.

Moving files: Just use the MOVE or COPY command, exactly as you would from the command line. In general, if you can do it from the command line, you can do it from a batch file. (And, with suitable adjustments, the reverse is usually true; I often use the FOR command from the command line, for example. The trick is remembering that at the command line you do not have to double the % character.)

There's a lot of information about using batch files in existing Instructables, and elsewhere on the web; I'd suggest you go and read some tutorials. There's also a significant amount of information available on your own machine; try reading the output of HELP CMD and HELP SET.

Generally, batchfiles should be considered a convenience -- a way to automate what you would type at the command line for something you do more than a few times -- and NOT a full programming language. As soon as you get beyond the basics, .BAT really is inadequate. It can be forced to do some things it wasn't designed to do, with sufficiently clever hackery, but now that compilers/interpreters for better languages are widely available cheap or free there's very little reason to make that effort except to show off ... and if that's your goal, you should try to solve the puzzle yourself, right?
Is there Perl for Windoze? Or is that like asking if you can get a first-baseman's mitt to fit your goldfish?
There are a Windows implementations of Perl. That would be a step up from using the .bat language. I think there's a Windows version of REXX too, which again is a better tool than .BAT.

Personally, I generally go right to Java or C/C++ when I want to do anything nontrivial. But I've been doing this for Too Many Years. I remember the days when we had to do things like trying to write batch files to generate batch files to misinterpret other files as batchfiles, and while those tricks still work they really are incredibly ugly and somewhat failure-prone. Better to do the job right.

"Quick, Robin -- to the Bat File!"
FastLearner (author)  orksecurity6 years ago
what language do you think would work?
mwrenfro6 years ago
Perl, Python, Ruby, take your pick. All of them exist for Windows. Any of them will result in much less hair loss than trying to do this in batch. VBScript in Windows Script Host is better than batch, for that matter. PowerShell in more recent versions of Windows is a weird language, but it's installed by default and has many more features than batch.

Any of these languages are better options than batch.