I'm having an issue with an URL in an if-statement using batch.
#ECHO OFF
SET /P input="Insert Link "
if %input%==l (echo true) else (echo false)
cmd /k
I want to determine if the user input is a link or a single character (in this case l). I get that the operators within the URL might cause the problem. But shouldn't the if-statement just check if %input% is l and everything else would trigger the else? Using SET %input%=l leads to the true case being triggered.
Any ideas on how to make this work in a simple way? Am I missing something regarding syntax?
Putting %input% and l within the if-statement in quotes solved the problem.
#ECHO OFF
SET /P input="Insert Link "
if "%input%"=="l" (echo true) else echo false
cmd /k
The way the Command Line works, %ThisIsAVariable% will be replaced with whatever ThisIsAVariable contains and then be interpreted as such. Hence, running your example prompts the following error:
=6zswl5YrvVw==l was unexpected at this time.
The simplest way to solve this is to wrap your %input% with ""
e.g.
#ECHO OFF
SET input=https://www.youtube.com/watch?v=6zswl5YrvVw
if "%input%"==l (echo true) else (echo false)
cmd /k
That would prompt false
Related
I have branch folder "feature-set" under this folder there's multibranch
I need to run the below script in my Jenkinsfile with a condition if this build runs from any branches under the "feature-set" folder like "feature-set/" then run the script
the script is:
sh """
if [ ${env.BRANCH_NAME} = "feature-set*" ]
then
echo ${env.BRANCH_NAME}
branchName='${env.BRANCH_NAME}' | cut -d'\\/' -f 2
echo \$branchName
npm install
ng build --aot --output-hashing none --sourcemap=false
fi
"""
the current output doesn't get the condition:
[ feature-set/swat5 = feature-set* ]
any help?
I would re-write this to be primarily Jenkins/Groovy syntax and only go to shell when required.
Based on the info you provided I assume your env.BRANCH_NAME always looks like `feature-set/
// Echo first so we can see value if condition fails
echo(env.BRANCH_NAME)
// startsWith better than contains() based on current usecase
if ( (env.BRANCH_NAME).startsWith('feature-set') ) {
// Split branch string into list based on delimiter
List<String> parts = (env.BRANCH_NAME).tokenize('/')
/**
* Grab everything minus the first part
* This handles branches that include additional '/' characters
* e.g. 'feature-set/feat/my-feat'
*/
branchName = parts[1..-1].join('/')
echo(branchName)
sh('npm install && ng build --aot --output-hashing none --sourcemap=false')
}
This seems to be more on shell side. Since you are planning to use shell if condition the below worked for me.
Administrator1#XXXXXXXX:
$ if [[ ${BRANCH_NAME} = feature-set* ]]; then echo "Success"; fi
Success
Remove the quotes and add an additional "[]" at the start and end respectively.
The additional "[]" works as regex
I have a folder with thousands of filenames like this. I need to put them inside folders and remove the useless parts from the filenames. I need to do this before adding the files inside my XBMC library.
[ www.AnnoyingSpam.com ] Some.File.Name.With.A.Very.Long.String.avi
[ www.AnnoyingSpam.com ] Another.File.With.A.Very.Long.String.mpg
[ www.AnnoyingSpam.com ] Again.And.Again.mp4
First, I want to strip the AnnoyingSpam.com tags in the files
Then, create a folder based on the file name without the tag and without the extension
Finally, move the file to the new folder
Repeat for the rest of the files in the root directory of the batch file
So far all I got is a script that will create folder and move files. But it adds the tag "Folder" to each folder and it doesn't remove the AnnoyingSpam.com tag
#echo off
for /f "usebackq delims=?" %%a in (`dir /a-d /b`) do (
if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a"
)
goto :EOF
:func
set file=%~1
set dir=%file% Folder
md "%dir%" Folder 2>nul
move "%file%" "%dir%"
goto :EOF
So my question is how can i remove these strings from the folder names that are being created in the above script:
"[ www.AnnoyingSpam.com ]"
" Folder"
Since you are performing the check if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a", my understanding is that you will be running the batch from inside the 'target' folder.
What follows is a batch script, based structurally on your sample, that accomplishes the necessary filtering and handles the rename and move operations.
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "usebackq delims=?" %%a in (`dir /a-d /b`) do (
if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a"
)
endlocal
echo Done.
goto EOF
:func
set file=%~1
for /F "delims=" %%a in ("!file!") do (
rem stripped extension, next step is to filter to remove the 'spam' part
set "c=%%~Na"
rem original filename, will be needed for the renaming
set "o=%%~a"
rem store the extension to use later on when re-constructing the filename
set "e=%%~xa"
rem this is effectively a multichar stripper; filters the 'spam' part out
set "c=!c:[ www.AnnoyingSpam.com ] =!"
rem reconstruct the filename
set "f=!c!!e!"
rem echo !o! : !c! : !e! ::: !f!
rem rename original file to the filtered version
ren "!o!" "!f!"
rem create dir named after the filtered filename, if it does not already exist; just a precaution
if not exist !c! (md !c!)
rem move the filtered file to the appropriate dir
move "!f!" "!c!" > nul
)
:EOF
The script is almost self-explanatory (see the comments inside), however there are some aspects that I would like to point out:
The instructions setlocal ENABLEDELAYEDEXPANSION and endlocal, that in fact form a wrap around the for-loop, establish that the variables inside the loop will be evaluated (expanded) at the execution time and not during parse time. Notice that the variables are referenced inside the loop as !var!. See more on this here.
The removal of the unwanted 'spam' part is performed by this command: set "c=!c:[ www.AnnoyingSpam.com ] =!". What it does is best described by the following example: set "var=!var:substring1=substring2!", has as a result the substitution of every occurrence of substring1 by substring2 in var.
If substring2 is null (as is the case here), then the actual effect is the removal of every substring1 pattern found in the original variable. More on this here, and for an interesting use as a multichar delimiter see dbenham's answer here.
And, as always, test thoroughly before you use the script on the actual folders.
I know you've asked for a batch script, but if you have php installed, you can use this:
renameSpam.php
<?php
$removeTag = "[ www.AnnoyingSpam.com ]";
foreach(glob("*.*") as $file){
if (strpos($file, $removeTag) !== false) {
$newFile = trim(str_ireplace($removeTag, "", $file));
$ext = pathinfo($newFile, PATHINFO_EXTENSION);
$fnNoExt = basename($newFile,".".$ext);
if(!is_dir($fnNoExt)){
mkdir($fnNoExt, 0777);
rename($file, "./$fnNoExt/$newFile");
}else{
rename($file, "./$fnNoExt/$newFile");
}
}
}
Place renameSpam.php on the directory with the files to rename and execute as php renameSpam.php.
I've tested this on windows and linux and it works as intended.
I'm building a tool were a command with a password needs to be entered.
I want when I enter this command with the password, the command line replaces each character with "*" or " ", so the command and the password will not be observable !
is there such a command that tells the TCL interpreter "from this point, show each character entered as *", and then switch back to regular mode ?
any other suggestion will be valuable too.
In your case, you shall take "full control" over your terminal and disable its default echoing behavior (In UNIX the likes the terminal should be entered into the so-called raw mode)
Then, you can read the characters one-by-one (till max password size or till Enter is pressed) and echo '*' per each pressed character.
You got working code examples both on UNIX and Windows how doing so here
You may want reading also this link echo-free password entry TCL wiki
proc enableRaw {{channel stdin}} {
exec /bin/stty raw -echo <#$channel
}
proc disableRaw {{channel stdin}} {
exec /bin/stty -raw echo <#$channel
}
enableRaw
set c [read stdin 1]
puts -nonewline $c
disableRaw
package require twapi
proc enableRaw {{channel stdin}} {
set console_handle [twapi::GetStdHandle -10]
set oldmode [twapi::GetConsoleMode $console_handle]
set newmode [expr {$oldmode & ~6}] ;# Turn off the echo and line-editing bits
twapi::SetConsoleMode $console_handle $newmode
}
proc disableRaw {{channel stdin}} {
set console_handle [twapi::GetStdHandle -10]
set oldmode [twapi::GetConsoleMode $console_handle]
set newmode [expr {$oldmode | 6}] ;# Turn on the echo and line-editing bits
twapi::SetConsoleMode $console_handle $newmode
}
enableRaw
set c [read stdin 1]
puts -nonewline $c
disableRaw
(Assuming Linux.) By far the easiest way to handle passwords in a terminal is to turn off echoing of input but leave the terminal otherwise in cooked mode. It won't show a * for each entered character, but it does mean that you don't have to handle things like backspace (when a user realises they typed the last couple of characters wrong before hitting Return), etc.
exec /bin/stty -echo <#stdin
set password [gets stdin]
puts ""
exec /bin/stty echo <#stdin
If you've got Tcl 8.6, you can easily make this more robust with this procedure:
proc getPassword {{prompt "Password: "}} {
exec /bin/stty -echo <#stdin
try {
puts -nonewline $prompt
flush stdout
return [gets stdin]
} finally {
puts ""
flush stdout
exec /bin/stty echo <#stdin
}
}
(It's possible to use catch and some scripting to emulate try…finally but it's really annoying.)
If you have a GUI and prefer that, you make a password entry box by setting the -show option to something non-empty (e.g., * to show an asterisk).
I am currently creating a segment of a larger batch file that needs to let the user interact with the command prompt, but I seem to be having issues with the IF Statements.
The following code is what I've been trying:
:ONE2
cls
echo Free Roam is used like command prompt, but with additional commands.
echo Type FRHELP for a list of additional commands.
:COMMANDLOOP
echo.
set /P TEMPCMD=%CD% :
IF %TEMPCMD% == QUIT (
GOTO END2
) else if %TEMPCMD% == FRHELP (
GOTO COMMANDSLIST
) else (
%TEMPCMD%
GOTO COMMANDLOOP
)
:COMMANDSLIST
echo.
echo FRHELP = Display Free Roam Commands
echo QUIT = Leave your current Free Roam Session
GOTO COMMANDLOOP
What happens is, I can make multi-part commands (EG: cd ..) without the IF statements. But with the IF statements, it will only allow me to make single part commands (EG: dir).
If I have the IF statements as listed above, it will give me the "'..' was unexpected" error and exit.
Is there any way to pass my TEMPCMD variable to the command prompt with these IF statements and not get this error?
try this, it works for me without any error:
#echo off &setlocal
:COMMANDLOOP
echo.
set "TEMPCMD=%CD%"
set /P "TEMPCMD=%CD% :"
IF "%TEMPCMD%"=="QUIT" (GOTO END2
) else (
if "%TEMPCMD%"=="FRHELP" (
GOTO COMMANDSLIST
) else (
GOTO COMMANDLOOP
)
)
pause
:COMMANDSLIST
echo.
echo FRHELP = Display Free Roam Commands
echo QUIT = Leave your current Free Roam Session
GOTO COMMANDLOOP
I dont know why my batch if and goto isnt working
it works until the user has to pick a choice to run. The whole thing just shutsdown
Here is my script:
#Echo off
:Password
set input=
set b=
set c=
set /p input=Password:
if %input%==******** goto yes
if not %input%==******** goto no
cls
:yes
cls
echo Access Accepted! Welcome Tucker!
echo.
set /p op=Enter Your Full-Name For Access:
if %op%== TuckerGarySiegel goto home
cls
:no
echo Wrong Password. Access Denied. No Entry.
goto password
cls
:home
color 1f
This is what I think is the problem area
Echo Welcome Tucker!
echo 1) My Batch Files
echo 2) Google Chrome
set /p input=Type Your Selection:
if %c%== 1 goto batch
if not %c%== 1 goto home
pause
cls
:batch
echo Choose File:
echo 1) Password Script
set /p b=Make Selection:
if %b%== 1 goto passscript
pause
cls
:passscript
i still need to make the rest
please help
set /p input=Type Your Selection:
if %c%== 1 goto batch
if not %c%== 1 goto home
You're setting the variable input but then checking the variable c. Try this instead, which will allow it to work:
set /p input=Type Your Selection:
if %input%== 1 goto batch
if not %input%== 1 goto home
P.S. you don't need to check an if and an if not. Doing this would be just fine:
set /p input=Type Your Selection:
if %input%== 1 goto batch
goto home