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.
Single line DOS commands and echoing formatted output
Moderator: DosItHelp
Re: Single line DOS commands and echoing formatted output
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.
Re: Single line DOS commands and echoing formatted output
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.
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.
Re: Single line DOS commands and echoing formatted output
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.)
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 @.
The number of files in the folder can be found using the following command.
HINT:
Only folders (directories): DIR /ad
Only files (not directories): DIR /a-d
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
Re: Single line DOS commands and echoing formatted output
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!
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!
Re: Single line DOS commands and echoing formatted output
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%]
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%]
Re: Single line DOS commands and echoing formatted output
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:.
Use the code without NUL:.
2.
or or .
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!]
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.]
Code: Select all
for /f %a in ('dir /b /a-d "*.txt"^| find /c /v ""') do @echo [FILECOUNT=%a]