I'm trying to make a cross-platform program that move a file to a folder inside the user home folder, like this:
os.rename(file_var, destination_folder)
but I can't make it work because I need to put the file name inside the destination var, and the problem is the Windows "\"
what are my options?
do this:
for example you want this path:
'+pathfile+'/output/log.txt'
instead of typing a string, join them like this:
os.path.join(pathfile,"output","log.txt")
NOTE:
according to this post / works both in windows and linux, i always use os.path.join()
Related
I want to know if is it possible to make a word, like drive work the way ~ works in bash.
For example:
mkdir ~/Documents/test_dir makes the folder /home/user/Documents/test_dir
My goal is that:mkdir drive/test_dir makes the folder /home/user/Documents/Google-Drive/test_dir
You could simply use a variable:
drive=/home/user/Documents/Google-Drive
Then echo "$drive/test_dir" prints /home/user/Documents/Google-Drive/test_dir
Right now I have a batch file that sets the PATH variable to all of the required directories.(there is actually a bunch more required directories, i just took them out so the code snippet would not be too long)
#echo off
set PATH=D:/src/trunk/build/bin;D:/src/trunk/build/bin/CoreTools;D:/src/trunk/build/bin/Plugins/Extensions;D:/src/trunk/build/bin/Plugins/CustomUI
set DEBRIEF_INSTALL_DIR=D:/src/trunk/DebriefSuite/D3D_Installation
set READERS=D:/src/trunk/build/bin/CoreTools/Readers
set BINARY_DIR=D:/src/trunk/build
cd D:/src/trunk/build/bin
start PROGRAM.exe --ConfigFile="D:/src/trunk/DebriefSuite/Installation/config/Projects/config.xml" ^
--Mode-File="D:/src/trunk/DebriefSuite/Installation/config/Projects/Common/anotherconfig.xml" ^
--Env:Bin="D:/src/trunk/build/bin"
cd D:/src/trunk
It works fine, but all of the directories are hard-coded. This needs to be able to work for other computers that might have their root directory in a different location. I need to be able to pass in a root directory (something like "D:\different_root_location") and substitute it in to each place in this code that currently says "D:\src\trunk". The problem is, i am not sure what the syntax would be for something like this. I am new to writing batch files. I tried doing something like
SET ROOTDIR=%1 .....
And then
set PATH=%ROOTDIR%/build/bin;%ROOTDIR%/build/bin/CoreTools;%ROOTDIR%/build/bin/Plugins/Extensions;%ROOTDIR%/build/bin/Plugins/CustomUI ..........
start PROGRAM.exe --ConfigFile="%ROOTDIR%/DebriefSuite/Installation/config/Projects/config.xml" ^
but it did not work. I'm not really sure how to make this work! Also, any links to good sources of information about writing batch files in general would be extremely helpful since i am starting out!
Change your line so it includes the original path as well.
From this:
set PATH=D:/src/trunk/build/bin...
to this: (and Windows uses \ and not / even though it works in some cases)
set PATH=%path%;D:\src\trunk\build\bin....
I want to open a file in a windows program using R, but specifying the program rather than the default for the file extension, and for a file not necesarrily in my current R session home directory (this one getwd())
From looking at the documentation, using shell(), should be the way, but I seem to have an issue with the way R references the home directory or the way I'm writing the string.
e.g.
This works ok in the cmd "run" in windows: excel e:\test.xlsx
but using this
route <- "e:\\test.xlsx"
shell(paste("excel " , route, sep=""), flag="")
seems to get to excel (excel copyright notice is printed), but also prints the home directory and doesn't open the file in route. Thanks for any help.
Your command does the same for me. However, this works:
shell(paste("start", "excel", route))
I have written following code in vb.net to delete the file.
If File.Exists(strPath & "\ReportEng.ini") = True Then
File.SetAttributes(strPath & "\ReportEng.ini", FileAttributes.Normal)
File.Delete(strPath & "\ReportEng.ini")
End If
File.Copy("\\192.168.0.1\SAP_Shared\AddonExtra\ReportEng.ini", strPath & "\ReportEng.ini")
This code works perfectly in windows xp. But in Windows 7,I can not delete it. This OS is hectic OS from developer's point of view. Some or other problem occurs and Microsoft has not considered the developer while building this OS.
How do I delete file in Windows 7 ?
It's so easy to do so;
If My.Computer.FileSystem.FileExists("C:\somefile.ext") Then 'Check whether file exists
My.Computer.FileSystem.DeleteFile("C:\somefile.ext") 'Delete the file!
End If
Have a nice day!
You don't need to delete the file: there is an overload File.Copy Method (String, String, Boolean) which allows overwriting.
You didn't say what error you get. I suspect it is because the user doesn't have write access to the directory. You should probably be using a subdirectory of the directory returned by Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) or maybe .LocalApplicationData, and definitely not the directory containing the program.
Also, using Path.Combine(strPath, "ReportEng.ini") is how you're meant to combine paths - it'll take care of, e.g., the trailing path separator for you.
The preferred method for interfacing with the Windows file system uses the following namespace:
Imports Microsoft.VisualBasic.FileIO.FileSystem
To delete a file:
Dim FileLocation As String = strPath & "\ReportEng.ini"
If Not GetDirectoryInfo(FileLocation).Exists Then
GetFileInfo(FileLocation).Delete()
End If
I am using VS installer to create an installer for my application. I have a custom action which will pass the installation path to the code behind like the following : /path="[TARGETDIR]\" .
Inside my installer Class I am displaying the installation path in the Install() method as path = Context.Parameters["path"]; MessageBox.Show(pathh);
however, what is displayed is something like this:c:\Program Files(x86)\Manufacturer\Applicationname\\
So I don’t know how this extra backslash is added and do not know how to remove it . Any idea?
it is worth mentioning that i was able to creat a file in the installtion path using "pathh"
If you are just want to display the path to the user u can use the following simple trick which works fine in your case: string path = pathh.Remove(pathh.Length-1); MessageBox.Show(path); Hope it helps.
Have you tried making your custom action without the trailing backslash?
Replace
/path="[TARGETDIR]\"
with
/path="[TARGETDIR]"