Renaming file via UNC path - vb.net

I need to have my VB.NET program rename a file over the network.
Microsoft says that My.Computer.FileSystem.RenameFile does not work if the file path starts with two backslashes ("\\"). So, what other way is there of doing this? I just need to rename a file in the domain, for instance:
rename("\\domain\1\exemple.txt", "\\domain\1\exemple2.txt")

The second parameter for rename should be just the file name eg:
My.Computer.FileSystem.RenameFile("C:\Test.txt", "SecondTest.txt")
So try changing your code to this:
My.Computer.FileSystem.RenameFile(#"\\domain\1\exemple.txt", "exemple2.txt")
Also beware of escaping because \ is an escape character, so add a # before any string that contains \. This will cause it to ignore escaping and therefore will treat \ as a normal character

Related

Escaping filename spaces with singularity %files directive

I have a file with spaces in its name that I want to copy to my container:
%files
/tmp/some file with spaces /usr/local/bin
I have tried using 1 or 2 backslashes paired with single or double quotes, but the full filename is not recognized. While I can change the filename, I was curious if there is a way to correctly escape the filename?
I agree, it does not appear they can be escaped. (There's a similar problem with trying to mount file names with :.) You might try raising an issue in the github repo.

batch-file variable %CD% adding a backslash when run from drive root

I have a problem with the variable %CD% in a batch-file. It adds a backslash if the script is run from the root of a drive.
as an example: updatedir=%CD%\Update & echo %updatedir% will return something like
From a folder E:\New Folder\Update
From a drive root E:\\Update
Is there any way to get rid of the extra backslash if run from root?
Yes %CD% only has a trailing \ if the current directory is the root. You could get rid of any trailing backslash that might be there. But there is a simpler solution.
Use the undocumented %__CD__% instead, which always appends the trailing backslash. This makes it easy to build a clean path, regardless of the current directory.
set "updatedir=%__CD__%Update
You can do something like this:
set "CurrentDir=%CD%"
if "%CD:~-1%"=="\" set "CurrentDir=%CD:~0,-1%"
Since you don't want to go changing the system variable %CD%, this sets a new variable %CurrentDir% to the current value of %CD%. Then, it checks to see if the last character in %CD% is a \, and if it is, sets %CurrentDir% to the value of %CD%, minus the last character.
This question/answer has more information on using substrings in batch files.
replace every occurence of \\ with \.
echo %updatedir:\\=\%

What is the meaning of the file names flanked by the '#' sign and how can I remove them?

When I do the 'ls' command in the terminal on my Raspberry Pi 2, I see different types of names of files, some like "#example.cpp#", as well as others like "homework1.cpp~".
What do these two file types mean, and how can I get rid of them? Simply using the 'rm' command doesn't seem to be working for me. Thanks!
Some applications will create a copy of a file and use special characters when creating the filename for the copy. For instance some text editors will make a copy of a file you are starting to edit by using the same name and adding a tilde character (~) to the end of the file. That way you will have a backup of the file that you are about to edit.
Another reason would be if an application is processing the file into a temporary file with the temporary file then being used for the next step. For example perhaps the C/C++ compiler is reading the file homework1.cpp with the C Preprocessor to generate the temporary file #homework1.cpp# which is then compiled by the compiler to generate the object code file.
I am not familiar with raspberry pi so am not sure as to what may be creating the filenames with the pound sign (#) on the front and back. Perhaps it is the C++ compiler. I am pretty sure the files with the tilde character on appended to the end of the file name is a back file from vi or vim containing a copy of the file at the time it was last opened with the text editor.
One thing that you could do is to look in those files to see what is there using a Linux command or a text editor. If you use a text editor I would copy the file to another folder as a back up and then look at it there.
Edit: Someone just posted and then deleted an answer which also mentioned about how to remove these files.
What I read was that the rm command is used however for some kinds of special characters you will need to use quotes around the name and you may also need to use an escape to escape certain special characters.
The command shell reads the command line you type in and makes changes to the text before passing it on to the command you type in. So if the filename has a space in it, say jj Johny then when you remove the file you have to specify rm "jj Johny" since spaces are used by the command processor to separate out arguments.
The other poster mentioned that you had to escape out the pound sign (#) using the back slash character in order to prevent it from being modified by the command processor.

Objective-C - How to convert file path to shell conform file path

I want to execute a shell command (I want to touch a file). I use system("shell command") to execute the command.
For example I want to touch the file at path /Users/username/New Folder/. Now I need to convert the NSString in a format that is conform to shell commands like /Users/username/New\ Folder.
Is there any method that does a conversion like this?
NOTE: It is NOT just replacing a whitespace with \. If you have a special character in the path like /Users/username/Folder(foo)/ the "shell path" looks like this /Users/username/Folder\(foo\)/
There is no need to convert the path, you can surround it in single quotes. Just use:
touch 'path'
You can enclose the parameters that contain spaces with " " marks.
touch "/Users/username/New Folder/"
At least this works at the shell prompt
Don't use system. It's insecure and unpredictable. Surrounding the string with quotes is not sufficient.
Use the execve style functions instead. They are simple and secure.

Is there any limitation in giving file name in Unix?

We are using crontab to schedule jobs and it was not picking the files for processing that have [ or ] or ¿ . Is there any limitation in giving file name or these characters means something in UNIX? Is there any other variables like these we shouldnt use in file name?? Thanks in advance.
Following are general rules for both Linux, and Unix (including *BSD) like systems:
All file names are case sensitive. So filename vivek.txt Vivek.txt VIVEK.txt all are three different files.
You can use upper and lowercase letters, numbers, "." (dot), and "_" (underscore) symbols.
You can use other special characters such as blank space, but they are hard to use and it is better to avoid them.
In short, filenames may contain any character except / (root directory), which is reserved as the separator between files and directories in a pathname. You cannot use the null character.
No need to use . (dot) in a filename. Some time dot improves readability of filenames.
And you can use dot based filename extension to identify file. For example:
.sh = Shell file
.tar.gz = Compressed archive
Most modern Linux and UNIX limit filename to 255 characters (255 bytes). However, some older version of UNIX system limits filenames to 14 characters only.
A filename must be unique inside its directory. For example, inside /home/vivek directory you cannot create a demo.txt file and demo.txt directory name. However, other directory may have files with the same names. For example, you can create demo.txt directory in /tmp.
Linux / UNIX: Reserved Characters And Words
Avoid using the following characters from appearing in file names:
/
>
<
|
:
&
Please note that Linux and UNIX allows white spaces, <, >, |, \, :, (, ), &, ;, as well as wildcards such as ? and *, to be quoted or escaped using \ symbol.
It will be good if you can avoid white spaces in your filename. It will make your scripting a lot more easier.
I got the answer from this link. I am just pasting it here so that this info will be available even if that website goes down.
The only characters that are actually illegal in *nix filenames are / (reserved as the directory separator) and NUL (because it's the C string terminator). Everything else is fair game, although various utilities may fail on certain characters - typically characters that have special meaning to the shell. These will need quoting or escaping to be handled correctly.