for name in `ls` and filenames with spaces
Just don’t use command substitution: use for name in *.
Just don’t use command substitution: use for name in *.
No, you can’t. Java is rather low-level language — comparing with shell-script — so things like this must be done more explicetly. You should search for files with required mask with folder.listFiles(FilenameFilter), and iterate through returned array deleting each entry. Like this: final File folder = … final File[] files = folder.listFiles( new FilenameFilter() { … Read more
new File(fileName).getName(); or int idx = fileName.replaceAll(“\\\\”, “https://stackoverflow.com/”).lastIndexOf(“https://stackoverflow.com/”); return idx >= 0 ? fileName.substring(idx + 1) : fileName; Notice that the first solution is system dependent. It only takes the system’s path separator character into account. So if your code runs on a Unix system and receives a Windows path, it won’t work. This is … Read more
Dir::Tmpname.create You could use Dir::Tmpname.create. It figures out what temporary directory to use (unless you pass it a directory). It’s a little ugly to use given that it expects a block: require ‘tmpdir’ # => true Dir::Tmpname.create([‘prefix-‘, ‘.ext’]) {} # => “/tmp/prefix-20190827-1-87n9iu.ext” Dir::Tmpname.create([‘prefix-‘, ‘.ext’], ‘/my/custom/directory’) {} # => “/my/custom/directory/prefix-20190827-1-11x2u0h.ext” The block is there for code … Read more
To create a file named the current date/time: Date date = new Date() ; SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH-mm-ss”) ; File file = new File(dateFormat.format(date) + “.tsv”) ; BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write(“Writing to file”); out.close();
I hate having to install libraries (especially those that want me to use installer packages to install them). If you’re looking for a clean solution for a directory listing on an absolute path in Lua, look no further. Building on the answer that sylvanaar provided, I created a function that returns an array of all … Read more
EDIT: A better way of doing this is to take a date/time string that has a defined and unchanging format instead of using the locale-defined ones from %date% and %time%. You can use the following to get it: for /f “skip=1” %%x in (‘wmic os get localdatetime’) do if not defined mydate set mydate=%%x It … Read more
Modified Base64 (when /,= and + are replaced) is safe to create names but does not guarantee reverse transformation due to case insensitivity of many file systems and urls. Base64 is case sensitive, so it will not guarantee 1-to-1 mapping in cases of case insensitive file systems (all Windows files systems, ignoring POSIX subsystem cases). … Read more
The following script can help you. You should not be running several copies of the script at the same time to avoid race condition. name=somefile if [[ -e $name.ext || -L $name.ext ]] ; then i=0 while [[ -e $name-$i.ext || -L $name-$i.ext ]] ; do let i++ done name=$name-$i fi touch — “$name”.ext
import os import re path = “/home/mypath” for filename in os.listdir(path): if re.match(“text\d+.txt”, filename): with open(os.path.join(path, filename), ‘r’) as f: for line in f: print line, Although you ignored my perfectly fine solution, here you go: import glob path = “/home/mydir/*.txt” for filename in glob.glob(path): with open(filename, ‘r’) as f: for line in f: print … Read more