Translate drive letter to full path for CD burner - c++

In the following code, I am trying to get a folder location from the user. However, when I selected E:\ in the folder browser, szAbsolutePath doesn't give me the path for the CD burner temporary folder. This prevents me from saving to this location. However, if I choose something like E:\folder1\, I get the full path and can write out files to this location.
char szDisplayName[MAX_PATH];
BROWSEINFO binfo;
memset(&binfo, 0, sizeof(BROWSEINFO));
binfo.lpszTitle = strTitle.c_str();
binfo.hwndOwner = hwndOwner;
binfo.pszDisplayName = szDisplayName;
binfo.ulFlags = BIF_USENEWUI | BIF_NEWDIALOGSTYLE | BIF_BROWSEFILEJUNCTIONS | BIF_RETURNONLYFSDIRS;
PIDLIST_ABSOLUTE pidl = SHBrowseForFolder(&binfo);
if(pidl) {
char szAbsolutePath[MAX_PATH];
SHGetPathFromIDList(pidl, szAbsolutePath);
}
How can I always get the full path when the user chooses the root of the CD-R drive?

You can use the ICDBurn::GetRecorderDriveLetter function to get the recorder's drive letter - then it's trivial to compare against the string you get back from GetSaveFileName(). If you do get back a path on the CD burner, you can use SHGetFolderLocation with CSIDL_CDBURN_AREA to get the path of the staging area - then it's simply a matter of replacing the drive letter at the beginning of the path string with the path of the staging area.

Related

Directory Open Issue when passing path as parameter

I having an issue with my program which is expected to either ask a user to input a directory path or to use an predefine directory path as an argument so that my program can open that directory.
Asking a user to input the directory path (C:\Users\Desktop\Test) works fine. But I am having an issue when I am passing the file path as argument.
It should be passed as this:
char location[1000] = "C:\Users\Desktop\Test";
if ((dir = opendir (location)) != NULL){
......
}
But using argument, my program can only open the directory if location is initialised and assigned as this:
char location[1000] = "C:\\Users\\Desktop\\Test";
if ((dir = opendir (location)) != NULL){
......
}
However, I need to concatenate location with a filename in another part of my program so that it becomes: C:\Users\Desktop\Test\file.txt
It won`t work with: C:\\Users\\Desktop\\Test\\file.txt.
char location[1000] can`t be modified as it works well with my code when opening or closing directory.

listing directories having "-" character in directories name

I want to list the directories in current directory having "-" character in directories name. I used os.listdir(path). Its giving me error :
"WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect:"
Any help will be greatly appreciated
Use os.listdir to get directory contents and then filter using os.path.isdir to check if each item is a dir:
dirs_with_hyphen = []
for thing in os.listdir(os.getcwd()):
if os.path.isdir(thing) and '-' in thing:
dirs_with_hyphen.append(thing)
print dirs_with_hyphen # or return, etc.
And that can be shortened using list comprehension:
dirs_with_hyphen = [thing for thing in os.listdir(os.getcwd()) if os.path.isdir(thing) and '-' in thing]
I'm using os.getcwd but you could pass in any string that represents a folder.
If you're getting an error about the filename being incorrect, you're probably not escaping correctly or it's not pointing to the right folder (absolute vs relative path issue).
I did some testing and I managed to get your error. I don't know if this is what you did to get the error though since no example has been provided.
What I did though is give an invalid drive path. Not one that could be valid and doesn't exist, one that is always wrong eg.'C::\' or 'CC:\' just anything that's not 'C:\'. As for your question.
Path should generally look like this, prefixing with r to ignore backslash as escape character or double backslash.
import os
path = r"C:\Users\Steven\Documents\"
path = "C:\\Users\\Steven\\Documents\"
for file in os.listdir(path):
if os.path.isdir(path+file) and '-' in file:
print path + file
#List Comp
[path+file for file in os.listdir(path) if os.path.isdir(path+file) and '-' in file]

Python - Counting the number of files and folders in a directory

