PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Thursday, December 22, 2022

[FIXED] What does 'wb' mean in this code, using Python?

 December 22, 2022     file, python, syntax     No comments   

Issue

Code:

file('pinax/media/a.jpg', 'wb')

Solution

File mode, write and binary. Since you are writing a .jpg file, it looks fine.

But if you supposed to read that jpg file you need to use 'rb'

More info

On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files.



Answered By - YOU
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, November 11, 2022

[FIXED] how to create /proc file inside kernel module?

 November 11, 2022     c, file, filesystems, linux-kernel, proc     No comments   

Issue

I want to save some information in a kernel module. I have seen similar question to mine here in stackoverflow, however mine is slightly different. Assuming I am using this code to write in a /proc file. How should I call it inside one of my kernel module? I have a custom kernel module called mymodule which is not the main file (does not have init_module()) inside it the below function is called. In this case what should be the input value to the function like value of file? Basically is it possible to create a /proc file inside a kernel module?

int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data)


Solution

It is definitely possible to add a proc entry in a kernel module but you might be misunderstanding how file handling works in the kernel.

When you create a file in proc, you're not actually 'creating' a file like you would in userspace. You're registering a few callbacks that are called when userspace programs request your file.

That means, when userspace programs request a read, your file read callback has to provide data. When a userspace program requests a write, your file write callback has to handle it.

If you want to use it like a conventional file to store information, you have to allocate the required space and have your read callback copy data from there.



