batch file send current folder inside a quoted argument - windows

I have a batch file for converting images, arguments are quotes, and quotes strings inside the argument are escaped. However, I want to send the current path %CD %also as an argument... but executing this:
"C:\Program Files\GIMP 2\bin\gimp-2.8.exe" -i -b "(python-fu-watermark-folder RUN-INTERACTIVE \"%CD%\" \"%CD%_watermarked\" \"C:/Documents and Settings/Jan/Desktop/watermarking/customEffectsWatermark.png\")" -b "(gimp-quit 0)"
gives me an echoed:
C:\Documents and Settings\Jan\Desktop\watermarking\tester>"C:\Program Files\GIMP2\bin\gimp-2.8.exe" -i -b "(python-fu-watermark-folder RUN-INTERACTIVE \"C:\Documents and Settings\Jan\Desktop\watermarking\tester\" \"C:\Documents and Settings\Jan\Desktop\watermarking\tester_watermarked\" \"C:/Documents and Settings/Jan/Desktop/watermarking/customEffectsWatermark.png\")" -b "(gimp-quit 0)"
which is nice, however, the backslashes from the directory are escape characters! so while executing the plugin I end up with a escaped directory (I dont want that):
WatermarkFolder-Warning: C:Documents and SettingsJanDesktopwatermarking ester
WatermarkFolder-Warning: C:Documents and SettingsJanDesktopwatermarking ester_watermarked
WatermarkFolder-Warning: files to be processed in total: 0
As I have no control over the path, is there a way to escape the escapecharacters?
Thanks!
Lode

Did you try making a variable with the value of %CD% double escaped?
Something like this:
set mycd=%cd:\=\\%
And using %mycd% where you were using %cd%

Related

CMD to bash script conversion semicolumn

I am trying to convert a CMD script (.bat) into a .sh script on Linux side.
I did not find a proper documentation for instance for the following lines
set PATH="${PATH1}%;${PATH_NAME};"
another_script.bat -create "%LocalDestination%TEST;%LocalDestination%" -e %GenericEnvironementName% -d "%SettingsPath%/Env"
For the first one it is an export but I do not know it is like the if condition?
${PATH1}=${PATH_NAME}
export PATH=$PATH1
for the second one the expression
"%LocalDestination%TEST;%LocalDestination%" it's like an assignement? why we put the % at the end?
$LocalDestination$TEST = $LocalDestination
%GenericEnvironementName% will be $GenericEnvironementName
%SettingsPath%/Env >>> $SettingsPath/Env?
Variables in dos bat files are delimited with %, before AND after. So %VAR% is replaced by the value of VAR.
set PATH="${PATH1};${PATH_NAME};" assigns the values of PATH1 and PATH_NAME to variable PATH, separated by ;.
In Bash you would write: export PATH="$PATH1;$PATH_NAME"
Therefore, yes, any variable referencing is bash is done with $ before the variable name. So %TATA% becomes $TATA.
Example: %SettingsPath%/Env --> ${SettingsPath}/Env

Dockerfile: COPY / ADD with space character in path (Windows)

