03a01Fa.wav
03a01Nc.wav
03a01Wa.wav
I have this text file
by using batch file i want to appent "anger" if the letter in caps is W, if the letter is F then "happy", N for "neutral". is it possible to do ?
i want it like this
03a01Fa.wav,happy
03a01Nc.wav,neutral
03a01Wa.wav,anger
Thanks in advance, looking forward to a solution.
#echo off
setlocal EnableDelayedExpansion
rem Define the equivalences array
for %%a in ("W=anger" "F=happy" "N=neutral") do (
for /F "tokens=1,2 delims==" %%b in (%%a) do (
set "append[%%b]=%%c"
)
)
for /F %%a in (theFile.txt) do (
set name=%%a
for /F %%b in ("!name:~5,1!") do echo %%a,!append[%%b]!
)
You may review the array management in Batch files at this post.
Another way using FIND :
#echo off
for /f "delims=" %%a in (file.txt) do (
echo %%a | find "F">nul && echo %%a,happy
echo %%a | find "N">nul && echo %%a,neutral
echo %%a | find "W">nul && echo %%a,anger)
If you need the output in a file (output.txt) :
#echo off
for /f "delims=" %%a in (file.txt) do (
echo %%a | find "F">nul && echo %%a,happy
echo %%a | find "N">nul && echo %%a,neutral
echo %%a | find "W">nul && echo %%a,anger)>output.txt
#echo off
setlocal enabledelayedexpansion
(for /f %%i in (t.t) do (
set a=%%i
set a=!a:~5,1!
if !a!==F echo %%i,happy
if !a!==N echo %%i,neutral
if !a!==W echo %%i,angry
))>o.t
#echo off
setlocal enableextensions disabledelayedexpansion
(for /f "usebackq" %%a in ( "wavfile.txt" ) do (
for /f "delims=0123456789abcdefghijklmnopqrstuvwxyz" %%b in ("%%a") do (
if %%b==W (
echo %%a,anger
) else if %%b==F (
echo %%a,happy
) else if %%b==N (
echo %%a,neutral
)
)
)) > wavFileOut.txt
Related
I have myfile.txt containing this:
line 1: keyword=string
line 2
line 3
I need to echo that variable string via constant keyword.
And this string I need is always in the end of the 1st line.
So far I have:
for /f "usebackq tokens=*" %%a in ("myfile.txt") do (
for /f "usebackq tokens=*" %%b in ('findstr /i "keyword=" %%a') do (
echo %%b & goto 108
)
)
:108
But I guess that findstr syntax is completely wrong.
Please, help.
You can do rhis very simply as:
Echo off
SETLOCAL ENABLEDELAYEDEXPANSION
For /F "tokens=*" %%_ IN ('Type "c:\my\path\myfile.txt" ^| FIND /I "keyword="
) DO (
SET "Tmp_Line=%%_"
For /F "Tokens=* delims==" %%A in ('ECHO=!Tmp_Line:*keyword=!') DO (
echo %%a
GOTO :108
Pause
)
GOTO :EOF
)
Goto :EOF
In File_1 I have:
Word_1;ger
Word_1;gr
Word_1;greber
Word_1;gaerfsd
Word_2;gerbhge
Word_2;tgbzrfvd
Word_3;gzfdfdc
I want to calculate the number of duplicates of each first column of each line then, depending on the number of duplicates(one or different than one), I will copy paste them to two different files.
File_2 will contain:
Word_3;gzfdfdc
File_3 will contain:
Word_1;ger
Word_1;gr
Word_1;greber
Word_1;gaerfsd
Word_2;gerbhge
Word_2;tgbzrfvd
Here is the code that I wrote:
setlocal EnableDelayedExpansion
(for /f "tokens=1-2 delims=;" %%a in (File_1) do (
set current_line=%%a
if "!current_line!" NEQ "!previous_line!" (
for /f %%C in ('Find /C %%a ^< File_1) do (
set Count=%%C
if "!Count!==1" (
findstr %%a File_1 >>File_2
)
if not "!Count!==1" (
findstr %%a File_1 >>File_3
)
)
)
set previous_line=!current_line!
)
It doesn't seem to work. Any Help ?
removed an unneeded variable, corrected an erroneous if syntax and added some quotes for "best practice". Seems to do exactly, what you intend:
setlocal EnableDelayedExpansion
(for /f "tokens=1-2 delims=;" %%a in (File_1.txt) do (
if "%%a" NEQ "!previous_line!" (
for /f %%C in ('Find /C "%%a" ^< File_1.txt') do (
if "%%C"=="1" (
findstr "%%a" File_1.txt >>File_2.txt
) else (
findstr "%%a" File_1.txt >>File_3.txt
)
)
)
set "previous_line=%%a"
))
Although your (corrected) code works ok, it is pretty inefficient. You execute findstr command two times for each group of duplicates. If the input file is large, this method could take a lot of time.
You may get the same result with a single for /F pass over the file, with no use of findstr:
#echo off
setlocal EnableDelayedExpansion
del File_2.txt File_3.txt 2>NUL
set "last1="
set "moreThanOne="
for /f "tokens=1-2 delims=;" %%a in (File_1.txt) do (
if "%%a" equ "!last1!" (
>> file_3.txt echo !last1!;!last2!
set "moreThanOne=1"
) else (
if defined moreThanOne (
>> file_3.txt echo !last1!;!last2!
) else if defined last1 (
>> file_2.txt echo !last1!;!last2!
)
set "moreThanOne="
)
set "last1=%%a"
set "last2=%%b"
)
if defined moreThanOne (
>> file_3.txt echo !last1!;!last2!
) else (
>> file_2.txt echo !last1!;!last2!
)
Here is my code
ECHO off
CLS
ECHO List of VMs:
ECHO .............
cd /D "F:\VMs"
setlocal enabledelayedexpansion
set num=0
for /f "tokens=*" %%i in ('dir /s/b *.vmx') do set /a num+=1&set VM[!num!]=%%i
set numberOfVMs=0
for /F "tokens=2 delims==" %%s in ('set VM[') do (
set /a numberOfVMs+=1
echo !numberOfVMs! -^> %%s
)
ECHO.
ECHO List of Running VMs:
ECHO .....................
set num=-1
for /f "tokens=*" %%i in ('vmrun -T ws list') do (
set /a num+=1
if !num! NEQ 0 set RUNNINGVM[!num!]=%%i
)
set numberOfRunningVMs=0
for /F "tokens=2 delims==" %%s in ('set RUNNINGVM[') do (
set /a numberOfRunningVMs+=1
echo !numberOfRunningVMs! -^> %%s
)
ECHO.
ECHO List of Available VMs:
ECHO .......................
for /l %%x in (1, 1, %numberOfVMs%) do (
::echo !VM[%%x]!
for /l %%y in (1, 1, %numberOfRunningVMs%) do (
echo !VM[%%x]!
echo !RUNNINGVM[%%y]!
if "!VM[%%x]!"=="!RUNNINGVM[%%y]!" (
echo "success"
)
)
)
Array varibale 'VM' has below values:
F:\VMs\PD-UI-Tests-Win10-x64-Office2016-32bit.vmwarevm\Windows-10-x64.vmx
F:\VMs\PD-UI-Tests-Win10-x64-Office2016-64bit.vmwarevm\Windows-10-x64.vmx
F:\VMs\PD-UI-Tests-Win7-x64-Office2013-32bit.vmwarevm\Windows7-x64-RTM.vmx
Array varibale 'RUNNINGVM' has below values:
F:\VMs\PD-UI-Tests-Win7-x64-Office2013-32bit.vmwarevm\Windows7-x64-RTM.vmx
But, the final if condition never becomes 'true' although the VM[3] is equal to RUNNINGVM[1]. What am i missing? Please help
Don't use ::-style comments within a code-block as it is actually a broken label, and labels break code-blocks. Use rem instead.
Suppose I have a text file like this
node1.log
node2.log
node3.log
node4.log etc...
I want to read each line by line and execute
alan.exe node1.log node2.log
alan.exe node3.log node4.log
etc..
What is the best way to accomplish this?
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (file.txt) do (
if not defined line (
set "line=%%a"
) else (
ECHO alan.exe !line! %%a
set "line="
)
)
Give this a try. File.txt will have your log file names.
#echo off
setlocal enabledelayedexpansion
FOR /F "delims=" %%G IN ('find /c /v "" ^<file.txt') DO SET NUM=%%G
SET /A NUM=NUM / 2
< file.txt (
FOR /L %%G IN (1,1,%NUM%) DO (
SET /P LINE1=
SET /P LINE2=
echo mypgm.exe !LINE1! !LINE2!
)
)
pause
Output
mypgm.exe node1.log node2.log
mypgm.exe node3.log node4.log
mypgm.exe node5.log node6.log
Press any key to continue . . .
Remove the word echo and replace mypgm.exe with the program you want to run. The echo is just in there for the proof of concept.
Like this:
#echo off
setlocal enabledelayedexpansion
set args=
set n=2
for /f "tokens=*" %%x in (file.txt) do (
set args=!args! %%x
set /a n -= 1
if !n! EQU 0 (
alan !args!
set args=
set n=2
)
)
I have a program to search drives for a specific file. The only problem is is that i wont know what drive it is on. The problem occurs when it searches a drive for a file that isn't there is there a way to go on to the next drive after say like 10 seconds. Heres my code so far
#echo off
setlocal EnableDelayedExpansion
set count=1
for /f "skip=1" %%a in ('wmic logicaldisk get caption') do (
set drive!count!=%%a
set /a count+=1
)
set "gh=!drive1!"
::Change this to do whatever with the variables
set "fh=!drive2!"
set "hh=!drive3!"
set "jh=!drive4!"
For /R %gh%\ %%G IN (*.ut2) do set jk="%%~dpG"
if defined jk (
echo %jk%
) else (
goto next
)
for /r %jk% %%a in (*) do if "%%~nxa"=="CTF-Hydro-16-2k3.ut2" set k=%%~dpnxa
if defined k (
echo %k% found
pause
cls
echo You have it
goto end
) else (
echo Map not found
copy %CD:~0,3%\Unrealmap\CTF-Hydro-16-2k3.ut2 %jk%
goto end
)
:next
For /R %fh%\ %%G IN (*.ut2) do set ht="%%~dpG"
if defined ht (
echo %ht%
) else (
goto there
)
for /r %ht% %%a in (*) do if "%%~nxa"=="CTF-Hydro-16-2k3.ut2" set m=%%~dpnxa
if defined k (
echo %m% found
pause
cls
echo You have it
goto end
) else (
echo Map not found
copy %CD:~0,3%\Unrealmap\CTF-Hydro-16-2k3.ut2 %jk%
goto end
:there
:end
cls
echo done
pause
#echo off
for /f "skip=1" %%D in ('wmic logicaldisk get caption') do (
for /r "%%D\\" %%G IN (*.ut2) do (
echo %%~dpG
if exist "%%~dpGCTF-Hydro-16-2k3.ut2" (
echo Found the map.
goto end
) else (
echo Map not found, copying
copy %CD:~0,3%\Unrealmap\CTF-Hydro-16-2k3.ut2 "%%~dpG"
goto end
)
)
)
:end
pause
Try next approach:
#ECHO OFF >NUL
SETLOCAL enableextensions
set /A "ii=0"
for /f "skip=1" %%D in ('
wmic logicaldisk get caption
') do for /F %%d in ("%%D") do (
echo searching %%d
for /F "delims=" %%G IN ('dir /B /S "%%d\CTF-Hydro-16-2k3.ut2" 2^>nul') do (
set /A "ii+=1"
set "k=%%G"
rem remove 'rem' from next line to discontinue searching
rem goto :testfound
)
TIMEOUT /T 10 /NOBREAK >NUL
)
:testfound
if %ii% EQU 0 (
echo Map not found
) else (
echo Map found %ii% times: last one "%k%"
)
Here the for loops against wmic command are
%%D to retrieve the caption value;
%%d to remove the ending carriage return in the value returned: wmic behaviour: each output line ends with 0x0D0D0A (<CR><CR><LF>) instead of common 0x0D0A (<CR><LF>).
See Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem
Another eventuality: parse
wmic datafile where "Extension='ut2' and FileName='CTF-Hydro-16-2k3'" get Name 2>NUL