Answered By - tangrs
Answer Checked By - Marie Seifert (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, November 6, 2022

[FIXED] How read the correct lines from this text file with a python program, and then create a .py file by filling in the data extracted from the .txt file?

 November 06, 2022     file, python, regex, regex-group, writefile     No comments   

Issue

Text file to be read (the real one contains more numbers), called number_info.txt

veinti tres
23

veinti dos
22

veinti uno
21

veinte
20

tres
3

dos
2

uno
1

This is the code (I need help with this)

import re

def auto_coding_text_to_number():

    with open('number_info.txt', 'r') as f:

        #lines 0, 3, 6, 9, 12, 15, 18, ...
        coloquial_numbers = []

        #lines 0+1, 3+1, 6+1, 9+1, 12+1, 15+1, 18+1, ... 
        symbolic_numbers = []


    n = 0
    with open('number_to_text.py', 'w') as f:
        f.write('import re\n\ndef number_to_text_func(input_text):\n')
       
        #write replacement lines based on regex
        if(" " in coloquial_numbers[n]):
            #for example write this line:   "    input_text = re.sub(r"veinti[\s|-|]*tres", "23", input_text)"
        
        if not (" " in coloquial_numbers[n]):
            #for example write this line:   "    input_text = re.sub("tres", "3", input_text)"
            
        f.write("    return(input_text)\n    input_text = str(input())\n 
   print(number_to_text_func(input_text))")

        n = n + 1

auto_coding_text_to_number()

And this is the correct file, called number_to_text.py, that should be written by the other script

import re

def number_to_text_func(input_text):
    input_text = re.sub(r"veinti[\s|-|]*tres", "23", input_text)
    input_text = re.sub(r"veinti[\s|-|]*dos", "22", input_text)
    input_text = re.sub(r"veinti[\s|-|]*uno", "21", input_text)
    input_text = re.sub("tres", "3", input_text)
    input_text = re.sub("dos", "2", input_text)
    input_text = re.sub("uno", "1", input_text)

    return(input_text)

input_text = str(input())
print(number_to_text_func(input_text))

EDIT:

The lines inside the .txt file are structured like this

"veinti tres"  <---- line 0
"23"           <---- line 1
"veinti dos"   <---- line 2
"22"           <---- line 3
"veinti uno"   <---- line 4
"21"           <---- line 5
"veinte"       <---- line 6
"20"           <---- line 7
"tres"         <---- line 8
"3"            <---- line 9

Then I suggested separating them into 2 groups and storing them in 2 lists

#lines 0, 3, 6, 9, 12, 15, 18, ...
coloquial_numbers = ["veinti tres", "veinti dos", "veinti uno", "veinte", "tres"]

#lines 0+1, 3+1, 6+1, 9+1, 12+1, 15+1, 18+1, ...
symbolic_numbers = ["23", "22", "21", "20". "3"]


body_template = """    input_text = re.sub(r"{}", "{}", input_text)\n"""

And then the body of the function should be structured like this

input_text = re.sub(coloquial_numbers[n].replace(' ', '[\s|-|]'), symbolic_numbers[n], input_text)

Getting something like this in the function body of the output file

def number_to_text(input_text):
    input_text = re.sub(r"veinti[\s|-|]*tres", "23", input_text)
    input_text = re.sub(r"veinti[\s|-|]*dos", "22", input_text)
    input_text = re.sub(r"veinti[\s|-|]*uno", "21", input_text)
    input_text = re.sub("tres", "3", input_text)

    return(input_text)

Solution

I omitted the reading/write steps for sake of simplicity. No rule(s) to specify the body of the meta function is given so I did a guess.

import re 

# body-component of the meta-code
body_template = """    input_text = re.sub(r"{}", "{}", input_text)\n"""

# read from file
with open('number_info.txt', 'r') as fd:
    text = fd.read()

# update body
body = ''
for n_text, n in re.findall(r'\n*([a-z\s]+)\n(\d+)', text):
    body += body_template.format(n_text.replace(' ', '[\s|-|]'), n)

# other components of the meta-code
header = """import re

def number_to_text_func(input_text):
"""

tail = """\n    return(input_text)

input_text = str(input())
print(number_to_text_func(input_text))"""

# merge together texts to be saved to file
meta_code = header + body + tail
print(meta_code)

Output (content of number_to_text.py)

import re

def number_to_text_func(input_text):
    input_text = re.sub(r"treinta[\s|-|]y[\s|-|]uno", "31", input_text) # <-
    input_text = re.sub(r"veinti[\s|-|]tres", "23", input_text)
    input_text = re.sub(r"veinti[\s|-|]dos", "22", input_text)
    input_text = re.sub(r"veinti[\s|-|]uno", "21", input_text)
    input_text = re.sub(r"veinte", "20", input_text)
    input_text = re.sub(r"tres", "3", input_text)
    input_text = re.sub(r"dos", "2", input_text)
    input_text = re.sub(r"uno", "1", input_text)

    return(input_text)

input_text = str(input())
print(number_to_text_func(input_text))

From the comments:

read file per line, no regex

with open('number_info.txt', 'r') as fd:
    lines = fd.read().split('\n')

symbolic_numbers, coloquial_numbers = [], []
for i, line in enumerate(lines):
    if i % 3 == 0:
        coloquial_numbers.append(line)
    elif i % 3 == 1:
        symbolic_numbers.append(line)

or read file with slices

with open('number_info.txt', 'r') as fd:
    lines = fd.read().split('\n')

coloquial_numbers = lines[::3]
symbolic_numbers = lines[1::3]


Answered By - cards
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, November 2, 2022

[FIXED] How to download file in swift?

 November 02, 2022     download, file, ios, swift     No comments   

Issue

I just started learning apple swift programming for iOS coming from android. I basically can now read and manipulate swift code and also learned some common classes used in iOS swift programming but still having some confusion with the syntax and everything.

I'm trying to download file. Like, lets just say coming from this URL

var url = "http://www.mywebsite.com/myfile.pdf"

in a button click. Maybe with visual progress too

Through searching here in stackoverflow, I stumbled upon Alamofire. I might try it but I'm not sure if this is the best way for me to do it.

So, I would like to ask how and what are my options (iOS7 and iOS8) in achieving my goal. Also, pros and cons would be awesome!


Solution

Example downloader class without Alamofire:

class Downloader {
    class func load(URL: NSURL) {
        let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
        let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
        let request = NSMutableURLRequest(URL: URL)
        request.HTTPMethod = "GET"
        let task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
            if (error == nil) {
                // Success
                let statusCode = (response as NSHTTPURLResponse).statusCode
                println("Success: \(statusCode)")

                // This is your file-variable:
                // data
            }
            else {
                // Failure
                println("Failure: %@", error.localizedDescription);
            }
        })
        task.resume()
    }
}

This is how to use it in your own code:

class Foo {
    func bar() {
        if var URL = NSURL(string: "http://www.mywebsite.com/myfile.pdf") {
            Downloader.load(URL)
        }
    }
}

Swift 3 Version

