Windows script to check directory for file - windows

Trying to do a bit of scripting to run on a windows server. The aim of the code is to check a given directory F:\TestFolder for the arrival of 1 or more files that start with IB30321* in the name.
If the file(s) have not arrived in the given directory then the script sleeps for 5mins before checking again. Once the correct file(s) have been found it exits the script.
Unfortunatly i'm getting the following error, any ideas what i can do to fix this?
FINDSTR: Bad command line
Incorrect number of files found, 1 file expected
#Echo off
cd /D "%F:\TestFolder%"
Set numfiles=0
For /f "tokens=1,* delims=:" %%A in (
'Dir /B "IB30321*" ^| findstr /n ^ '
) DO Set numfiles=%%a&Set filename=%%B
If %numfiles% equ 1 (
echo %filename% found
exit /B 0
) else (
echo "Incorrect number of files found, 1 file expected"
set numfiles=0
Timeout /T 300
)

To elaborate a bit more on my comment.
dir outputs an error message if no matching file(s) found.
findstr doesn't care what lines it counts.
suppressing error output on no find will have the do part not executed as there isn't any output.
#Echo off
cd /D "%F:\TestFolder%"
:Loop
Set numfiles=0
For /f "tokens=1,* delims=:" %%A in (
'Dir /B "IB30321*" 2^>NUL ^| findstr /n ^ '
) DO Set numfiles=%%a&Set filename=%%B
If %numfiles% equ 1 (
echo %filename% found
exit /B 0
) else (
echo "Incorrect number of files found, 1 file expected"
Timeout /T 300
)
Goto :Loop

Related

Windows batch: combining start and FOR /f "tokens=1" with | escape coded symbols

I am trying to execute this command from standard windows batch file.
start /B for /f "tokens=1" %%a in ('Query SESSION ^| find /i "rdp"') do (echo yes |reset session %%a)
and it throws an error:
"| was unexpected at this time."
I just found that it fails because start in the beginning.
What's wrong with it?
From a batch file it's a bad idea to try to build a valid one line solution with start, it's very tricky to escape/quote a complex expression.
But it's quiet easy to use start and jump to a label in the same batch file.
#echo off
REM *** Thread redirector
for /F "tokens=3 delims=:" %%F in ("%~0") do goto %%F
REM *** Start a new thread in this batch file at the label :myThread
start "" /b cmd /c "%~d0\:myThread:\..\%~pnx0"
echo Main
for /L %%n in (1 1 10) DO (
echo MainThread doing some stuff %%n
ping localhost -n 2 > nul
)
exit /b
:myThread
for /f "tokens=1" %%a in ('Query SESSION ^| find /i "rdp"') do (
echo myThread: Resetting session %%a
echo yes | reset session %%a
)
exit /b

Batch file how to use findstr command to find a text and make it a variable?

I want to know how to use Findstr command to find a text in a file and make it a variable here what i tried :
#echo off
for /F "delims=" %%a in ('findstr /I /m "100" Config.dat') do set "Variable=%%a"
cls
if %errorlevel% == 0 (
goto found
) else (
goto nope
)
:found
cls
echo founded ! %Variable%
pause
exit
:nope
cls
echo not found!
pause
exit
Ok i explain : In the 2nd line the number "100" is what i want to find and the "Config.dat" is the file that have in it the number 100 and some other numbers and the "Variable" in there is the name of the variable that i want to store in it 100.
The problem is when it founded number 100 it goes to the function "found" and displays "Founded! 100" but when it not founded it also goes to "found" function and only display founded! without 100. So why when it didn't founded it it goes to "found" i need it to go to "nope".
So i hope you guys explain to me if i did something wrong and thanks!
This is because for /F calls command in a standalone cmd.exe process and does not return the error level into context of the caller:
#echo off
rem drop last error level
type nul>nul
for /F "usebackq delims=" %%a in (`cmd.exe /C #exit /b 123`) do rem
echo ERRORLEVEL=%ERRORLEVEL%
-
ERRORLEVEL=0
If you want just load config values into environment variables, then there is no need to search anything. Just create standalone configuration file for that.
config.vars
# loads by cmd.exe script
aaa=111
"bbb=111 222"
/A ccc=1+1
"ddd=%bbb% & 333"
load_config.bat
#echo off
for /F "usebackq eol=# tokens=* delims=" %%i in ("config.vars") do (
call set %%i
)

Batch echo specific line

This is my first post and I am not very skilled with batch so sorry if I really mess up.
Basically I'm working on a little batch script where once run, the user inputs a file path and line number and the specified line of the specified file will be output to the command line. I have all my variables and commands working, and the command to specify the line of the text file works fine, its just when I put my variables in it doesn't work. Now I'm guessing what I'm doing is obviously wrong since I'm new to batch, but anyway here's my code:
#echo off
color b
:top
title specified line copy tool
echo input full path to txt file
set /P filepath=">"
cls
echo what line would you like to copy?
set /P lineoriginal=">"
set /A actualline=%lineoriginal%-1
for /F "skip=%actualline% delims=" %%i in (%filepath%) do if not defined output set "output=%%i"
echo %output%
pause
See if you can see what I did wrong, thanks.
From this link Windows Batch file to echo a specific line number
you can call this function like that : Call:ReadNthLine <File> <nLine>
:ReadNthLine File nLine
FOR /F %%A IN ('^<"%~1" FIND /C /V ""') DO IF %2 GTR %%A (ECHO Error: No such line %2. 1>&2 & EXIT /b 1)
FOR /F "tokens=1* delims=]" %%A IN ('^<"%~1" FIND /N /V "" ^| FINDSTR /B /C:"[%2]"') DO ECHO.%%B
EXIT /b
And your code can be re-written like that :
#echo off
color b
:top
cls
title specified line copy tool
echo input full path to txt file
set /P "filepath=>"
cls
echo what line would you like to copy ?
set /P "nLine=>"
cls
CALL :ReadNthLine "%filepath%" %nLine%
PAUSE >NUL & goto:top
GOTO :EOF
::***************************************************************************************
:ReadNthLine File nLine
FOR /F %%A IN ('^<"%~1" FIND /C /V ""') DO IF %2 GTR %%A (ECHO Error: No such line %2. 1>&2 & EXIT /b 1)
FOR /F "tokens=1* delims=]" %%A IN ('^<"%~1" FIND /N /V "" ^| FINDSTR /B /C:"[%2]"') DO ECHO.%%B
EXIT /b
::***************************************************************************************

