For loop in batch file dies calling Windows conversion tool - parsing

ParseRat is an old program that I have had a long time. I need to transpose text files in c:\wherefilesare.
I run the below and it just dies:
echo on
setlocal EnableDelayedExpansion
cd c:\wherefilesare
SET progdir=C:\program files (x86)\ParseRat
for%%x in (*.txt) do ("%%progdir%\parserat.exe" "%%x.txt" "%%progdir%\test.prz" "%%x.csv"

As #Squashman says:
"You (have) an extra percent symbol for all your (environment) variables. You are also not using the FOR meta-variable correctly. Essentially %%x expands to the actual file name with the extension. So it will see file1.txt.txt and file1.txt.csv. You need to use the command modifiers. %%~nx to get just the file name without the extension."
Also, you are missing a closing parenthesis...
echo on
setlocal EnableDelayedExpansion
cd c:\wherefilesare
SET progdir=C:\program files (x86)\ParseRat
for %%x in (*.txt) do (
"%progdir%\parserat.exe" "%%~nx.txt" "%progdir%\test.prz" "%%~nx.csv"
)

Related

Jenkins EnvInject plugin - Environment variable value containing multiple lines

I followed the suggestions from this post on how to use the EnvInject plugin to create and set Jenkins environment variables. I'm using the "Inject environment variables" in post build step and set "Properties File Path"
The windows batch script create a environment variable OPS and writes it to a property file : results.txt which contains multiple lines , like :
OPS= This is line one,
This is two
This is three
Challenge: OPS picks up only the first line from results.txt and skips the rest of the lines.
How do I set OPS have all the lines as its value ?
cd C:\To\Test\Class\Path
java utilities.LogExtractor>ops.txt
#echo off
setlocal EnableDelayedExpansion
set LF=^
rem *** Two empty lines are required for the linefeed
FOR /F "delims=" %%a in (ops.txt) do (
set var=!var!!LF!%%a
)
set var=!var!!LF!
echo OPS=!var! > %JENKINS_HOME%\jobs\%JOB_NAME%\builds\%BUILD_NUMBER%\results.txt
and I set the "Properties File Path" to %JENKINS_HOME%\jobs\%JOB_NAME%\builds\%BUILD_NUMBER%\results.txt
From the source code, I'd say it uses java.util.Properties to load the file, calling the load method. The documentation says you can escape a line break with a backslash, so using
OPS= This is line one,\
This is two\
This is three
should be sufficient. (Be careful, whitespace at the beginning of the line is ommitted.)

how to escape empty lines from end of command result

i use the following command to create a variable from my CPU Name:
#echo off
Rem create variable from cpu name
for /f "useback tokens=* skip=1" %%g in (`wmic cpu get name ^|findstr /i "."`) do (
set CPU_NAME=%%g
echo %CPU_NAME%
)
but the result is nothing, because there is some empty lines at end of "wmic cpu get name" command result and remove created variable
how can i solve it?
thanks a lot
Please search SO for delayed expansion.
call echo %%CPU_NAME%%
should show you the required data. This is one of several well-documented solutions.
There are some empty lines at the end of wmic cpu get name
Use findstr as follows to strip blank lines from the wmic output. You also need to use delayed expansion
Corrected batch file (test.cmd):
#echo off
setlocal enabledelayedexpansion
Rem create variable from cpu name
for /f "useback tokens=* skip=1" %%g in (`wmic cpu get name ^| findstr /r /v "^$"`) do (
set CPU_NAME=%%g
echo !CPU_NAME!
)
endlocal
Example usage:
> test
Intel(R) Core(TM) i5-2410M CPU # 2.30GHz
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
findstr - Search for strings in files.
wmic - Windows Management Instrumentation Command.
There is no need to echo the result within the for loop in your case because you are only setting a single name. Just echo it later.
#Echo Off
For /F "Skip=1 Delims=" %%A In ('WMIC CPU Get Name'
) Do For /F "Delims=" %%B In ("%%A") Do Set "CPU_NAME=%%B"
Echo=%CPU_NAME%
The second For loop is intended to remove the unwanted 'empty lines' you reported.

How to get relative path from current folder?

This is what i currently get with FOR loop:
FOR /R "ProgramFolder" %%P in (*) do (
echo %%P
)
C:\Folder\Folder2\ProgramFolder\Managed\bolt.dll
C:\Folder\Folder2\ProgramFolder\Resources\fmodstudio.dll
C:\Folder\Folder2\ProgramFolder\Plugins\dll\DynamicWaterNativeWrapper.dll
How to get pathes like this?
Managed\bolt.dll
Resources\fmodstudio.dll
Plugins\dll\DynamicWaterNativeWrapper.dll
edited on dbenham comments
#echo off
setlocal enableextensions disabledelayedexpansion
pushd c:\somewhere\ProgramFolder
for /f "tokens=1,* delims=\" %%a in (
'xcopy . "%temp%" /l /s'
) do if not "%%b"=="" echo(%%b
popd
The basic idea behind this code is to use the xcopy command not to copy, but to retrieve a list (/l) of the files that should be processed with relative paths. For it to work, it is necessary to first change the current active directory to the required one (pushd) and use a relative reference to the current folder (.)
The output of xcopy command with this configuration will be in the form
.\folder\folder\file.ext
To remove the prefixing dot and backslash the for /f is configured to use the slashes as delimiters and to retrieve the first token (the dot) before the first backslash and the rest of the line as the second token, that is, the relative paths
As the xcopy command output includes an aditional line with the total number of files, and this line will result in an aditional blank line in the output of the script, an aditional if is included to discard this line.

Noobs approach to automate x264 cmd

so here is my script to loop through specific video extensions » add a manual profile » generate necessary *.bat & finally a final 'loader' batch file to execute previous *.bat files sequentially & necessary logging (this gives quiet a deal of freedom if you so want)
::==
:: gets lines into vars c1 v2 v...
#echo off
:: user input required
cd /d "d:\Trainers\out\"
setLocal EnableDelayedExpansion
dir /B /O:N | findstr ".wmv$ " >filename.txt
echo. >log.txt
:: user input required
for /f "tokens=* delims= " %%a in ('type filename.txt ^|findstr ".wmv$"') do (
set /a n+=1
echo. >file!n!.bat
set in=%in%%%a
:: user input required
set out=!in:.wmv=.mp4!
:: user input required
set v=x264 --crf 23 --level 3.1 --tune film -o "d:\Trainers\out\!in!" "d:\Trainers\out\!out!"
echo. !v!>file!n!.bat
)
dir /B /O:N | findstr ".bat$ " >x264_home.txt
for /f "tokens=* delims= " %%a in (x264_home.txt) do (
set /a n+=1
:: mtee is an external library Google it
set "z=call %%a | mtee /d/c/t/+ log.txt"
echo. !z! >> x264_home.bat
)
echo. #echo off > newFile.bat
type x264_home.bat >> newFile.bat
type newFile.bat > x264_home.bat
del newFile.bat,x264_home.txt,filename.txt
echo. pause >> x264_home.bat
echo. #echo All Operation done... >> x264_home.bat
:: user input required
move "d:\Trainers\out\*.bat" "d:\Program Files\x264_auto\test\"
:: user input required
move "d:\Trainers\out\log.txt" "d:\Program Files\x264_auto\test\"
::==
Now the above code which is fairly easy to understand (bcz its written by a noob) run perfectly & create necessary files. For instance one of the file1.bat looks like this:
x264 --crf 23 --level 3.1 --tune film --preset veryslow --deblock -2:-1 --zones 24233,25324,q=20 --acodec aac --abitrate 80 -o "d:\Trainers\out\1.wmv" "d:\Trainers\out\1.mp4"
...& the loader .bat file looks like
#echo off
call file1.bat | mtee /d/c/t/+ log.txt
call file2.bat | mtee /d/c/t/+ log.txt
call file3.bat | mtee /d/c/t/+ log.txt
#echo All Operation done...
You see this is a quiet flexible approach in that you can use special filestr » set another loop » set another profile. Furthermore every batch file can be latter edited especiialy when you heavily use --zone x264 feature
I am successful because there is no error in any output ...but its the x264.exe (provider/compiler x264GUI) throws error which it otherwise don't?
d:\Program Files\x264_auto\test>x264 --crf 23 --level 3.1 --tune film --preset
veryslow --deblock -2:-1 --zones 24233,25324,q=20 --acodec aac --abitrate 80 -o
"d:\Trainers\out\1.wmv" "d:\Trainers\out\1.mp4"
ffms [error]: could not create index
lavf [error]: could not open input file
raw [error]: raw input requires a resolution.
x264 [error]: could not open input file `d:\Trainers\out\1.mp4' via any method!
its the x264 thats the culprit perhaps a senior guide is required here
Is your x264 compiled with mp4 input support? (I believe that needs lavc/lavformat, just download precompiled x264 from x264.nl which has all extras)
Do you get the same error if you run the same command directly? (not through bat files)
If yes, does it only happen when you use zones? (if it does, then post an example of your command line as x264 bug to x264-devel mailing list)
If no, are you sure you are running the exact same x264? (perhaps there are several in different places on your system)
I recommend doing what you're doing either (a) in python with subprocess.call(...) or (b) in cygwin/bash/shell script, or . bat files are pretty much the wrong answer to any problem :) The nice thing about either of those two is that they have simple, regular escaping rules for program arguments.

Windows cmd - findstr and gawk or how would you pull the below data out

I was wondering if it's possible to use findstr and/or gawk to return the output of a windows cmd exactly how I need it to be. I'm currently returning the output raw, then stripping out the blank lines, and parsing out what I need. Just as a learning thing, I was hoping I could see how this can be done better.
The raw output:
Change 2086888 on 2012/01/23 by user1#server1
test_description_1.2.3.4#29816
Change 2086888 on 2012/01/23 by user1#server2
test_description2_4.5.6.7#29816
Change 2078677 on 2012/01/20 by user2#server1
test_description3_7.8.9.10#29816
I take that output and parse it out to this with php:
1. 2086888,test_description_1.2.3.4#29816
2. 2086888,test_description2_4.5.6.7#29816
3. 2078677,test_description3_7.8.9.10#29816
To make it a little easier on myself, I remove the blank lines from the output by piping it to findstr and using /V "^$". So it's | findstr /V "^$".
How can I get the output that I parse out with php directly from the command line?
for command in cmd.exe skips blank lines, so it may be easier to parse it this way:
#Echo Off
setlocal enabledelayedexpansion
set linenum=1
for /f "tokens=1,2" %%A in (output.txt) do #(
if %%A==Change set result=%%B
if not %%A==Change (
echo !linenum!. !result!,%%A
set /a linenum=!linenum!+1
)
)
endlocal

Resources