In Windows 10 (cmd) I'm trying to copy a file from a subfolder containing a space character in its name.
First I tried to use quotation marks:
FROM jfloff/alpine-python:2.7
COPY "Folder 1/File.txt" "Dir 1"
Error message:
failed to process "\"Folder": unexpected end of statement while looking for matching double-quote
"JSON" format (skipped the first line):
COPY ["Folder 1/File.txt" "Dir 1"]
Error message:
failed to process "[\"Folder": unexpected end of statement while looking for matching double-quote
Trying to escape with a single backslash:
COPY "Folder\ 1/File.txt" "Dir\ 1"
Error message:
failed to process "\"Folder\\": unexpected end of statement while looking for matching double-quote
Trying to escape with a double backslash:
COPY "Folder\\ 1/File.txt" "Dir\\ 1"
Error message:
failed to process "\"Folder\\\\": unexpected end of statement while looking for matching double-quote
Also tried a suggestion to use %20 instead of space:
COPY ["Folder%201/File.txt" "Dir%201"]
Error message:
COPY failed: no source File
Escape character replacement:
# escape=`
COPY "Folder` 1/File.txt" "Dir 1"
Error message:
failed to process "\"Folder`": unexpected end of statement while looking for matching double-quote
The same, but without quotes:
#escape=`
COPY Folder` 1/File.txt Dir` 1
Error message:
COPY failed: stat /var/lib/docker/tmp/docker-builder082039614/Folder: no such file or directory
Method with packing / unpacking using a tar archive (I'm not happy with that idea).
It should be possible, shouldn't it?
Maybe you can use ARG to help you, like this:
Dockerfile:
FROM jfloff/alpine-python:2.7
ARG src="Folder 1/File.txt"
ARG target="Dir 1/"
COPY ${src} ${target}
BTW, a / has to be add at the end of Dir 1 if you treat really want to treat it as a folder.
And, JSON format is also ok, just you miss ,, it should be:
FROM jfloff/alpine-python:2.7
COPY ["Folder 1/File.txt", "Dir 1/"]
Update for your comments:
In official guide, it said:
When copying files or directories that contain special characters (such as [ and ]), you need to escape those paths following the Golang rules to prevent them from being treated as a matching pattern.
So, for your case, it should be:
FROM jfloff/alpine-python:2.7
ARG src="[[]Folder 1]/__SLIM_TEMPLATE.mm"
ARG target="[Folder 1]/"
COPY ${src} ${target}
Or:
FROM jfloff/alpine-python:2.7
COPY ["[[]Folder 1]/__SLIM_TEMPLATE.mm", "[Folder 1]/"]
agree it's annoying to get files from docker that have special characters,
had similar experience and after hours of try and errors, I came up with this simple solution that I now use since then.
tar/zip the files and then "docker cp" them easily without having to worry about dozens of []'"..
tar files in your container machine:
#zipped
tar czf myfiles.tar.gz file1.txt dir24 file_xyz
#or unzipped
tar -cvf myfiles.tar file1.txt dir24 file_xyz
Then copy them to local directories in your windows machine.
open cmd in windows
run
# docker cp ABCD_CONTAINER_NAME:PATH/myfiles.tar.gz d:/TARGETDIR/
check if files have been transferred in d:/TARGETDIR/
The solution is the follow:
Docker has updated it's COPY command. So you can use brackets and your filenames can have whitespaces. Read more here (official docs)

Bashrc script does not change directory on manipulated string that contains space

I'm trying to develop a fast solution to navigate on folders between Windows and Cygwin.
On windows the path looks like this:
C:\Users\foo\my folder\
When I need to get to the same folder on Cygwin I need to use the following format
/cygdrive/c/Users/foo/my\ folder/
Every time I'm on explorer I need to copy the link and edit it.
So I made a simple script for my .bashrc file
It works fine for nonspace containing paths, but when I try to replace the space with '\ ', the link does not find the directory.
I tried to echo the path, copied and pasted to the terminal and it works.
But this does not work:
function cdWinPath(){
if [ `expr index "$1" C:\\` == '1' ]
then
length=`expr length "$1"`
pathSubs=${1:2:$length}
cygPath="/cygdrive/c${pathSubs//\\/\/}"
cygPath="${cygPath// /\\\ }" #piece of code that replaces blank space
echo "cd $cygPath"
cd "$cygPath" # shows 'No such file or directory'
else
echo "Could not understand path $1"
fi
}
The shell parses quotes and escapes before expanding variables, so quotes and escapes in variables' values don't get treated as such, just as normal characters. So don't try to embed escapes in the variable, just put double-quotes around the variable reference.
Some other suggestions: Rather than messing around with expr, just use the shell's built-in string manipulation. When you do need to capture output from another program, use $( ) instead of backticks. Finally, the function keyword is nonstandard, just use () after the name to define a function. Something like this:
cdWinPath() {
local cygPath="${1/#[Cc]:\\//}" # Replace 'C:\' or 'C:\' prefix with just '/'
cygPath="${cygPath//\\//}" # Replace any other '\' with '/'
cd "$cygPath"
}
Note, however, that this requires the windows path to be passed correctly to the function. That means that backslashes and/or spaces in the path must be properly escaped or quoted (meaning that you must double-quote any variable references).
cdWinPath 'C:\Users\foo\my folder\' # This works
cdWinPath C:\\Users\\foo\\my\ folder\\ # So does this
cdWinPath C:\Users\foo\my folder\ # This does not work
cdWinPath "$somevar" # This works, provided `somevar` is properly set
cdWinPath $somevar # This does not work
If you want it to tolerate the path being passed incorrectly, you can make it a little more tollerant by replacing the first line with:
local cygPath="${*/#[Cc]:\\//}"
Using ${*...} instead of $[1...} will take all parameters passed to the function and mash them together with spaces between, on the assumption that they were originally one string that got split on spaces. This will sometimes work, but not always. For example, this command:
cdWinPath C:\Users\foo\my folder\
... will still not work because the shell will interpret the backslashes as escapes before the function even gets the string, and there's no way to fix that.

shell concat directory and interpret caractere

I try to loop in a directory to read file (*.pem)
i MUST USE path="/opt/test" . How concat path with *.pem (all the file in the directory with the extension pem)
for fichierCourant in $path/(*.pem) or "$path/*.pem" or `$path/*.pem` or
'$path/*.pem'
do
..
done
Nothing works?
Thanks
when path can contain a space or tab character must be between double quotes
for fichierCourant in "$path/"*.pem; do
Try without any quote, this works if none of the files does not contains any space.
for file in $path/*.pem
do
echo $file
done
With double quotes "$path/*.pem", though expands but consider all files as a single variable.
Additional Info:
'Single quote' : doesn't expands the variable
backtick (or) backquote: consider the content as a command

TeamCity - passing parameter values with whitespace to command line

I'm passing some TeamCity parameters to the command line build step. The problem comes when the parameter value contains spaces, e.g.:
%env.TEAMCITY_BUILDCONF_NAME% ---> My TC Project
Is there a way to replace white spaces with some other character, for example underscore?
%env.TEAMCITY_BUILDCONF_NAME% ---> My_TC_Project
You can typically keep using the white spaces if you wrap the parameter in double quotes:
%program.files.dir% => C:\Program Files (x86)
Executable: dir
Parameters: "%program.files.dir%"
I don't know how to replace spaces with underscore, but I had an issue with whitespaces.
In a TeamCity build step, I was trying to run an sqlcmd as Executable with Parameters
-S %sql_server% -U %sql_username% -P %sql_password%
-i "custom_script.sql" -d "%custom_db%"
-v DealerName="%DealerName%"
where DealerName was "Great Dealer Ltd" but it didn't work with white spaces, even with double quotes.
It fixed the issue by setting it as a Custom Script like
sqlcmd -S %sql_server% -U %sql_username% -P %sql_password%
-i "custom_script.sql" -d "%custom_db%"
-v DealerName="%DealerName%"
and (thanks to my boss suggestion) it worked like charm.
Even if is not the precise answer to your question, it could be useful for similar issues.
String given below worked for me.
%env.TEAMCITY_BUILDCONF_NAME% ---> "My\ TC\ Project"

Resources