How to echo. only if previous line is not empty? [closed] - windows

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
How in my batch I can echo. ONLY if the previous line outputted is non-empty ? As you understood I want to avoid to have 2 /n/n

Write a function for echo, that remember if the last line was empty.
#echo off
call :func1
call :func2
:func1
call :echo "Func1"
call :echo ""
call :echo ""
exit /b
:func2
call :echo ""
call :echo "Func2"
exit /b
:echo
set "text=%~1"
setlocal EnableDelayedExpansion
if "!text!!last_line!" NEQ "" (
echo(!text!
)
endlocal
set "last_line=%~1"
exit /b
Output:
Func1
Func2

Related

Splitting by newline in a Windows batch file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I have a variable %data% in a Windows batch file which has variable information in the following format:
URL_A
TEXT
URL_B
So essentially, three strings separated by two newline characters. The length of each string varies on each run. I need to split these three lines into three separate variables. The FOR command seemed promising but there doesn't seem to be a way to specify a newline delimiter for the delim= option.
EDIT (as requested by #Compo):
Here is the batch file's syntax:
play_vid.bat -stream <VID_HEIGHT> <START-TIME> <URL>
Here is the command that generates the %data% variable in my batch file (requires youtube-dl to be installed within the %PATH% environment variable to work):
FOR /F "skip=1 delims=" %%a IN ('youtube-dl --get-title -g -f "bestvideo[height<=%2][fps<=30],worstaudio" --no-playlist "%4"') DO #set data=%%a
Here is a sample command with all the batch variables resolved so that it can be run directly from cmd to test and view the contents of the %data% variable:
FOR /F "skip=1 delims=" %a IN ('youtube-dl --get-title -g -f "bestvideo[height<=1080][fps<=30],worstaudio" --no-playlist "https://www.youtube.com/watch?v=ltynWs_LtIw"') DO #echo %a
For this particular example, %data% will contain:
https://manifest.googlevideo.com/api/manifest/dash/expire/1592941694/ei/HgjyXvilD4ndwQG3nrzoBg/ip/[redacted]
How To Stabilize GoPro Video for Free with FFMPEG
https://manifest.googlevideo.com/api/manifest/dash/expire/1592941694/ei/HgjyXvilD4ndwQG3nrzoBg/ip/[redacted]
that is,
line 1: The URL of the video (DASH) stream.
line 2: The title of the video.
line 3: The URL of the audio (DASH) stream.
The for /F command is intended to loop through lines, so line-breaks as delims option would not make much sense. Anyway, you could combine for /F with if defined:
#echo off
rem // Initially clear variables:
set "ONE=" & set "TWO=" & set "THREE="
rem // Read file line by line (note that `eol=;` by default):
for /F "usebackq delims=" %%L in ("data.txt") do (
if not defined ONE (
rem // This section is entered in the 1st iteration:
set "ONE=%%L"
) else if not defined TWO (
rem // This section is entered in the 2nd iteration:
set "TWO=%%L"
) else (
rem // This section is entered in the 3rd iteration (and further):
set "THREE=%%L"
)
)
echo Line 1: %ONE%
echo Line 2: %TWO%
echo Line 3: %THREE%
As an alternative, you could assign a pseudo-array, like this:
#echo off
rem // Initially clear pseudo-array variables:
for /F "delims== eol==" %%V in ('set $LINE[ 2^> nul') do set "%%V="
rem // Read file line by line, precede each one with line number + `:`:
for /F "tokens=1* delims=:" %%K in ('findstr /N "^" "data.txt"') do (
rem // Assign pseudo-array element with line number as index:
set "$LINE[%%K]=%%L"
rem // Conditionally leave loop (optionally):
if %%K geq 3 goto :NEXT
)
:NEXT
rem // Return result:
set $LINE[
For more information on that topic, refer to this comprehensive answer: Arrays, linked lists and other data structures in cmd.exe (batch) script
I don't know if you mean something like this ?
#echo off
<test.txt (
set /p Var1=
set /p Var2=
set /p Var3=
)
echo Var1=%Var1%
echo Var2=%Var2%
echo Var3=%Var3%
pause

Windows BATCH filter with regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to do a bat script to process file one by one, according to a number in the filename. Here is an example of what I can get :
CDEP_0003.pdf
CIM_0001.pdf
ESCALE_0002.pdf
It's possible to loop and process each of those files in an ordered way using regex or something like this to exclude the number and use it as a loop index ? (0001, 0002, 0003 etc..)
Simply loop through .*0000\.pdf$ to .*9999\.pdf$ using another answer on how to loop through files matching wildcard in batch file
You could
build an array of used numbers by first iterating through the files, or
use a counting loop (for /l) from 10000 to 19999 to have the
leading zeros and take the last 4 digits..
Provided there is only one underscore preceding the number:
#Echo off
Pushd "X:\folder\to\start"
::clear array
for /f "delims==" %%A in ('Set No[ 2^>Nul') do Set "%%A="
:: fill array
for /f "tokens=2delims=_." %%A in (
'Dir /B "*_????.pdf" ^| findstr "^.*_[0-9][0-9][0-9][0-9]\.pdf$"'
) Do Set /A "No[%%A]=%%A"
:: process existing numbers
for /f "tokens=2 delims=[]" %%A in ('Set No[ 2^>Nul') do (
Rem dir "*_%%A.pdf" or whatever you want to do
)

Windows batch script to rename files that have random names? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to make windows batch script to rename files that have random names. I have a folder with thousand .txt files, their names are completely random,
I want to rename first 5 files in that folder to file1.txt, file2.txt,file3.txt, file4.txt,file5.txt.
Help appreciated.
Is this okay?
#ECHO OFF
(SET f=C:\Test)
IF /I "%CD%" NEQ "%f%" PUSHD "%f%" 2>NUL||EXIT/B
SET "i=5"
FOR %%A IN (*.txt) DO CALL :SUB "%%A"
EXIT/B
:SUB
IF %i% GTR 0 REN %1 File%i%.txt
SET/A i-=1
Just change line two if your stated directory path\name has changed.

How would I write a batch script to either minimise all cmd windows or minimise a window with a given name? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
The windows that I am trying to minimise are already running so start /min is not an option.
It can be done with an vbscript hybrid.
#echo off
set "lookFor=Calculator"
for /f "tokens=2 delims=," %%A in ('tasklist /nh /fo csv /fi "WINDOWTITLE eq %lookfor%"') do set "pid=%%~A"
>"%TEMP%\_edit.vbs" (
echo/Set oShell = CreateObject("WScript.Shell"^)
echo/oShell.AppActivate %pid%
echo/WScript.Sleep 500
echo/oShell.SendKeys "%% (n)" ' minimize (Alt+SpaceBar,n^)
)
start /B cmd /C "CScript //nologo //E:vbs "%TEMP%\_edit.vbs" & rem del /F /Q "%TEMP%\_edit.vbs" 2>NUL"
exit/B

Batch Script: to remove a specific/targeted words that include special characters from multiple filenames [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have few mp3 files that have external site names say www.mymusic.com and that name will be either at the start/end of the mp3 file
Examples:
01 - {www.mymusic.com} Hello Hello.mp3,
02 - {www.mymusic.com} Hai Hai.mp3,
03 - Hello Boys [www.musicworld.in].mp3,
04 - Hello girls [www.musicworld.in].mp3
So I want a (windows) batch script which asks me to enter a name with special characters say "[www.musicworld.in]" and removes it from multiple mp3 files present in a directory.
Also give me an example how to use/run the code.
Please help :)
Thanks in advance
The code works immediately!
#echo off &SETLOCAL enabledelayedexpansion
SET /p "word=enter word to remove: "
IF "%word%"=="" GOTO :EOF
FOR /f "delims=" %%a IN ('dir /a-d /b "*%word%*.mp3"') DO (
SET "fname=%%~na"
SET "fname=!fname:%word%=!"
IF NOT "!fname!"=="" REN "%%~a" "!fname!%%~xa"
)
This is a batch file that can help you - it uses a helper batch file called repl.bat from http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
At the moment it only echos the REN command so remove echo if you are happy with the result. One benefit with this is that ! characters are not an issue as they are with delayed expansion.
#echo off
set /p "var=Enter the text to remove: "
for /f "delims=" %%a in ('dir *.mp3 /b /a-d ^|repl "^(.*)%var%(.*)" "ren \x22$&\x22 \x22$1$2\x22" x ') do echo %%a

Resources