So I'm going to count files in a directory including all subfolders and get a output as a number of how many files it found within the directory. The problem is I want to exclude all the folders that has the name error. Those files within that folder I do not want to be counted.
Simply...
for /r "\\server\share\folderA" %%G in (*.*) do (
set /A count=count + 1
)
echo %count%
Under folderA there are plenty of subfolders that I'm counting, but also the "error" folders that I do not want to count.
So I'm trying the following...
Create a temp file called exclude.txt and type error to that file
if not exist "c:\temp" mkdir "c:\temp" 2>nul
if not exist c:\temp\exclude mkdir c:\temp\exclude 2>nul
echo error> %excludefolder%\exclude.txt
Now I want to combine this somehow. Basically do something like this...
for /r "\\server\share\folderA" %%G in (*.*) EXCLUDE: c:\temp\exclude\exclude.txt do (
set /A count=count + 1
)
But I'm aware this will not work and I'm not sure how to work it out. Does anyone know? Thanks!
Use DIR /S /B /A-D to iterate all files. The output includes the full path to each file.
Pipe that result to FINDSTR /L /I /V "\\error\\ to filter out paths that contain \error\. This will also exclude any folders within an error folder. The search could be modified to exclude only 'error' but include children of 'error'.
Pipe that result to FIND /C /V "" to count the number of files (lines).
dir /s /b /a-d | findstr /liv "\\error\\" | find /c /v ""
The above will simply display the count.
If you want to capture the count in a variable, then use FOR /F to parse the output of the above command. Unquoted poison characters like | must be escaped when used in FOR /F.
#echo off
for /f %%N in ('dir /s /b /a-d^|findstr /liv "\\error\\"^|find /c /v ""') do set count=%%N
echo count=%count%
You can exclude from count the folder containing the string "error" :
#echo off
set count=0
for /r "\\server\share\folderA" %%a in (*.*) do (
echo %%~pa | find /i "error" || set /A count+=1
)
echo %count%
It's a good way if you sure that you just have ONE Error folder. If you have another folder containing the string "error" it will be excluded too from count.
Related
I currently have a list of files in a folder that i need to iterate through and exclude the non production files that matches the pattern. how do i search for multiple strings in a batch command?
for example i have files as
QA_test_1.txt
QA_809_test.txt
dev_93439_sd.txt
stg_abc_sldkf_df.txt
prod_4845_sdfd.txt
prod_998_sdfd.txt
live_lskd_sd3434.txt
xyz_sdfdf_s3rer.txt
from the list above i need to exclude every file that has the word "dev", QA "Stg" and generate a list of files without them - which is
prod_4845_sdfd.txt
prod_998_sdfd.txt
live_lskd_sd3434.txt
xyz_sdfdf_s3rer.txt
The problem is - i am not able to find any batch command that can do a grep equivalent for multiple strings. FindStr does it only for one string and hence i am not able to get that working. here is code that i have
set i=0
for /F "delims=" %%a in ('dir /B /A /S Path *.txt') do (
set /A i+=1
rem put the file names in array
set list[!i!]=%%~na
rem try to find the file names
echo %%~na|find "dev" or "qa" or "stg" >nul
if errorlevel 1 (echo notfound) else (echo found %%~na )
)
Thank you for all the response.
I was able to do it below way
echo %%~na|findstr /i /v "dev QA Stg" >nul
if errorlevel 1 (echo not found %%~na) else (echo %%~na>>newfile.txt)
I'm doing some BATCH scripting looping through files to copy. But I came to a problem where I need the path relative to the current .bat execution folder (%cd%)
So if I have files like this:
c:\games\batchTest\test.bat
c:\games\batchTest\subFolder1\test1.txt
How can I get just "subFolder1\test1.txt" so I can copy the file with the sub folder?
My current code:
for /r %%a in (*) do (
echo "%%a"
)
You can try this:
#Echo Off
Setlocal enabledelayedexpansion
For /r %%a In (*) Do (
Set p="%%a"
Echo !p:%__CD__%=!
)
The for /R loop always returns absolute paths, even if the (optional) given root directory behind /R is relative.
A possible way to get relative paths is to (mis-)use the xcopy command together with its /L option that prevents anything to be copied:
xcopy /L /S /I ".\*.*" "%TEMP%"
To remove the summary line # File(s) apply a filter using find using a pipe:
xcopy /L /S /I ".\*.*" "%TEMP%" | find ".\"
To process the returned items use a for /F loop:
for /F "eol=| delims=" %%F in ('
xcopy /L /S /I ".\*.*" "%TEMP%" ^| find ".\"
') do (
echo Processing file "%%F"...
)
If you just want to copy files including the sub-directory structure you do not even need the above stuff with for loops, you can simply use xcopy:
xcopy /S /I "D:\Source\*.*" "D:\Destination"
or robocopy:
robocopy /S "D:\Source" "D:\Destination" "*.*"
I'm kind of struggling with the following search.
I need to look for files which are misplaced in wrong folders. I have the following testing structure (in reality it is hudreds of folders):
C:\test2\John\John_phone.txt
C:\test2\Mary\John_address.txt
C:\test2\Mary\Mary_address.txt
C:\test2\Mary\Mary_phone.txt
C:\test2\Mary\Peter_address.txt
The files John_address.txt and Peter_address.txt are misplaced in Mary's folder. I want to check Mary's folder for any misplaced files and list them in a separate log file. So for the example above the log would contain the names (paths) of the two misplaced files, The deciding identifier is the person's name. I have this piece of code:
#echo off
cls
set /p name="Specify the name: "
::forfiles /p "%CD%\%name%" /s /m *.* /c "cmd /c echo #path">>log.txt
forfiles /p "%CD%\%name%" /s /m "| findstr /i /v "\*%name%*"" /c "cmd /c echo #path">>log.txt
pause
The commented line for forfiles works (lists all files in the folder), so I have an issue with findstr: ERROR: Files of type "| findstr /i /v *Mary*" not found.
The /v switch with findstr should find all files that do not contain the specified name, but obviously I'm doing something wrong while using it as input for forfiles.
I don't want to use the dir command since it lists additional information and I need to integrate the output into larger log file (I need to get only the path of the misplaced file).
Any help is appreciated.
Thanks.
Edit: Would it be easier to write the code if the correct file name would be fixed like this?
All_data_%NAME%_new.txt
The stuff before and after the name would be fixed and this format of the file name would be the only correct option, so anything else would have to be reported.
For a simple solution
#echo off
setlocal enableextensions disabledelayedexpansion
set "rootFolder=c:\test2"
( for /d %%z in ("%rootFolder%\*"
) do for %%y in ("%%~fz\*"
) do for /f "tokens=1 delims=_" %%a in ("%%~ny"
) do if /i not "%%~nxz"=="%%a" echo %%~fy
) > log.txt
This will iterate the files under each of the folders testing if their name starts with the same string that the folder name.
edited the original proposed solution does not work as intended, so, removed
edited - as the name of the file has not a fixed format, and the name of the folder can be in any place, this can be used
#echo off
setlocal enableextensions disabledelayedexpansion
set "rootFolder=c:\test2"
rem create a temporary file to hold the patterns to match against the list
rem of files to determine if it is correctly placed
set "patterns=%temp%\%~nx0.%random%.tmp"
> "%patterns%" (
for /d %%a in ("%rootFolder%\*") do echo(\\%%~nxa\\[^^\\]*%%~nxa[^^\\]*
)
rem Get the list of files, and filter to only get those that do not match
rem the generated patterns
dir /s /b /a-d "%rootFolder%\*" ^
| findstr /r /i /c:"%rootFolder:\=\\%\\[^\\]*\\." ^
| findstr /r /i /e /v /g:"%patterns%" ^
> log.txt
rem Patterns file is no longer needed
del "%patterns%" >nul 2>nul
this should help provided that comparison is case sensitive
for /f "delims=.'_\ tokens=2,3,*" %%a in ('forfiles /s /m *.txt /c "cmd /c echo #relpath"') do if NOT "%%a"=="%%b" #echo "%%a\%%b_%%c
So I have this code for sorting files within a complex of subdirectories within a main directory:
#ECHO OFF
SETLOCAL
SET relroot=g:\Pictures\Uda 18
(SET relroot=g:\Pictures\Uda 18)
SET "relroot=g:\Pictures\Uda 18"
SET "destdir=g:\Pictures\Uda 18\Sets"
:again
(SET artist=)
SET /p artist="Artist? "
IF NOT DEFINED artist GOTO :eof
MD "%destdir%\%artist%" 2>nul
FOR /f "delims=" %%i IN (
' dir /s /b /a-d "%relroot%\*%artist%*" '
) DO (
>>undo.txt ECHO %%i^|%destdir%\%artist%\%%~nxi
MOVE "%%i" "%destdir%\%artist%\%%~nxi" >nul)
)
GOTO again
It takes an input, searches the directory 'Uda 18' and everything within it, and moves all files with the input in their names to a folder named after the input under the directory 'Uda 18/Sets'. However, careless testing has caused me to draw files from 'Uda 18/Sets', the names of which mean they can't be reorganised using the batch. To avoid this, I need to exclude 'Uda 18/Sets' from where files are taken from, but I can't find a way. So as the titles asks; is it possible to skip a select subdirectory, and if so, how?
personally, I would prefer creatÃng a destination-directory BESIDES the source-directory, not within:
source: g:\pictures\uda 18\xxx
destination: g:\pictures\sorted\uda 18\xxx
This way you do not only avoid that problem, you will also allways have a clear and consistent directory-structure
I too would separate my destination folder from the folders to be processed.
Moving it out and moving it back later is an option though.
move "g:\Pictures\Uda 18\Sets" "g:\Pictures"
do your other commands here
move "g:\Pictures\Sets" "g:\Pictures\Uda 18"
FOR /f "delims=" %%i IN (
' dir /s /b /a-d "%relroot%\*%artist%*" ^|findstr /v /i /b /c:"%destdir%\\"'
) DO ( ECHO MOVE "%%i" "%destdir%\%artist%\%%~nxi")
Should list the MOVE to be performed /v EXCLUDING any directory /b beginning /i case-insensitive /c: the entire string including spaces ... where \ escapes \ so therefore excluding "g:\Pictures\Uda 18\Sets" and any of its subdirectories (where the full filename would begin g:\Pictures\Uda 18\Sets\"
simply remove the /v to select ONLY the matching files in "g:\Pictures\Uda 18\Sets" and its subdirectories.
Try this to draw the file names except those from the "sets" folder:
dir /b /s /a-d "uda 18" | find /v /i "sets"
... and with your folders:
echo "%destdir%\%artist%" | find /v /i "sets" >nul && goto:createFolder || goto:eof
I am very new to this. Please help me
I was trying to write a batch file program to count number of files in a folder and assign that to a variable and display it to verify that it has been stored
please help me with the syntax,
thank you in advance
-VK
I'm going to assume you do not want to count hidden or system files.
There are many ways to do this. All of the methods that I will show involve some form of the FOR command. There are many variations of the FOR command that look almost the same, but they behave very differently. It can be confusing for a beginner.
You can get help by typing HELP FOR or FOR /? from the command line. But that help is a bit cryptic if you are not used to reading it.
1) The DIR command lists the number of files in the directory. You can pipe the results of DIR to FIND to get the relevant line and then use FOR /F to parse the desired value from the line. The problem with this technique is the string you search for has to change depending on the language used by the operating system.
#echo off
for /f %%A in ('dir ^| find "File(s)"') do set cnt=%%A
echo File count = %cnt%
2) You can use DIR /B /A-D-H-S to list the non-hidden/non-system files without other info, pipe the result to FIND to count the number of files, and use FOR /F to read the result.
#echo off
for /f %%A in ('dir /a-d-s-h /b ^| find /v /c ""') do set cnt=%%A
echo File count = %cnt%
3) You can use a simple FOR to enumerate all the files and SET /A to increment a counter for each file found.
#echo off
set cnt=0
for %%A in (*) do set /a cnt+=1
echo File count = %cnt%
#echo off
setlocal enableextensions
set count=0
for %%x in (*.txt) do set /a count+=1
echo %count%
endlocal
pause
This is the best.... your variable is: %count%
NOTE: you can change (*.txt) to any other file extension to count other files.....
The mugume david answer fails on an empty folder; Count is 1 instead of a 0 when looking for a pattern rather than all files. For example *.xml
This works for me:
attrib.exe /s ./*.xml | find /v "File not found - " | find /c /v ""
This might be a bit faster:
dir /A:-D /B *.* 2>nul | find /c /v ""
`/A:-D` - filters out only non directory items (files)
`/B` - prints only file names (no need a full path request)
`*.*` - can filters out specific file names (currently - all)
`2>nul` - suppresses all error lines output to does not count them
To build for statement you should know some details at first.
for /F %%i in ('dir /A:-D /B *.* 2^>nul ^| find /c /v ""') do set "COUNT=%%i"
The example above would work, but if you want to copy paste it into another for-expression - might not.
The for /F ... expression by default has ; character as EOL character and space+tabulation characters as line separators.
If you use a file path as input in for-expression, then you can override these characters:
for /F "eol= delims=" %%i in (";My file path") do echo.Value: %%i
Where the end of eol= might not visible here. It is just a file path invalid not printable character, in this case - code 04. In most consoles and editors (except stackoverflow itself) you can type it as:
press ALT
type 04 from numeric keypad
release ALT
Another issue avoid here is always reset variable you want to use before the for-expression in case if for-expression is not executed:
set FILE=undefined
for /F %%i in (";My file path") do set "FILE=%%i"
echo.FILE=%FILE%
The fastest code for counting files with ANY attributes in folder %FOLDER% and its subfolders is the following. The code is for script in a command script (batch) file.
#for /f %%a in ('2^>nul dir "%FOLDER%" /a-d/b/-o/-p/s^|find /v /c ""') do set n=%%a
#echo Total files: %n%.
Change into the directory and;
attrib.exe /s ./*.* |find /c /v ""
EDIT
I presumed that would be simple to discover. use
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "batchfile.bat";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
I run this and the variable output was holding this
D:\VSS\USSD V3.0\WTU.USSD\USSDConsole\bin\Debug>attrib.exe /s ./*.* | find /c /v "" 13
where 13 is the file count. It should solve the issue
for /F "tokens=1" %a in ('dir ^| findstr "File(s)"') do echo %a
Result:
C:\MyDir> for /F "tokens=1" %a in ('dir ^| findstr "File(s)"') do #set FILE_COUNT=%a
C:\MyDir> echo %FILE_COUNT%
4 // <== There's your answer
FOR /f "delims=" %%i IN ('attrib.exe ./*.* ^| find /v "File not found - " ^| find /c /v ""') DO SET myVar=%%i
ECHO %myVar%
This is based on the (much) earlier post that points out that the count would be wrong for an empty directory if you use DIR rather than attrib.exe.
For anyone else who got stuck on the syntax for putting the command in a FOR loop, enclose the command in single quotes (assuming it doesn't contain them) and escape pipes with ^.
I have used a temporary file to do this in the past, like this below.
DIR /B *.DAT | FIND.EXE /C /V "" > COUNT.TXT
FOR /F "tokens=1" %%f IN (COUNT.TXT) DO (
IF NOT %%f==6 SET _MSG=File count is %%f, and 6 were expected. & DEL COUNT.TXT & ECHO #### ERROR - FILE COUNT WAS %%f AND 6 WERE EXPECTED. #### >> %_LOGFILE% & GOTO SENDMAIL
)
With a for loop:
FOR /F %i IN ('dir /b /a-d "%cd%" ^| find /v /c "?"') DO set /a count=%i
echo %count%
Without/avoiding for loop:
(dir /b /a-d ^| find /v /c "?") | (set /p myVar=& cmd /c exit /b %myVar%)
set count=%errorlevel%
echo %count%
Tested in Win 10 cmd
Solution
Requires enabling Windows Subsystems For Linux
Ensure directoryToCount is set to the respective directory
SET directoryToCount=C:\Users\
dir %directoryToCount% > contentsOfDir.txt
echo cat contentsOfDir.txt ^| grep File > countFiles.sh
sh countFiles.sh
del countFiles.sh
del contentsOfDir.txt
Explanation
In both bash and batch environments, the output of a command can be redirected to a file using the > operator.
For example, echo hello world > myFile.txt will produce a file named myFile.txt with hello world as its text.
In a bash environment, one can cat the contents of a File and grep a particular line from the file containing a specified pattern.
For example, cat myFile.txt | grep hello will return lines containing hello
If Windows Subsystems for Linux is enabled, then one can execute sh from a Command Prompt to access a linux-like environment.
Therefore, we can solve this by doing the following
Use dir to acquire a list of files in the directory, as well as the number of files
Redirect the output of dir to a file (perhaps named contentsOfDir.txt).
Create a .sh file to grep for File from contentsOfDir.txt
Call the .sh file from command prompt to invoke the grep
Delete the .sh file
Delete contentsOfDir.txt