Also note to download large files on disk instead instead in memory. see `downloadTask:

class Downloader {
    class func load(url: URL, to localUrl: URL, completion: @escaping () -> ()) {
        let sessionConfig = URLSessionConfiguration.default
        let session = URLSession(configuration: sessionConfig)
        let request = try! URLRequest(url: url, method: .get)

        let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
            if let tempLocalUrl = tempLocalUrl, error == nil {
                // Success
                if let statusCode = (response as? HTTPURLResponse)?.statusCode {
                    print("Success: \(statusCode)")
                }

                do {
                    try FileManager.default.copyItem(at: tempLocalUrl, to: localUrl)
                    completion()
                } catch (let writeError) {
                    print("error writing file \(localUrl) : \(writeError)")
                }

            } else {
                print("Failure: %@", error?.localizedDescription);
            }
        }
        task.resume()
    }
}


Answered By - Devran Cosmo Uenal
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to read files from the sub folder of resource folder with using toURI

 November 02, 2022     file, java, spring, spring-boot     No comments   

Issue

How to read files from the sub folder of resource folder with using URI

    How to read files from the sub folder of resource folder.

I have some json file in resources folder like :

src 
    main
        resources 
            jsonData
                d1.json
                d2.json
                d3.json

Now I want to read this in my class which is

src 
    main
        java
            com
                myFile
                    classes 

here is what I am trying.

 File[] fileList = (new File(getClass().getResource("/jaonData").toURI())).listFiles();
    
            for (File file : listOfFiles) {
                if (file.isFile()) {
                   // my operation of Data.
                }
            }

my things are working fine but the problem what I am getting is i don't want to use toURI as it is getting failed.


Solution

You're probably not using Spring Boot, so how to read folder from the resolurces files in spring boot, : Getting error while running from Jar won't help you much.

I'll repeat myself from a comment to that question:

Everything inside a JAR file is not a file, and cannot be accessed using File, FileInputStream, etc. There are no official mechanisms to access directories in JAR files.

Fortunately, there is a non-official way, and that uses the fact that you can open a JAR file as a separate file system.

Here's a way that works both with file-based file systems and resources in a JAR file:

private void process() throws IOException {
    Path classBase = getClassBase();
    if (Files.isDirectory(classBase)) {
        process(classBase);
    } else {
        // classBase is the JAR file; open it as a file system
        try (FileSystem fs = FileSystems.newFileSystem(classBase, getClass().getClassLoader())) {
            Path root = fs.getPath("/");
            return loadFromBasePath(root);
        }
    }
} 

private Path getClassBase() {
    ProtectionDomain protectionDomain = getClass().getProtectionDomain();
    CodeSource codeSource = protectionDomain.getCodeSource();
    URL location = codeSource.getLocation();
    try {
        return Paths.get(location.toURI());
    } catch (URISyntaxException e) {
        throw new IllegalStateException(e);
    }
}

private void processRoot(Path root) throws IOException {
    // use root as if it's either the root of the JAR, or target/classes
    // for instance
    Path jsonData = root.resolve("jsonData");
    // Use Files.walk or Files.newDirectoryStream(jsonData)
}

If you don't like using ProtectionDomain, you can use another little trick, that makes use of the fact that every class file can be read as resource:

private Path getClassBase() {
    String resourcePath = '/' + getClass().getName().replace('.', '/') + ".class";
    URL url = getClass().getResource(resourcePath);
    String uriValue = url.toString();
    if (uriValue.endsWith('!' + resourcePath)) {
        // format: jar:<file>!<resourcePath>
        uriValue = uriValue.substring(4, uriValue.length() - resourcePath.length() - 1);
    } else {
        // format: <folder><resourcePath>
        uriValue = uriValue.substring(0, uriValue.length() - resourcePath.length());
    }
    return Paths.get(URI.create(uriValue));
}


Answered By - Rob Spoor
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to read the file without encoding and extract desired urls with python3?

 November 02, 2022     encoding, file, python-3.x, utf-8     No comments   

Issue

Environment :python3.
There are many files ,some of them encoding with gbk,others encoding with utf-8. I want to extract all the jpg with regular expression

For s.html encoding with gbk.

tree = open("/tmp/s.html","r").read()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 135: invalid start byte

tree = open("/tmp/s.html","r",encoding="gbk").read()
pat = "http://.+\.jpg"
result = re.findall(pat,tree)
print(result)

['http://somesite/2017/06/0_56.jpg']

It is a huge job to open all the files with specified encoding,i want a smart way to extract jpg urls in all the files.


Solution

If they have mixed encoding, you could try one encoding and fall back to another:

# first open as binary
with open(..., 'rb') as f:
    f_contents = f.read()
    try:
        contents = f_contents.decode('UTF-8')
    except UnicodeDecodeError:
        contents = f_contents.decode('gbk')
    ...

If they are html files, you may also be able to find the encoding tag, or search them as binary with a binary regex:

contents = open(..., 'rb').read()
regex = re.compile(b'http://.+\.jpg')
result = regex.findall(contents)
# now you'll probably want to `.decode()` each of the urls, but you should be able to do that pretty trivially with even the `ASCII` codec

Though now that I think of it, you probably don't really want to use regex to capture the links as you'll then have to deal with html entities (&amp;) and may do better with something like pyquery

Here's a quick example using pyquery

contents = open(..., 'rb').read()
pq = pyquery.PyQuery(contents)
images = pq.find('img')
for img in images:
   img = pyquery.PyQuery(img)
   if img.attr('src').endswith('.jpg')
       print(img.attr('src'))

Not on my computer with things installed, so mileage with these code samples may vary



Answered By - anthony sottile
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to open file in another directory in java?

 November 02, 2022     directory, file, java     No comments   

Issue

How to open a file that is not present in the current directory but in another directory. For example, I have a folder F:/test and my file is in F:/test/test2/doit.txt and D:/test3/doit2.txt

What to enter in path in parameter while making File object as follows :

File f = new File("/test2/doit.txt");

Solution

Irrespective of which operating system, a file for example, demo.txt can be accessed like

File file = new File("/d:/user/demo.txt");

in Windows where the file is at D:\user\ and

File file = new File("/usr/demo.txt");

in *nix or *nuxwhere the file is at /usr/

Also, a file if wanted to be accessed relatively can be done as (considering the Windows example) :

Suppose I am in the songs directory in D: like:

D:/
|
|---songs/
|   |
|   |---Main.java
|
|---user/
    |
    |---demo.txt

and the code is inside Main.java, then the following code works.

File file = new File("../user/demo.txt");


Answered By - TechSpellBound
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to compare 2 files fast using .NET?

 November 02, 2022     c#, checksum, compare, file     No comments   

Issue

Typical approaches recommend reading the binary via FileStream and comparing it byte-by-byte.

  • Would a checksum comparison such as CRC be faster?
  • Are there any .NET libraries that can generate a checksum for a file?

Solution

A checksum comparison will most likely be slower than a byte-by-byte comparison.

In order to generate a checksum, you'll need to load each byte of the file, and perform processing on it. You'll then have to do this on the second file. The processing will almost definitely be slower than the comparison check.

As for generating a checksum: You can do this easily with the cryptography classes. Here's a short example of generating an MD5 checksum with C#.

However, a checksum may be faster and make more sense if you can pre-compute the checksum of the "test" or "base" case. If you have an existing file, and you're checking to see if a new file is the same as the existing one, pre-computing the checksum on your "existing" file would mean only needing to do the DiskIO one time, on the new file. This would likely be faster than a byte-by-byte comparison.



Answered By - Reed Copsey
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to load a pre-trained Word2vec MODEL File and reuse it?

 November 02, 2022     file, gensim, model, python, word2vec     No comments   

Issue

I want to use a pre-trained word2vec model, but I don't know how to load it in python.

This file is a MODEL file (703 MB). It can be downloaded here:
http://devmount.github.io/GermanWordEmbeddings/


Solution

just for loading

import gensim

# Load pre-trained Word2Vec model.
model = gensim.models.Word2Vec.load("modelName.model")

now you can train the model as usual. also, if you want to be able to save it and retrain it multiple times, here's what you should do

model.train(//insert proper parameters here//)
"""
If you don't plan to train the model any further, calling
init_sims will make the model much more memory-efficient
If `replace` is set, forget the original vectors and only keep the normalized
ones = saves lots of memory!
replace=True if you want to reuse the model
"""
model.init_sims(replace=True)

# save the model for later use
# for loading, call Word2Vec.load()

model.save("modelName.model")


Answered By - AbtPst
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to make file sparse?

 November 02, 2022     file, linux, sparse-file     No comments   

Issue

If I have a big file containing many zeros, how can i efficiently make it a sparse file?

Is the only possibility to read the whole file (including all zeroes, which may patrially be stored sparse) and to rewrite it to a new file using seek to skip the zero areas?

Or is there a possibility to make this in an existing file (e.g. File.setSparse(long start, long end))?

I'm looking for a solution in Java or some Linux commands, Filesystem will be ext3 or similar.


Solution

Some filesystems on Linux / UNIX have the ability to "punch holes" into an existing file. See:

  • LKML posting about the feature
  • UNIX file trunctation FAQ (search for F_FREESP)

It's not very portable and not done the same way across the board; as of right now, I believe Java's IO libraries do not provide an interface for this.

If hole punching is available either via fcntl(F_FREESP) or via any other mechanism, it should be significantly faster than a copy/seek loop.



Answered By - FrankH.
Answer Checked By - David Marino (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to split csv file into multiple files by size

 November 02, 2022     csv, file, java, size, split     No comments   

Issue

in a java project i generate a big csv file (about 500 Mb), and i need to split that file into multiple files of at most 10 Mb size each one. I found a lot of posts similar but any of them answer to my question because in all posts the java code split the original files in exactly 10 Mb files, and (obviously) truncate records. Instead i need each record is complete, intact. Any record should be truncated. If i'm copying a record from the original big csv file to one generated file, and the file dimension will overflow 10 Mb if i copy the record, i should be able to not copy that record, close that file, create a new file and copy the record in the new one. Is it possible? Can someone help me? Thank you!

I tried this code:

File f = new File("/home/luca/Desktop/test/images.csv");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
FileOutputStream out;
String name = f.getName();
int partCounter = 1;
int sizeOfFiles = 10 * 1024 * 1024;// 1MB
byte[] buffer = new byte[sizeOfFiles];
int tmp = 0;
while ((tmp = bis.read(buffer)) > 0) {
 File newFile=new File("/home/luca/Desktop/test/"+name+"."+String.format("%03d", partCounter++));
 newFile.createNewFile();
 out = new FileOutputStream(newFile);
 out.write(buffer,0,tmp);
 out.close();
}

But obviously doesn't work. This code split a source file in n 10Mb files truncating records. In my case my csv file has 16 columns so with the procedure above i have for example the last record has only 5 columns populated. The others are truncated.

SOLUTION Here the code i wrote.

FileReader fileReader = new FileReader("/home/luca/Desktop/test/images.csv");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line="";
int fileSize = 0;
BufferedWriter fos = new BufferedWriter(new FileWriter("/home/luca/Desktop/test/images_"+new Date().getTime()+".csv",true));
while((line = bufferedReader.readLine()) != null) {
    if(fileSize + line.getBytes().length > 9.5 * 1024 * 1024){
        fos.flush();
        fos.close();
        fos = new BufferedWriter(new FileWriter("/home/luca/Desktop/test/images_"+new Date().getTime()+".csv",true));
        fos.write(line+"\n");
        fileSize = line.getBytes().length;
    }else{
        fos.write(line+"\n");
        fileSize += line.getBytes().length;
    }
}          
fos.flush();
fos.close();
bufferedReader.close();

This code read a csv file and split it to n files, each file is at most 10 Mb big and each csv line is completely copied or not copied at all.


Solution

In principle very simple.

You create a buffer of 10MB (byte[]) and read as many bytes as you can from the source. Then you search from the back for a line feed. The portion from the beginning of the buffer to the line feed = new file. You retain the part you have read in excess and copy it to start of buffer (offset 0). The you repeat everything until no more source.



Answered By - Durandal
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to find files that match a wildcard string in Java?

 November 02, 2022     file, java, wildcard     No comments   

Issue

This should be really simple. If I have a String like this:

../Test?/sample*.txt

then what is a generally-accepted way to get a list of files that match this pattern? (e.g. it should match ../Test1/sample22b.txt and ../Test4/sample-spiffy.txt but not ../Test3/sample2.blah or ../Test44/sample2.txt)

I've taken a look at org.apache.commons.io.filefilter.WildcardFileFilter and it seems like the right beast but I'm not sure how to use it for finding files in a relative directory path.

I suppose I can look the source for ant since it uses wildcard syntax, but I must be missing something pretty obvious here.

(edit: the above example was just a sample case. I'm looking for the way to parse general paths containing wildcards at runtime. I figured out how to do it based on mmyers' suggestion but it's kind of annoying. Not to mention that the java JRE seems to auto-parse simple wildcards in the main(String[] arguments) from a single argument to "save" me time and hassle... I'm just glad I didn't have non-file arguments in the mix.)


Solution

Consider DirectoryScanner from Apache Ant:

DirectoryScanner scanner = new DirectoryScanner();
scanner.setIncludes(new String[]{"**/*.java"});
scanner.setBasedir("C:/Temp");
scanner.setCaseSensitive(false);
scanner.scan();
String[] files = scanner.getIncludedFiles();

You'll need to reference ant.jar (~ 1.3 MB for ant 1.7.1).



Answered By - Misha
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to copy and paste a whole directory to a new path recursively?

 November 02, 2022     c#, copy-paste, directory, file     No comments   

Issue

I want to move a directory as a copy/paste routine, by keeping its structure as it is. I am not looking only for the files within all subfolders in a directory then copy/paste them (as this solution), instead I want to clone the whole thing and keep its structure as it is (Tree -> subfolders and files), exactly like a copy and paste routine.

So I found this function that copies a folder full of files to a new path:

Folder -> File(s)

The function behaves as known as the copy/paste routine. It takes SourcePath, DestinationPath and boolean value to OverWriteExisting. Nice and small but too bad it wasn't marked as the actual answer of that question there (recommend a rate).

But what if I want to move a whole directory? In other words, what if I have a folder that has folders of folders of folders of files and etc? And maybe it is unknown the file structure tree size like this:

Folder -> Folder(s) -> ... -> Folder(s) -> File(s)

I am using the below routine to copy/paste a folder that has folders. But here I know that I only have one level of folders so only one foreach loop is required:

foreach (var Folder in DestinationFolder) // here I know that I have only one level of folders to reach the files
{
    CopyDirectory(FolderPath, DestinationPath, false); // use that function to copy the files
}

This above function serves this directory structure:

Folder -> Folder(s) -> File(s)

I tried this and it didn't do what I want. I only retrieve all files while it searches all the subfolders. Which is not what I want. I want to keep the subfolders and the original structure as it is. Here I get four files instead of the directory structed as it is, subfolders and their subfolders, subfolders, files. Only four because it removes duplicates which I do not want this to happen because I need all of them.

Here is my current structure(but my question is global to any directory):

Folder -> Folders -> Folders + Files

Here is what the below code does in the new path:

NewFolder -> AllFilesFoundInAnySubfolder

dialog.FileName = dialog.FileName.Replace(".xml", ""); // get the destination path
DirectoryInfo dirInfo = new DirectoryInfo(dialog.FileName);

if (dirInfo.Exists == false)
    Directory.CreateDirectory(dialog.FileName);

List<String> EverythingInTheDirectory = Directory
                    .GetFiles(FileStructure.baseSessionPath + "\\" + SelectedSession.Name, "*.*", SearchOption.AllDirectories).ToList(); // source

foreach (string file in EverythingInTheDirectory)
{
    FileInfo mFile = new FileInfo(file);
    // to remove name collusion
    if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false)
        mFile.MoveTo(dirInfo + "\\" + mFile.Name);
}

How to move the whole directory with unknown size and keep its structure as it is? Not get only the files from a directory and move them!


Solution

Here is an example that will recursively clone a directory to another destination directory. In the future, adding in what you have tried to the current question will get you better responses

class Program
{
    static void Main(string[] args)
    {
        CloneDirectory(@"C:\SomeRoot", @"C:\SomeOtherRoot");
    }

    private static void CloneDirectory(string root, string dest)
    {
        foreach (var directory in Directory.GetDirectories(root))
        {
            string dirName = Path.GetFileName(directory);
            if (!Directory.Exists(Path.Combine(dest, dirName)))
            {
                Directory.CreateDirectory(Path.Combine(dest, dirName));
            }
            CloneDirectory(directory, Path.Combine(dest, dirName));
        }

        foreach (var file in Directory.GetFiles(root))
        {
            File.Copy(file, Path.Combine(dest, Path.GetFileName(file)));
        }
    }
}


Answered By - hawkstrider
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to read all integers into array from input file in C++

 November 02, 2022     arrays, c++, file     No comments   

Issue

So let's say I have a file like this: 1 23 14 abc 20

It will read the numbers 1, 23, 14, but I also want it to read the value 20 into the array. How can I accomplish this?

#include <iostream>
#include <fstream>
using namespace std;

int main(){
    const int ARRAY_SIZE = 1000;
    int numbers[ARRAY_SIZE], count = 0;
    double total = 0, average;

    ifstream inputFile;

    if(argc == 2){
        inputFile.open(argv[1]);
    }
    else{ 
        cout << "Invalid" << endl;
        return 1;
    }
    while (count < ARRAY_SIZE && inputFile >> numbers[count]){
         count++;
    }
    inputFile.close();
    ...

Solution

Here's how I would do it. I added comments to the code to explain the intent and used a std::vector<int> instead of an array with a hardcoded size since you didn't mention any restrictions and I feel it is a better way to handle the problem. If you must used a statically sized array you should be able to adapt this.

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>

// Avoid using namespace std;

std::vector<int> readData(std::istream& input)
{
    // In general prefer std::vector for variable sized data
    std::vector<int> ret;
    std::string word;
    std::stringstream ss;

    while (input >> word)
    {
        // Clear error flags and set the conversion stream to the value read.
        ss.clear();
        ss.str(word);

        int val = 0;
        // The first condition will fail if the string doesn't start with a digit
        // The second will fail if there is still data in the stream after some of
        // it was converted.
        if (ss >> val && ss.eof())
        {
            ret.push_back(val);
        }
    }
    return ret;
}

int main()
{
    // In your code open the file and pass the stream to
    // the function instead of std::cin
    auto v = readData(std::cin);
    for (auto n : v)
    {
        std::cout << n << " ";
    }
    std::cout << "\n";
}
Input: 1 23 14 abc 20 123xyz 50 nope 12345 a 100
Output: 1 23 14 20 50 12345 100

Working example: https://godbolt.org/z/ab87KjzjK



Answered By - Retired Ninja
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How do I split a line from a file into a 2D list?

 November 02, 2022     arrays, file, list, python, split     No comments   

Issue

My file text.txt contains the following:

hello my
name is
jack black
and I
eat food

I'm trying to read this file into a 2D list called arr such that arr[0][0] = hello , arr[0][1] = my, and so on. I'm new to Python so I don't want to use numpy yet (I see that in a lot of similar questions).

Here is part of the code I have written so far:

for x in range(5):
    for y in range(2):
        nextLine = my_file.readline()
        nextLine = line.strip()
        arr[x][y]= [nextLine.split('\t')]
print(arr[0][0])
print(arr[0][1])

However, when I print arr[0][0] and arr[0][1]. I get the following:

[['hello my']]
[['name is']]

What is the best way to split 'hello' and 'my' such that each one enters the 2D list correctly in the same row?


Solution

Don't use line counts. Just read until the file is empty. And .strip returns a list; you don't need to put it inside another list.

arr = []
for line in my_file:
    arr.append( nextLine.strip().split() )
print(arr[0][0])
print(arr[0][1])


Answered By - Tim Roberts
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How do I rename all files to lowercase?

 November 02, 2022     bash, file, macos     No comments   

Issue

I have for example TREE.wav, ONE.WAV. I want to rename it to tree.wav, one.wav. How do I rename all files to lowercase?


Solution

If you're comfortable with the terminal:

  1. Open Terminal.app, type cd and then drag and drop the Folder containing the files to be renamed into the window.
  2. To confirm you're in the correct directory, type ls and hit enter.
  3. Paste this code and hit enter:

    for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done
    
  4. To confirm that all your files are lowercased, type ls and hit enter again.

(Thanks to @bavarious on twitter for a few fixes, and thanks to John Whitley below for making this safer on case-insensitive filesystems.)



Answered By - wjl
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] Why filestream.close not available

 November 02, 2022     fclose, file, filestream, fopen, vala     No comments   

Issue

I see following code example on this Vala documentation page:

public static int main (string[] args) {
    // Opens "foo.txt" for reading ("r")
    FileStream stream = FileStream.open ("foo.txt", "r");
    assert (stream != null);

    // buffered:
    char buf[100];
    while (stream.gets (buf) != null) {
        print ((string) buf);
    }

    return 0;
}

However, I cannot find a close() function. I want to open file once for reading and later again for writing. Is it safe to do so without a close in between?

(I do not want to use a+ etc mode which permit both reading and writing as both may not be needed while running the application.)


Solution

There are two key items at play:

  1. The FileStream class is a binding to standard C library functions (e.g. open for fopen, read for fread, etc.). (See: this Stack Overflow answer for a good overview of various file APIs)
  2. Vala does automatic reference counting and will free objects for you (See: Vala's Memory Management Explained).

Now if we look at the definition for the FileStream Vala binding, we see:

[ CCode ( cname = "FILE" , free_function = "fclose" ) ]
public class FileStream

Notice the free_function = "fclose" part. This means that when it comes time for Vala to free a FileStream object, it will implicitly call fclose. So no need to attempt this manually. (Also see: Writing VAPI files under the Defining Classes section for details on free_function)

What this means for you is that once your stream object goes out of scope, reference count hits 0, etc. it will get cleaned up for you as you would expect with any other object. You can safely open the file for reading again later by using FileStream.open and getting a new FileStream object.



Answered By - avojak
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How can I write a custom encoder for the below text in C# .NET

 November 02, 2022     c#, encoding, file, serialization, unicode     No comments   

Issue

I'm attempting to read a .txt file and place the text into an object. Then later on serialize said object and write to another .txt, all while keeping the exact same characters.

I've tried using 'iso-8859-1' encoding when using File.ReadAllLines() but I get following: Result

I've also tried creating a custom JavascriptEncoder for serialization but that did not work, I'm assuming since the read wasn't even getting the correct characters.

Is there a way I can write a custom encoder for both File.ReadAllLines() and JsonSerializer.Serialize() so that I can keep the exact same characters throughout. Thanks

Edit : I removed the encoding entirely and it worked for most characters, but still returns 'œ' as 'o'. Original Text: sfør Är du säker på a un¹æ ko róciæ kolejnoœæ numeró e¿y pamiêtaæ, ¿e w aŸn nieœ w górê g³ówna w³aœc


Solution

Ultimately, if you're going to read and write text: you need to know what encoding you're meant to be using. You cannot usually guess. There's not really any such thing as a "text file"; there's just a binary file that your code is going to translate to text via an encoding; either the system can guess, or you can tell it. These days, UTF8 is a pragmatic default, and ANSI encodings such as iso-8859-1 should usually be considered legacy and reserved for handling data that is limited to that specific codepage for historic reasons

So, either:

  1. determine what encoding you're meant to be using and use that for both read and write, or
  2. treat the data as raw bytes, without attempting to parse it into string (etc) data


Answered By - Marc Gravell
Answer Checked By - Marie Seifert (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How do I get specific names from a text file with a location as condition?

 November 02, 2022     c, file, string     No comments   

Issue

Please help me. I'm new to programming. I've been trying and searching for days to answer this.

I need a C program that will open a text file named users.txt

In that text file there are names then comma and their schools.

John Paul, Legit Univ
Paul John, Yo Univ
Lebron James, School Univ
James Lebron, Legit Univ

All I managed so far is to display them all. The output should be all the users that are from "Legit Univ".

Sample output:

Found 2 users from Legit Univ

John Paul
James Lebron

Solution

Use fgets() to read a line from file into a string, then strchr() to to find the position of the comma ',' field separator in the string (or strstr() if the field separator is comma space ", "). Now you can check the part of the string after the field separator for a match on your query with strcmp(). Instead of parsing the file, you could also use a regex and match against the string.



Answered By - Allan Wind
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How can I remove lines from .txt files using a loop?

 November 02, 2022     bash, edit, file, loops     No comments   

Issue

In my current directory I have a couple of .txt files. I want to write a script to search for a string in those .txt files, and delete lines which contains that string. For example, I'd like to delete all lines which have the word "start" in all .txt files in my current directory.

I have written the following code, but I don't know how to continue!

#!bin\bash
files=`find . -maxdepth 1 -name \*.txt`

How should I use "while" to go through each file?


Solution

Use Globs to Populate Loop Variables

When you use -maxdepth 1 on the current directory, you aren't recursing into subdirectories. If that's the case, there's no need at all to use find just to match files with an extension; you can use shell globs instead to populate your loop constructs. For example:

#!/bin/bash

# Run sed on each file to delete the line.
for file in *txt; do
    sed -i '/text to match/d' "$file"
done

This is simple, and avoids a number of filename-related issues that you may have when passing filename arguments between processes. Keep it simple!



Answered By - Todd A. Jacobs
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing