running batch process over file named in unicode - batch-file

I need to run a process over file named in unicode ?
How using a batch file ?
I tried :
dir /b %folder% > dir.lst
::cmd /u /c dir /b %folder% > dir.lst
FOR /F "delims= tokens=*" %%G IN (dir.lst) DO (
echo %%G
#set filename2=%%G
#echo %filename2%
#set filename=%pathlogs%\!filename2!
#echo %filename%
#echo %filename2%
#%binaries%\gawk -v fn=%filename% -v fn2=%filename2% -f %scripts%\AlexBank_barcode.awk %folder%\%filename2% 2> %scripts%\AlexBank_barcode.fil
#%binaries%\sort -k 1.129,1.144 -T F:\tmp %pathlogs%\%filename2%.log > %pathlogs%\%filename2%.log.tmp 2> %scripts%\%filename2%".sort.fil"
#set filename3=%filename%.log.tmp1
#%binaries%\gawk -f %scripts%\AlexBank_collect_barcode.awk %pathlogs%\%filename2%.log.tmp 2> %scripts%\AlexBank_collect_barcode.fil
#set filename3=%filename%.separate_all.log
#%binaries%\gawk -f %scripts%\AlexBank_separate_barcode.awk %pathlogs%\%filename2%.log.tmp1 2> %scripts%\AlexBank_separate_barcode.fil
#set filename3=%filename%.separate_statement_le.awk.log
#%binaries%\gawk -v fn=%filename3% -f %scripts%\AlexBank_separate_statement_le.awk %pathlogs%\%filename2%.log.tmp1 2> %scripts%\%filename2%".AlexBank_separate_statement_le.awk.fil"
#set filename3=%filename%.separate_statement_gt.awk.log
#%binaries%\gawk -v fn=%filename3% -f %scripts%\AlexBank_separate_statement_gt.awk %pathlogs%\%filename2%.log.tmp1 2> %scripts%\%filename2%".AlexBank_separate_statement_gt.awk.fil"
)

You have two options here:
Set a TrueType font for your console window
Don't use for /f to iterate over the output of dir /b. It's unnecessary in the vast majority of cases, more complicated to get right, more brittle and fails on Unicode file names when the settings are slightly wrong as you experience. You can use for to iterate over files directly:
for %%F in (%folder%\*) do ...

Related

Find all .sql-Files in a Directory and execute them with sqlcmd

I'm still a CMD beginner and would like to import all SQL files I have in a directory structure with sqlcmd to my DB.
If I copy all *.sql into the root folder the following command works:
#ECHO ON
FOR %%G in (*.sql) DO sqlcmd /S *SERVER* /d *DB* /U *USER* /P *PW* -i"%%G" > LOG_lastrun.txt 2> LOG_errors.txt
pause
#ECHO ON
FOR /F "tokens=1 delims=sql" %%G in (*.sql) DO sqlcmd /S *SERVER* /d *DB* /U *USER* /P *PW* -i"%%G" > LOG_lastrun.txt 2> LOG_errors.txt
pause
Unfortunately, it doesn't work for me FOR /F loops. Can you help me here?
The FOR /F works differently. It runs a command. Jusr *.sql is not a command. Also, I suspect that you want to concatenate stdout and stderr output using >>.
#ECHO ON
SET "SERVER_NAME=*SERVER*"
SET "DB_NAME=*DB*"
SET "LOGIN_ID=*USER*"
SET "PASS=*PW*"
FOR /F "delims=" %%G IN ('DIR /B "*.sql"') DO (
sqlcmd -S %SERVER_NAME% -d %DB_NAME% -U %LOGIN_ID% -P %PASS% -i "%%~G" >> LOG_lastrun.txt 2>> LOG_errors.txt
)
pause

CMD search results send to console

I would like to ask if anyone has an idea how to send search result to console in single command line but with some specific code before and after the search results output
I need to run program Xconverter that is made to be operated by console, and has parameters:
-s :silent mode (only mode suported yet)
-i [files] : list location of the files in quotes: {-i "a.txt" "b.txt"..}
-t [tool file] : tool file location
-m 5 : conversion style
syntax accepted for example (single command for XConverter):
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
but I need about 500 -i [files] to list
so i created search that fill look for those .pgm in tree mode where .bat is located and adds search result (file locations) one after other
my code is working but the only way i made it to work is that i outputed echo result in to extra .bat file and started the file. Is there i way to reprogram my code that it will send these outpust in to single one line command?
echo off
SET mypath=%~dp0
::THIS STARTS PROGRAM
(
echo | set /p="""
echo | set /p="C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe" -s -i "
)>seznam_konvertovanych_suboru.bat
::THIS SEARCHERS FOR PGM FILES AND ADDS PATH OF EACH FILE TO THE LINE
for /f "delims=" %%A in ('forfiles /s /m *.pgm /c "cmd /c echo #relpath"') do (
set "file=%%~A"
setlocal enableDelayedExpansion
echo | set /p="""
echo | set /p="%mypath:~0,-1%\!file:~2!"
echo | set /p="" "
)>>seznam_konvertovanych_suboru.bat
::THIS ADDS SOME EXTRA FILE PATH TO THE LINE OF TOOL FILE, THAT IS NEEDED
(
echo | set /p=-t "Tlgx\def.tlgx" -m 5
)>>seznam_konvertovanych_suboru.bat
seznam_konvertovanych_suboru.bat
This code will output file seznam_konvertovanych_suboru.bat exactly in this format
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
Anyone knows how to reprogram this to send that output to console in single line without creating extra file to store that code?
Edited:
What i need is having START_CONVERTING.BAT file that will send to console one command :
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
but "C:\a.pgm" "C:\abc\b.pgm" are the search results of all .pgm files in that folder and child folders. So i will copy this START_CONVERTING.BAT anywehere where i store .pgm files. (some of them are in folders and those folders in other folders) so i will just copy STAR_CONVERTING.BAT in major folder that will thake care of all files with all files in child folders.
I will just click START_CONVERTING.BAT and it will convert those .pgm files. (without creating creating any other output to any other .bat file.
SOLVED (code)
#echo off & setlocal enableDelayedExpansion
PushD "%~dp0"
Set "XC=C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe"
Set "TOOLFILE=Tlgx\def.tlgx"
Set "Files="
:: Concatenate all files in one string
for /f "delims=" %%A in ('Dir /B /S *.pgm') do set Files=!Files! "%%~A"
:: command, command line length may be an issue
"%XC%" -s -i%Files% -t "%TOOLFILE%" -m 5
If I understand right this might work
#echo off & setlocal enableDelayedExpansion
PushD "%~dp0"
Set "XC=C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe"
Set "Files="
:: Concatenate all files in one string
for /f "delims=" %%A in ('Dir /B /S *.pgm') do set Files=!Files! "%%~A"
:: store command in batch file, command line length may be an issue
>"seznam_konvertovanych_suboru.bat" Echo "%XC%" -s -i %Files% -t "tool.tlgx" -m 5
try with that batch
Xconverter is for xxl to PGMX
1 set all path for converter
2 creat to do list of xxl files on desktop on this exemple or desktop sub folder!
3 Masetro can't be install as Office PC you must run it on CN PC!
#echo OFF
COLOR F1
TITLE "Convertor XXL to PGMX"
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º Converteur Xconverter º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo.
echo "Stop command with Ctrl+C"
echo.
Echo "Path SET"
TIMEOUT /t 5 >nul
Set todo="%userprofile%\desktop\todo.txt"
Set def="%PROGRAMFILES(x86)%\Scm Group\Maestro\Tlgx\def.tlgx"
Set Compil="%PROGRAMFILES(x86)%\Scm Group\Maestro\Xconverter.exe"
Set desktop="%userprofile%\desktop"
echo "To do list of *.XXL files"
TIMEOUT /T 10 >nul
cd %userprofile%\desktop
dir /a /b /o /s *.xxl > %todo%
echo "Files convert command"
TIMEOUT /T 10 >nul
for /f "delims=" %%a in (%todo%) do %Compil% ^ -s -m 4 ^
-i "%%a" ^
-t %def%
echo "Rapport files"
Timeout /T 10 >nul
cd %desktop%
dir /a /b /o /s *.pgmx > Rapport.txt
Echo "Delete to do list"
TIMEOUT /T 10 >nul
DEL %todo% /F /S /Q >nul
But haven't go detail of Convesion style -m X
if you can replie me detail of -m number it was great!
All the best, and hope this is helpful
D-Alexandre V.

parallel processing in bat file for loop

I want a batch file to do parallel processing. I have a storedprocedure which returns 1000+ records(has unique rowid column along with other info). Iterating though each row works fine. However, it takes long time to complete 1000 loops. Is there a way to run two loops parallel without overlapping or having to maintain separate batch files. Can this be accomplished by having one .bat file.
WORKING CODE:
#echo off
setlocal EnableExtensions
set ListFile=%TEMP%\StudentList.tmp
Set varServerPath=http://xyz/ReportServer
sqlcmd -Q "exec dbo.Storedproc_StudentList" -S ServerName -d DatabaseName >"%ListFile%" 2>nul
if exist "%ListFile%" (
for /F "usebackq tokens=1,2,3,4 skip=2 delims=', " %%A in ("%ListFile%") do (
echo Processing StudentID %%A and SubjectID %%B ...
if not exist "%%D" mkdir "%%D"
rs -i C:\ReportRender\Student.rss -s%varServerPath% -e Exec2005 -v StudentID="%%A" -v SubjectID="%%B" -v vOutputFilePath="%%C" -v vReportPath="/Student Reports/ReportName.rdl" -l 900
)
del "%ListFile%"
)
exit
I tried doing something like having two for loops one from 1 to 200 and other from 201 to 400 and so on....but seems like i'm on the wrong track. It doesn't work, Please suggest.
#echo off
setlocal EnableExtensions
set ListFile=%TEMP%\StudentList.tmp
Set varServerPath=http://xyz/ReportServer
sqlcmd -Q "exec dbo.Storedproc_StudentList" -S ServerName -d DatabaseName >"%ListFile%" 2>nul
if exist "%ListFile%" (
for /F "usebackq tokens=1,2,3,4 skip=2 delims=', " %%A in ("%ListFile%") do (
for /L %%A in(1,1,200) do (
echo Processing StudentID %%A and SubjectID %%B ...
if not exist "%%D" mkdir "%%D"
rs -i C:\ReportRender\Student.rss -s%varServerPath% -e Exec2005 -v StudentID="%%A" -v SubjectID="%%B" -v vOutputFilePath="%%C" -v vReportPath="/Student Reports/ReportName.rdl" -l 900
)
for /L %%A in(201,1,400) do (
echo Processing StudentID %%A and SubjectID %%B ...
if not exist "%%D" mkdir "%%D"
rs -i C:\ReportRender\Student.rss -s%varServerPath% -e Exec2005 -v StudentID="%%A" -v SubjectID="%%B" -v vOutputFilePath="%%C" -v vReportPath="/Student Reports/ReportName.rdl" -l 900
)
)
del "%ListFile%"
)
exit
Thanks,
Your approach is wrong. You are executing a for /L %%A in (1,1,200) ... and a for /L %%A in (201,1,400) ... for each record in the %ListFile%. You need to distribute the records in the %ListFile% into the two parallel processes. Although this can be done in groups of 200 records, it is much simpler to do that one-by-one. Also, the only way to have parallel processes in a Batch file is via start command or using a | pipe. In this case you want to distribute several input records that will be read (and processed) by two "output processes", so the pipe approach is simpler.
#echo off
setlocal EnableDelayedExpansion
if "%1" neq "" goto %1
set ListFile=%TEMP%\StudentList.tmp
Set varServerPath=http://xyz/ReportServer
sqlcmd -Q "exec dbo.Storedproc_StudentList" -S ServerName -d DatabaseName >"%ListFile%" 2>nul
if not exist "%ListFile%" exit
set numProcs=2
( "%~F0" Input | "%~F0" Output ) 2>&1 | "%~F0" Output
del "%ListFile%"
exit
:Input
set i=0
for /F "usebackq tokens=1,2,3,4 skip=2 delims=', " %%A in ("%ListFile%") do (
set /A i+=1, proc=i%%numProcs
if !proc! equ 1 (
echo %%A %%B %%C %%D
) else (
>&2 echo %%A %%B %%C %%D
)
)
exit /B
:Output
for /F "tokens=1-4" %%A in ('findstr "^"') do (
echo Processing StudentID %%A and SubjectID %%B ...
if not exist "%%D" mkdir "%%D"
rs -i C:\ReportRender\Student.rss -s%varServerPath% -e Exec2005 -v StudentID="%%A" -v SubjectID="%%B" -v vOutputFilePath="%%C" -v vReportPath="/Student Reports/ReportName.rdl" -l 900
)
exit /B
The :Input part just distribute the "%ListFile%" records to Stdout (channel 1) and Stderr (channel 2), one by one.
The :Output part just take the %%A %%B %%C %%D values sent by :Input part and process they in the usual way; the input data is read from Stdin via findstr command.
The :Input and :Output parts could be in separate Batch files, but they are included in the same file and selected via a parameter and the if "%1" neq "" goto %1 command placed at beginning.
The most interesting code is the pipeline that run the 3 processes in parallel. The :Input part run and its Stdout output is feed into the first :Output process. The Stderr output (channel 2) of :Input part is redirected into Stdin via the 2>&1, so this output is feed into the second :Output process.
This method may also be used for more than two output parallel processes; you just need to add more similar parts changing the number 2 for 3, etc. For example, with three output processes the pipeline should be this one:
( ( "%~F0" Input | "%~F0" Output ) 2>&1 | "%~F0" Output ) 3>&1 | "%~F0" Output
However, it is very important that you note that this method does NOT necessarily imply that the whole process will run faster! This point depends on several factors, like the number of CPU cores and the speed/buffers of the disk drive. Just a test can answer this question...
Post the result, please.

How to verify root from adb shell?

In my script I have the following code:
for /f "delims=" %%a in ('adb -s devicename shell su') do #set res=%%a
echo %res%
But I get no output. How to properly check for su availability from a batch script?
set uid=
for /f "delims=" %%a in ('adb -s devicename shell "su 0 id -u 2>/dev/null"') do set uid=%%a
echo %uid%
%uid% will be 0 if su is available

Batch script terminating unexpectedly before IF command

I have written the below code in a batch script.
set /p timestamp=Enter timestamp:
cd "C:\temp\%timestamp%"
for %%a in (*.rmt) do (bldtool -c COMMAND -a SPLIT -n %%a -l C:\temp\%timestamp%)
if exist "C:\temp\%timestamp%\XCLES01A.c" (xcopy /Y "C:\Program Files\CA\AllFusion Gen\GEN\extrn\src\XCLES01A.c" "C:\temp\%timestamp%")
for %%a in (*.icm) do (bldtool -c COMMAND -a BUILD -n %%a -l C:\temp\%timestamp% -f CodeMgr)
pause
When i run the above script line-by-line in cmd, it runs as expected. However, when I run it in the form of a script, it terminates before the if command is executed i.e. the window just disappears.
I have spent hours on trying to resolve this and am still stuck. Please help ! I am very new to batch scripting.
Thanks in advance.
Update: BLDTOOL is an executable software.
Try this (I haven't though):
set /p timestamp=Enter timestamp:
cd /D "C:\temp\%timestamp%"
for %%a in (*.rmt) do (
bldtool -c COMMAND -a SPLIT -n %%a -l "C:\temp\%timestamp%"
)
if exist "C:\temp\%timestamp%\XCLES01A.c" (
xcopy /Y "C:\Program Files\CA\AllFusion Gen\GEN\extrn\src\XCLES01A.c" "C:\temp\%timestamp%"
)
for %%a in (*.icm) do (
bldtool -c COMMAND -a BUILD -n %%a -l "C:\temp\%timestamp%" -f CodeMgr
)
pause

Resources