I've got a python script that deletes an entire directory and its subfolders, and I'd like to print out the number of files and folders removed. Currently, I have found some code from a different question posed 2010, but the answer I receive back is 16... If I right-click on the the folder it states that theres 152 files, 72 folders...
The code I currently have for checking the directory;
import os, getpass
user = getpass.getuser()
copyof = 'Copy of ' + user
directory = "C:/Documents and Settings/" + user
print len([item for item in os.listdir(directory)])
How can I extend this to show the same number of files and folders that there actually are?
To perform recursive search you may use os.walk.
os.walk(top, topdown=True, onerror=None, followlinks=False)
Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at
directory top (including top itself), it yields a 3-tuple (dirpath,
dirnames, filenames).
Sample usage:
import os
dir_count = 0
file_count = 0
for _, dirs, files in os.walk(dir_to_list_recursively):
dir_count += len(dirs)
file_count += len(files)
I was able to solve this issue by using the following code by octoback (copied directly);
import os
cpt = sum([len(files) for r, d, files in os.walk("G:\CS\PYTHONPROJECTS")])

Python: copy long file path Shutil.copyfile

I want to copy too long paths with python using shutil.copyfile.
Now I read this Copy a file with a too long path to another directory in Python page to get the solution. I used:
shutil.copyfile(r'\\\\?\\' + ErrFileName,testPath+"\\"+FilenameforCSV+"_lyrErrs"+timestrLyr+".csv")
to copy the file but it gives me an error : [Errno 2] No such file or directory: '\\\\?\\C:\\...
Can anyone please let me know how to incorporate longs paths with Shutil.copyfile, the method I used above should allow 32k characters inside a file path, but I cant even reach 1000 and it gives me this error.
Since the \\?\ prefix bypasses normal path processing, the path needs to be absolute, can only use backslash as the path separator, and has to be a UTF-16 string. In Python 2, use the u prefix to create a unicode string (UTF-16 on Windows).
shutil.copyfile opens the source file in 'rb' mode and destination file in 'wb' mode, and then copies from source to destination in 16 KiB chunks. Given a unicode path, Python 2 opens a file by calling the C runtime function _wfopen, which in turn calls the Windows wide-character API CreateFileW.
shutil.copyfile should work with long paths, assuming they're correctly formatted. If it's not working for you, I can't think of any way to "force" it to work.
Here's a Python 2 example that creates a 10-level tree of directories, each named u'a' * 255, and copies a file from the working directory into the leaf of the tree. The destination path is about 2600 characters, depending on your working directory.
#!python2
import os
import shutil
work = 'longpath_work'
if not os.path.exists(work):
os.mkdir(work)
os.chdir(work)
# create a text file to copy
if not os.path.exists('spam.txt'):
with open('spam.txt', 'w') as f:
f.write('spam spam spam')
# create 10-level tree of directories
name = u'a' * 255
base = u'\\'.join([u'\\\\?', os.getcwd(), name])
if os.path.exists(base):
shutil.rmtree(base)
rest = u'\\'.join([name] * 9)
path = u'\\'.join([base, rest])
os.makedirs(path)
print 'src directory listing (tree created)'
print os.listdir(u'.')
dest = u'\\'.join([path, u'spam.txt'])
shutil.copyfile(u'spam.txt', dest)
print '\ndest directory listing'
print os.listdir(path)
print '\ncopied file contents'
with open(dest) as f:
print f.read()
# Remove the tree, and verify that it's removed:
shutil.rmtree(base)
print '\nsrc directory listing (tree removed)'
print os.listdir(u'.')
Output (line wrapped):
src directory listing (tree created)
[u'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaa', u'spam.txt']
dest directory listing
[u'spam.txt']
copied file contents
spam spam spam
src directory listing (tree removed)
[u'spam.txt']

Get actual folder path

How I can get actual folder path where my program is without my exe file name in C++?
The following function will give you the application path:
::GetModuleFileName(NULL, szAppPath, MAX_PATH);
Now to extract the folder, you need to find the last backslash:
char szApplicationPath[MAX_PATH] = "";
::GetModuleFileName(NULL, szApplicationPath, MAX_PATH);
//Get the folder part
CString strApplicationFolder;
strApplicationFolder = szApplicationPath;
strApplicationFolder = strApplicationFolder.Left(strApplicationFolder.ReverseFind("\\"));