Method for verifying xcopy has copied all files

After I copy an entire directory using xcopy, I want to verify that all the files got copied across as it often seems to fail. I'm trying to do it by looping through each file in the source directory and then checking it exists in the destination directory.
I have something that seems to work on my machine but doesn't seem to on the build machine, which is where I need it.
#echo off
set /a count=0
set /a count2=0
for /r "C:\work\DLS\built\Android_EU\data" %%f in (*) do (set /a count+=1
)
for /r "C:\work\DLS\TegraProject\DLS\assets" %%f in (*) do (set /a count2+=1
)
echo count is %count%
echo count 2 is %count2%
if %count%==%count2% echo equal
if not %count%==%count2% echo not equal
pause
if %count%==%count2% exit 0
if not %count%==%count2% exit 1
I checked manually and there are 594 files in both folders on both machines.
It's reported correctly on my machine.
On the build machine, it says there are 583 and 584 files. Is there any reason that would skip files?
Thanks,
Chris.
OK, so this seems to work- thanks for your input #foxidrive.
cd C:\work\DLS\built\Android_EU\data
dir /s /b /a-d |find /c /v "" > temp.txt
set /p count=<temp.txt
cd C:\work\DLS\TegraProject\DLS\assets\data
dir /s /b /a-d |find /c /v "" > temp.txt
set /p count2=<temp.txt
echo count is %count%
echo count 2 is %count2%
pause
if %count%==%count2% exit 0
if not %count%==%count2% exit 1
Try this: it will work up to 2^31 -1 files. :)
#echo off
set "count="
set "count2="
for /f %%a in (' dir "C:\work\DLS\built\Android_EU\data" /s /b /a-d ^|find /c /v "" ') do set count=%%a
for /f %%a in (' dir "C:\work\DLS\TegraProject\DLS\assets" /s /b /a-d ^|find /c /v "" ') do set count2=%%a
echo count is %count%
echo count 2 is %count2%
if %count% EQU %count2% echo equal
if %count% NEQ %count2% echo not equal
pause
if %count% EQU %count2% exit 0
if not %count% NEQ %count2% exit 1

Check folder is empty using batch command?

for /F %%i in ('dir /b "C:\Program Files\Apache folder\*.*"') do (
echo Folder is NON empty
goto launch_app
)
How to check a folder is empty?
I tried above command, but it didn't work.
try this:
for /F %%i in ('dir /b /a "C:\Program Files\Apache folder\*"') do (
echo if you see this the folder is NOT empty
goto launch_app
)
File Not Found
#for /f "tokens=*" %%a in ('dir /b /a-d "C:\Progra~1\Apache"') do #...
The error that you see when you run this command, comes for the standard error output. But that is only a warning printed to your console. When this case happens, the body of the iteration won't be evaluated, and the algorithm based on this "for/dir" instruction, is in fact correct.
Now, if you want to get rid of this ugly error message, you need to add the following to your script, in order to redirect the standard error to null device:
2>NUL
so for instance, in a batch file, with the appropriate escape character:
#rem Print a list of file paths
#for /f "tokens=*" %%a in ('dir /b /a-d "%srcPath%" 2^>NUL') do #echo(%srcPath%\%%a
#echo off
cls
echo.
echo.
echo.
echo TEST FOR EMPTY DIRECTORY
echo was tested with "&" without a file
echo.
echo Can use Full or Subfolder path
echo (as below) of where test.bat
echo.
set p=Raw\
echo.
echo Just to show p is set
echo.
echo "p=%p%"
echo.
echo.
echo.
pause
for /F %%i in ('dir /b /a "%p%*"') do (
echo Folder %p% was NOT empty
goto :process
)
echo Folder %p% was empty
:process
echo "BLAH, BLAH, BLAH "
:end
pause
exit
rem Copy past in notepad to try. "Create and use or change the "raw" to your own fancy."
rem Hope this helps. Works great for me and I use it.
rem Have Fun.
rem Note use go to end next line after was empty. Worked great in w10. (this program hmm)
rem IF IT WORKS FOR YOU . GIVE A +. THX!
I wasn't satisfied with the given answers because I try to avoid for loops, goto and temp files whenever I can.
To check if the folder is empty:
dir /b /s /a "C:\Program Files\Apache folder\" | findstr .>nul || (
echo Folder is empty
)
To check if folder is not empty:
dir /b /s /a "C:\Program Files\Apache folder\" | findstr .>nul && (
echo Folder is NOT empty
)
dir C:\TEST\*.* /a/b/d/od>C:\TEMP\CHECKFOLDER
for /R C:\TEMP\ %%? in (CHECKFOLDER) do (
if "%%~z?"=="0" (
ECHO Folder is empty.
) ELSE (
ECHO Folder is NOT empty
)
)

Resources