Single line DOS commands and echoing formatted output

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
necron99
Posts: 4
Joined: 17 Oct 2015 09:15

Single line DOS commands and echoing formatted output

#1 Post by necron99 » 17 Oct 2015 09:34

I have a requirement for a project I'm working on at my job that requires some serious DOS programming skills that I am lacking and was hoping someone would be able to come up with a solution.

#1 A single DOS command line that can determine if a directory exists and echo [DIRECTORY_EXISTS=true] or [DIRECTORY_EXISTS=false]

#2 A single DOS command line that can return a file count from a path and echo [FILECOUNT=X] where X is the number of files in the path provided.

I have found examples of commands that will accomplish both of these requirements but I can't get them to output in the format that is required by the project. Any help would be greatly appreciated.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Single line DOS commands and echoing formatted output

#2 Post by Squashman » 17 Oct 2015 09:43

necron99 wrote: I can't get them to output in the format that is required by the project. .

Then maybe you should provide those details otherwise we will just be guessing.

necron99
Posts: 4
Joined: 17 Oct 2015 09:15

Re: Single line DOS commands and echoing formatted output

#3 Post by necron99 » 17 Oct 2015 10:18

Sorry...here's the first command I found that will generate the file count. Right now it just spits out a number which doesn't meet my requirement for formatted output.

dir /b/a-d "*.txt" | find /v /c "::"

and the second that determines the existence of a directory is just a 'dir somepath...' and if I get back an error I know it doesn't exist but that doesn't meet the requirement that's been given to me of formatting some output so we know it's not some other type of error.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Single line DOS commands and echoing formatted output

#4 Post by trebor68 » 17 Oct 2015 12:09

There are various ways in order to check the existence of a folder.

Code:
if exist E:\Programme\VirtuaGirl\data\d0100\nul (echo Folder exist.) else (echo Folder not exist.)

The folder can be specified as absolute or relative.
Quotation marks are required if the specified path contains spaces.

Example 1: Check folder if this in this folder.
Example 2: Check folder if this in the realtive folder.
Example 3: Check folder if this in the absolute folder. (Same folder as Example 2.)

Code: Select all

E:\Programme\VirtuaGirl\models>if exist d0100\nul (echo Folder exist.) else (ech
o Folder not exist.)
Folder not exist.

E:\Programme\VirtuaGirl\models>if exist ..\data\d0100\nul (echo Folder exist.) e
lse (echo Folder not exist.)
Folder exist.

E:\Programme\VirtuaGirl\models>if exist E:\Programme\VirtuaGirl\data\d0100\nul (
echo Folder exist.) else (echo Folder not exist.)
Folder exist.

E:\Programme\VirtuaGirl\models>



It is also possible to check whether there is a subfolder named.

Code:
for /f %a in ('dir /b /ad ^| find /c "d0100"') do if %a equ 0 (echo Folder not exist.) else (echo Folder exist.)

Example 2 and 3 are the same command. The differnt is only @.

Code: Select all

E:\Programme\VirtuaGirl\models>for /f %a in ('dir /b /ad ^| find /c "d0100"') do
 if %a equ 0 (echo Folder not exist.) else (echo Folder exist.)

E:\Programme\VirtuaGirl\models>if 0 EQU 0 (echo Folder not exist. )  else (echo
Folder exist. )
Folder not exist.

E:\Programme\VirtuaGirl\models>for /f %a in ('dir /b /ad ^| find /c "a0100"') do
 if %a equ 0 (echo Folder not exist.) else (echo Folder exist.)

E:\Programme\VirtuaGirl\models>if 1 EQU 0 (echo Folder not exist. )  else (echo
Folder exist. )
Folder exist.

E:\Programme\VirtuaGirl\models>for /f %a in ('dir /b /ad ^| find /c "a0100"') do
 @if %a equ 0 (echo Folder not exist.) else (echo Folder exist.)
Folder exist.

E:\Programme\VirtuaGirl\models>



The number of files in the folder can be found using the following command.

Code: Select all

E:\Programme\VirtuaGirl\models>(@set num=0) & (for /f %a in ('dir /b /a-d *.txt
) do @set /a num+=1 >nul) & @echo In this folder are !num! files.
In this folder are 2 files.

E:\Programme\VirtuaGirl\models>



HINT:
Only folders (directories): DIR /ad
Only files (not directories): DIR /a-d

necron99
Posts: 4
Joined: 17 Oct 2015 09:15

Re: Single line DOS commands and echoing formatted output

#5 Post by necron99 » 17 Oct 2015 13:13

Yep, that did the trick!

You dropped a ' on the second one and I had to switch out ! with % on the num at the end but other than that it works great - thanks!

necron99
Posts: 4
Joined: 17 Oct 2015 09:15

Re: Single line DOS commands and echoing formatted output

#6 Post by necron99 » 17 Oct 2015 13:26

oopppsss...noticed a problem. What happens when there's a space in the file name or path for either of these command lines? surprisingly we've had that happen. Here's how I've changed the above examples to support what I'm trying to do. Notice the space in the directory name in the first example and in the file name in the second. Thanx!


if exist "my junk"\nul (echo [FolderExists=true]) else (echo [FolderExists=false])

(@set num=0) & (for /f %a in ('dir /b /a-d "dos command.txt"') do @set /a num+=1 >nul) & @echo [FILECOUNT=%num%]

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Single line DOS commands and echoing formatted output

#7 Post by Yury » 17 Oct 2015 16:02

1.

Double quotes and NUL are incompatible, even if your code is written syntactically correct, as the following code always displays that the folder does not exist:

Code: Select all

if exist "my junk\nul" (echo [FolderExists=true]) else (echo [FolderExists=false])
.

Use the code without NUL:

Code: Select all

if exist "my junk\" (echo [FolderExists=true]) else (echo [FolderExists=false])
.


2.

Code: Select all

set "num="& (for /f eol^= %a in ('dir /b /a-d "*.txt"') do @>nul set /a num+=1) & cmd /v /c echo [FILECOUNT=!num!]
or

Code: Select all

set "num="& (for /f eol^= %a in ('dir /b /a-d "*.txt"') do @>nul set /a num+=1) & <nul set /p =[FILECOUNT=& set /a num& echo.]
or

Code: Select all

for /f %a in ('dir /b /a-d "*.txt"^| find /c /v ""') do @echo [FILECOUNT=%a]
.

Post Reply