Batch file %%i was unexpected at this time - variables

I am trying to get a patch file to set a variable to the number of instances that are running for a particular executable but am getting this error:
%%i was unexpected at this time
Here is my code:
for /f %%i in ('wmic process where name="chrome.exe" ^| find "chrome.exe" /c') do set var=%%i
I looked for an answer but none of the suggestions worked, even escaping the | with a ^.

If you are executing this directly in command prompt try this:
for /f %i in ('wmic process where "name='chrome.exe'" get caption /format:value ^| find "chrome.exe" /c') do set var=%i
for batch file left the double %

Related

Batch scripting for executing SQL scripts in a directory

I want to develop a batch script which will execute each .sql SQL scripts present in the folder where the batch file is placed to and record the logs to a filename_sqloutput.txt file .
The condition is : If any script gives any error message like column name incorrect , or table name etc. The script execution should stop immediately and further scripts should not executed .
I tried with the below code: But its not working as even if the script is giving errors in the output file . The script execution is not stopping ..
Need your help !!!
#echo off
for /f %%a IN ('dir /b *.sql') do (call sqlcmd -S AMRVSP000000318 -i %%a -o"%%~na_sqloutput.txt"
findstr "Msg" %%~na_sqloutput.txt >nul & if %errorlevel% EQU 1 (exit) else (echo Successfully executed %%~na_sqloutput.txt)
)
pause
FINDSTR will set %ERRORLEVEL% as follows:
0 (False) a match is found in at least one line of at least one file.
1 (True) if a match is not found in any line of any file, (or if the file is not found at all).
2 Wrong syntax
An invalid switch will only print an error message in error stream.
Moreover, follow EnableDelayedExpansion article and rewrite the script to more readable structure:
#echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
for /f %%a IN ('dir /b *.sql') do (
call sqlcmd -S AMRVSP000000318 -i %%a -o"%%~na_sqloutput.txt"
findstr "Msg" "%%~na_sqloutput.txt" 2>NUL
if !errorlevel! EQU 0 (
echo error occured %%~na_sqloutput.txt
rem pause to see error
pause
exit /B
) else (
echo Successfully executed %%~na_sqloutput.txt
)
)
pause
.... if errorlevel 1 (exit...
%errorlevel% is the initial value of errorlevel as it stands when the for is encountered.
See endless SO articles about using delayed expansion as another solution.
This syntax means "if the current errorlevel is 1 or greater dothis else dothat"

Batch file to zip Apache Tomcat for windows file names that do not equal to today

We have a number of Apache Tomcat for Windows logs - for example..
server.log.2014-02-04-10
server.log.2014-02-04-11
server.log.2014-02-04-12
server.log.2014-02-05-13
server.log.2014-02-05-14
server.log.2014-02-05-15
These are defined with a file extension of .YYYY-MM-DD-HH and by default Apache has them by date: .YYYY-MM-DD however, we have to define the HH breakover because of system usage and other reasons.
I am trying to do something very simple..
If the file extension does not equal today, to archive (zip) it up..
Here is what I have and no matter what I do is that it shows: 2014-02-05 even though the script is showing .2014-02-04.
Here is the script and I am not sure if I need SETLOCAL enabledelayexpansion..
rem http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
SETLOCAL enabledelayedexpansion
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=!MyDate:~0,4!-!MyDate:~4,2!-!MyDate:~6,2!
rem today=%MyDate:~0,8%
for %%i in (D:\11\*.*) do (
SET FILETIME=%%~xi
rem SET Dt=!FILETIME:~0,11!
rem IF NOT "!fldt!" == ".!today!" (
rem echo %%~ni%%~xi
rem )
)
pause
Thanks
This filters the filenames by the date string in the name and
will echo the filenames that do not have todays date in them.
#echo off
rem http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
SETLOCAL enabledelayedexpansion
for /f %%x in ('wmic os get localdatetime ^|find "." ') do set "MyDate=%%x"
set "today=!MyDate:~0,4!-!MyDate:~4,2!-!MyDate:~6,2!"
rem set "today=%MyDate:~0,8%"
for /f "delims=" %%a in ('dir "D:\11\*.*" /b ^|find /v ".%today%-" ') do (
echo "%%~fa"
)
pause

Batch Help -- Move File To Directory, If Exist Filename, Rename 001

I have a batch file that checks a root directory every 10 seconds for PDF files and copies those files to their corresponding subfolders of the exact same name.
What I'm missing is that I need my batch file to check the target subdirectory for the named file, and if the named file exists, to rename the new file to be copied to !filename!001.pdf and have the 001 get incremented as duplicate file names are copied. Here's what I got:
:loop
setlocal enabledelayedexpansion
cls
pushd c:\files\
for /f "tokens=*" %%1 in ('dir /a-d /b c:\files\*.pdf') do (
set filename=%%~n1&set dirname=!filename:~0,7!
dir c:\files /b *.pdf > location1list.tmp
for /f %%a in (location1list.tmp) do dir c:\files\%%a > location2list.tmp
if not exist c:\files\!dirname! (md c:\files\!dirname!)
move %%1 c:\files\!dirname!\>nul
)
timeout /t 10
goto:loop
Any suggestions how I can get the files renamed?
I.e. if Bob.pdf exists in the Bob folder and another Bob.pdf is added to the C:\files folder, I want it to be copied to the C:\files\bob folder as Bob001.pdf.
Something like this should do what you want. May need some tweaking. Also I have not tested it, just wrote it from memory. Need any explanations, just ask.
Script
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd C:\files
:Main
for /f "tokens=*" %%A in ('dir /a-d /b *.pdf') do (
if not exist "%%~dpnA\*" md "%%~nA"
set "File=%%~dpnA\%%~nxA"
if exist "!File!" call :Name "!File!" File
echo !File!
move "%%~fA" "!File!"
)
goto Wait
:Name <Target> <Variable>
set "Count=0"
:Count
set /a "Count+=1"
set "Number=00%Count%"
if exist "%~dpn1%Number:~-3%%~x1" goto Count
set "%~2=%~dpn1%Number:~-3%%~x1"
goto :eof
:Wait
timeout /t 10
goto Main
:End
popd
endlocal
Limitations
Each target subdirectory can only handle a maximum of 1000 files as this is written. If the subdirectory hits this maximum then the script will get stuck in an infinite loop.
Update
Fixed an error in my initial script

Batch file that returns folder size

I'm having space issues on my Vista machine and need to figure out what's taking up so much space.
I would like to write a simple batch file that returns all folders under C: and the size of each folder.
The dir command doesn't appear to return folder size.
Unfortunately we don't have admin rights and can't install a third party application and we have other users in our group that also need this information.
I'd have a look at this thread for some clues as to how to achieve the directory size:
Batch File To Display Directory Size
Otherwise:
dirsize:
#echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
#echo %CD%:!sum! k
AllDirSize:
echo off
set WORKING_DIRECTORY=%cd%
for /f "delims=" %%a in ('dir /a:D /D /B /S') do (
echo off
cd %%a
"%WORKING_DIRECTORY%"\dirsize "%%a"
cd %WORKING_DIRECTORY%
)
Use it: ALLDIRSIZE > C:\temp\FileContainingFolderSizes.txt
Which is taken from the excellent Richard Bishop testing forums: http://www.bish.co.uk/forum/index.php?topic=58.0
Not exactly answering your question, but if you have GUI access I'd suggest using TreeSize:
http://www.jam-software.com/freeware/index.shtml
If you prefer command line use du command from Unix utils:
http://unxutils.sourceforge.net/

How to make for loop in windows batch file run in name order

I have a windows batch file that does this:
for %%s in (*.sql) do call
It loops through all the sql script in a folder.
In the folder the file names are like:
s4.06.01.sql
s4.07.01.sql
s4.08.01.sql
s4.10.01.sql
s5.01.sql
But the for loop goes through the files randomly (not in the name order), it first run s5.01, then s4.06, then s4.08, then s4.10, then s4.07. How can I make them run in the name order?
It used to work, but now it doesn't. What may cause this problem?
Jerry's answer might very well be what is causing the problem.
You might solve it by changing
for %%s in (*.sql) do call
to
for %%s in (dir"*.sql ^| sort) do call
Edit cudo's to LonelyPixel
The posted solution does not work as is on a Windows 8 machine (perhaps it did back then with Windows XP but I have no idea).
Following is a working solution using Windows 8 command prompt.
for /f "tokens=*" %%s in ('dir /b *.sql ^| sort') do echo %%s
If memory serves, it will operate on the files in the order they're returned by the file system. As such, if you run it on a disk partition that's formatted with NTFS, the names will be returned in sorted order so they'll be processed in sorted order. If the disk partition is formatted with something like FAT or FAT32, the names will be retrieved (and processed) in a more or less random order.
I too had to deal with spaces in the filenames, so I added quotes around the input variable like so:
for /f "tokens=*" %%s in ('dir /b *.sql ^| sort') do echo "%%s"
In my case it was SQLCMD, with the scripts in a subdirectory, like so:
SET "machineName=localhost"
for /f "tokens=*" %%f in ('dir /b PostDeployScripts\*.sql ^| sort') do sqlcmd -E -S%machineName% -i"PostDeployScripts\%%f"