I have a github action that wants to test if a specific path has been created, and I'm running into a problem where github has
USERNAME=runneradmin
TMP=C:\Users\RUNNER~1\AppData\Local\Temp
The test script (VIRTUAL_ENV is set by the activate.bat script):
echo on
set "WORKON_HOME=%TMP%\wo home %RANDOM%"
mkdir "%WORKON_HOME%"
cd /d "%WORKON_HOME%"
virtualenv "name with spaces"
cd "name with spaces"
call Scripts\activate.bat
if "%WORKON_HOME%\name with spaces"=="%VIRTUAL_ENV%" (
echo workon home and virtualenv are equal
) else (
echo. "%WORKON_HOME%\name with spaces" is not equal to "%VIRTUAL_ENV%"
)
if "%CD%"=="%VIRTUAL_ENV%" (
echo cd and virtualenv are equal
) else (
echo. "%CD%" is not equal to "%VIRTUAL_ENV%"
)
exit /b 1
The output is (the difference is RUNNER~1 vs runneradmin):
"C:\Users\RUNNER~1\AppData\Local\Temp\wo home 14549\name with spaces" is not equal to "C:\Users\runneradmin\AppData\Local\Temp\wo home 14549\name with spaces"
"C:\Users\RUNNER~1\AppData\Local\Temp\wo home 14549\name with spaces" is not equal to "C:\Users\runneradmin\AppData\Local\Temp\wo home 14549\name with spaces"
The action yaml for completeness:
name: BUG
on: [ push, pull_request, workflow_dispatch ]
jobs:
ci-test:
name: BUG
runs-on: windows-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-python#v2
with:
python-version: 3.9
- run: pip install virtualenv
- shell: cmd
run: set
- name: Run tests
shell: cmd
run: |
tests\bug.bat
How can I make the paths compare equal?
I found an answer using a subroutine and the %~s1 magic string to get the 8.3 version of the file name:
echo on
set "WORKON_HOME=%TMP%\wo home %RANDOM%"
mkdir "%WORKON_HOME%"
cd /d "%WORKON_HOME%"
virtualenv "name with spaces"
cd "name with spaces"
call Scripts\activate.bat
call:shorten "%WORKON_HOME%\name with spaces",WO
call:shorten "%VIRTUAL_ENV%",VE
if "%WO%"=="%VE%" (
echo workon home and virtualenv are equal
) else (
echo. "%WORKON_HOME%\name with spaces" is not equal to "%VIRTUAL_ENV%"
)
goto:eof
:shorten
for %%T in ("%~s1") do set "%~2=%%T"
exit /b 0
Related
I am new to Batch scripting.
I am trying to write a script which parses given command and check if argument with name 'folder 'is present in that command and if not , add that argument with default value.
I have written following script. This scripts executes correctly if argument is missing.
But if argument is present , both if and else blocks are executed.
Please help. Thanks in advance.
#echo off
set ARGS=-action generate -folder "Source"
set t=%ARGS%
echo %t%|find "-folder" >nul
if errorlevel 1 (
goto setDefaultFolder
) else (
echo Folder is specified in command
)
:setDefaultFolder
echo Folder is NOT specified in command. User's current directory will be used as Folder.
set folderArgName=-folder
set folderArgValue="%cd%"
set folderArg=%folderArgName% %folderArgValue%
echo folderArgName : %folderArgName%
echo folderArgValue : %folderArgValue%
echo folderArg: %folderArg%
set ARGS=%ARGS% %folderArg%
echo ARGS : %ARGS%
Output of code :
Folder is specified in command
Folder is NOT specified in command. User's current directory will be used as Folder.
folderArgName : -folder
folderArgValue : "C:\work"
folderArg: -folder "C:\work"
ARGS : -action generate -folder "Source" -folder "C:\work"
You have to have a goto in the else, that goes to after the setDeafultFolder, otherwise it just will execute the setDefaultFolder after it
#echo off
set ARGS=-action generate -folder "Source"
set t=%ARGS%
echo %t%|find "-folder" >nul
if errorlevel 1 (
goto setDefaultFolder
) else (
echo Folder is specified in command
goto endOfBatch
)
:setDefaultFolder
echo Folder is NOT specified in command. User's current directory will be used as Folder.
set folderArgName=-folder
set folderArgValue="%cd%"
set folderArg=%folderArgName% %folderArgValue%
echo folderArgName : %folderArgName%
echo folderArgValue : %folderArgValue%
echo folderArg: %folderArg%
set ARGS=%ARGS% %folderArg%
echo ARGS : %ARGS%
:endOfBatch
So I have this very simple script which just tries to create a text file and then read it.
version: 1.0.{build}-{branch}
shallow_clone: true
environment:
matrix:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
TOOLSET: msvc-14.1
ARCH: x86_64
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
TOOLSET: msvc-14.1
ARCH: x86
install:
- if %ARCH% == x86 ( set TRIPLET=x86-windows ) else ( set TRIPLET=x64-windows )
- if %ARCH% == x86 ( set AM=32 ) else ( set AM=64 )
- git clone -b master --depth 1 https://github.com/boostorg/boost.git boost-root
- cd boost-root
build: off
test_script:
- set PATH=%ADDPATH%%PATH%
- |-
set VCPKG=%APPVEYOR_BUILD_FOLDER%\..\vcpkg\installed\%TRIPLET%
set "CHUNK=^<include^>%VCPKG%\include ^<search^>%VCPKG%\lib"
echo using zlib : : %CHUNK% ; > config.jam
echo using libjpeg : : %CHUNK% ; >> config.jam
echo using libpng : : ^<name^>libpng16 %CHUNK% ; >> config.jam
echo using libtiff : : %CHUNK% ; >> config.jam
more config.jam
When I run it I get the following error:
Cannot access file C:\projects\gil\boost-root\config.jam
Command exited with code 1
Is there something obviously wrong that I'm missing?
I think set "CHUNK=^<include^>%VCPKG%\include ^<search^>%VCPKG%\lib" breaks something in environment variables. Try moving first " after =, like set CHUNK="^<include^>%VCPKG%\include ^<search^>%VCPKG%\lib".
I have some document in a folder. these document may have same name with different extention, e.g: xyz.doc, xyz.pdf and xyz.log. I want implement following logic in windows command line script:
foreach *.doc {
if EXIST *.pdf{
do something_1
}else {
do something_2
}
if EXIST *.log {
do something_3
}
Here's my Minimal, Complete, and Verifiable example (Copy&Paste from cmd window). Used another extensions but principle is the same:
==> dir /b "sta *.*"
sta tus.csv
sta tus.txt
sta tus2.txt
==> for %G in ("sta *.txt") do #if exist "%~nG.csv" (echo + %~nG ) else (echo - %~nG)
+ sta tus
- sta tus2
==>
A batch script for your extensions:
#echo off
for %%G in ("*.doc") do (
if exist "%%~nG.pdf" (
echo + %%~nG
) else (
echo - %%~nG
)
)
Resources (required reading, incomplete):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%~G, %~nG etc. special page) Command Line arguments (Parameters)
I have a situation in which i have to list file which is of the type as
databaseName.schemaName#1234sdf2323.lock where _Database_Name and _target_schema_name
_lockFolder are variables
# is a token then it is followed by the random alphanumeric number and the same is end with .lock .
I have acheived this in batch file through the code as
FOR /R %_lockFolder% %%F in (%_Database_Name%.%_target_schema_name%#*.lock) do (
for /f "tokens=1* delims=# " %%G IN ("%%~nF") DO (
SET _no=%%H
)
)
but when i am changing it into bash enviorment so that it can run on unix enviorment
for entry in "${_lockFolder}"/*
do
echo ENTRY "$entry"
name='${_lockFolder}/${_Database_Name}.${_target_schema_name}#*.lock'
ls -l $name > "${lockFolder}"
if [ "$?" -eq "0" ]
then
echo "Do your work here"
else
echo "No files are there for the given pattern"
fi
# exit 21
done
It is not able to recognize the pattern . The files are present in the folder which i have specified
you can simple write a for loop like,
for file in `find ${lockdir} -name "^${_lockFolder}/${_Database_Name}.${_target_schema_name}#*.lock$"`
do
echo $file
:
:
your job
done
example,
[root#giam20 unix]# ls
checksumupdator.sh GIAMMEFProcessor.sh GIAMRoleExtractor.sh
GIAMAccountExtractor.sh GIAMMetaDataLoader.sh GIAMRoleLoader.sh
GIAMAccountLoader.sh GIAMOOPControlledAttrExtractor.sh GIAMRoleMappingLoader.sh
GIAMAccountTransferLoader.sh GIAMOOPControlledAttrsLoader.sh
[root#giam20 unix]# find . -name "GIAM*.sh"
./GIAMAccountTransferLoader.sh
./GIAMIntermediateCodeUpgrader.sh
./GIAMServiceUpdator.sh
./GIAMOOPControlledAttrsUpdator.sh
./GIAMRoleUpdator.sh
./GIAMProvisioningPolicyExtractor.sh
./GIAMCompExemptionExtractor.sh
./GIAMApprovalNotificationLoader.sh
In Unix shell scripting, variable references are not resolved in a string that is enclosed in single quotes. Therefore, in this line
name='${_lockFolder}/${_Database_Name}.${_target_schema_name}#*.lock'
the value will be stored into name literally, including all the ${name} references. It is no surprise then that the pattern is not matched later.
So, just change the single quotes to double quotes:
name="${_lockFolder}/${_Database_Name}.${_target_schema_name}#*.lock"
In CMD the following variable will give you the name of the computer: %COMPUTERNAME%
I need a variable that takes a part of the computername.
I need a if statement that checks if the computername contains "KM" at the start and 00 at the end. It should not look at the number between KM and -00
KM100-00
KM200-00
This works here:
echo %computername%| findstr "^KM.*00$" >nul && echo found the right format
You can do this with substring commands, as per the following transcript:
pax> set xyzzy=KM100-00 KM200-00
pax> echo %xyzzy%
KM100-00 KM200-00
pax> echo %xyzzy:~0,2%
KM
pax> echo %xyzzy:~-2,2%
00
pax> if %xyzzy:~0,2%==KM if %xyzzy:~-2,2%==00 echo yes
yes
That final (chained) if statement is the one you're looking for to see if your variable starts with KM and ends with 00.
The expression %X:~Y,Z% will give you the Z characters starting at position Y (zero-based) of the variable X. You can provide a negative value of Y to make it relative to the end of the string.
echo %computername%| findstr /I /b "KM" | findstr /i /e "00" && echo computer name is like KM-XX-00
You can try also with hostname instead of echo %computername%
I recommend you to read this page, which is about substring usage in command prompt.
And why dont you try this;
set str=KM2000-00
echo.%str%
set pre=%str:~0,2%
echo.%pre%
set pst=%str:~-2%
echo.%pst%
IF %pre% == KM( IF %pst% == 00( echo.true ) )
pause