Questions tagged [filenames]
Filenames are metadata about a file; a string used to uniquely identify a file stored on the file system of a computer.
4,182
questions
2821
votes
40
answers
2.3m
views
Extract filename and extension in Bash
I want to get the filename (without extension) and the extension separately.
The best solution I found so far is:
NAME=`echo "$FILE" | cut -d'.' -f1`
EXTENSION=`echo "$FILE" | cut -d'.' -f2`
This ...
1821
votes
33
answers
1.5m
views
Extracting extension from filename in Python
Is there a function to extract the extension from a filename?
837
votes
6
answers
1.3m
views
How to loop over files in directory and change path and add suffix to filename
I need to write a script that starts my program with different arguments. I start my program with:
./MyProgram.exe Data/data1.txt [Logs/data1_Log.txt].
Here is the pseudocode for what I want to do:
...
759
votes
17
answers
556k
views
C++ code file extension? What is the difference between .cc and .cpp [duplicate]
I have seen C++ code saved as both .cc and .cpp files.
Which of these (or another!) is the best practice/most modern/best to use? The Google style guide seems to suggest .cc, are there any other ...
682
votes
20
answers
738k
views
What characters are forbidden in Windows and Linux directory names?
I know that / is illegal in Linux, and * " / \ < > : | ? are illegal in Windows.
What else am I missing? I need a comprehensive guide that also accounts for double-byte characters.
539
votes
15
answers
499k
views
How do I remove the file suffix and path portion from a path string in Bash?
Given a string file path such as /foo/fizzbuzz.bar, how would I use bash to extract just the fizzbuzz portion of said string?
470
votes
17
answers
213k
views
How do I manually create a file with a . (dot) prefix in Windows? For example, .htaccess
I want to create a .htaccess file manually and discovered it seems impossible through the Windows UI. I get a "you must type a filename." message. There has to be a way to create files with . as a ...
467
votes
9
answers
671k
views
Extract file basename without path and extension in bash [duplicate]
Given file names like these:
/the/path/foo.txt
bar.txt
I hope to get:
foo
bar
Why this doesn't work?
#!/bin/bash
fullfile=$1
fname=$(basename $fullfile)
fbname=${fname%.*}
echo $fbname
What's ...
458
votes
3
answers
1.0m
views
Getting the filenames of all files in a folder [duplicate]
I need to create a list with all names of the files in a folder.
For example, if I have:
000.jpg
012.jpg
013.jpg
I want to store them in a ArrayList with [000,012,013] as values.
What's the best ...
450
votes
28
answers
294k
views
Turn a string into a valid filename?
I have a string that I want to use as a filename, so I want to remove all characters that wouldn't be allowed in filenames, using Python.
I'd rather be strict than otherwise, so let's say I want to ...
437
votes
19
answers
327k
views
Get names of all files from a folder with Ruby
I want to get all file names from a folder using Ruby.
352
votes
23
answers
348k
views
How to replace spaces in file names using a bash script
Can anyone recommend a safe solution to recursively replace spaces with underscores in file and directory names starting from a given root directory? For example:
$ tree
.
|-- a dir
| `-- file with ...
322
votes
6
answers
280k
views
.c vs .cc vs. .cpp vs .hpp vs .h vs .cxx [duplicate]
Possible Duplicates:
*.h or *.hpp for your class definitions
What is the difference between .cc and .cpp file suffix?
I used to think that it used to be that:
.h files are header files for C and C++,...
313
votes
9
answers
81k
views
What is the naming standard for path components?
I keep getting myself in knots when I am manipulating paths and file names because I don’t follow a naming standard for path components.
Consider the following toy problem (Windows example, but ...
311
votes
10
answers
466k
views
Given a filesystem path, is there a shorter way to extract the filename without its extension?
I program in WPF C#. I have e.g. the following path:
C:\Program Files\hello.txt
and I want to extract hello from it.
The path is a string retrieved from a database. Currently I'm using the ...
304
votes
13
answers
660k
views
How do I get the file name from a String containing the Absolute file path?
String variable contains a file name, C:\Hello\AnotherFolder\The File Name.PDF. How do I only get the file name The File Name.PDF as a String?
I planned to split the string, but that is not the ...
289
votes
15
answers
524k
views
Maximum filename length in NTFS (Windows XP and Windows Vista)?
I'm designing a database table which will hold filenames of uploaded files. What is the maximum length of a filename in NTFS as used by Windows XP or Vista?
284
votes
7
answers
424k
views
How can I create a full path to a file from parts (e.g. path to the folder, name and extension)?
I need to pass a file path name to a module. How do I build the file path from a directory name, base filename, and a file format string?
The directory may or may not exist at the time of call.
For ...
280
votes
14
answers
471k
views
How do I get a file name from a full path with PHP?
For example, how do I get Output.map
from
F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map
with PHP?
278
votes
6
answers
73k
views
Change case of a file on Windows? [duplicate]
There are a couple of files in our git-controlled codebase that I'd like to rename. Specifically, I just want to change the case of the file, so that sourceCode.java becomes SourceCode.java, for ...
272
votes
8
answers
342k
views
How can I replace (or strip) an extension from a filename in Python?
Is there a built-in function in Python that would replace (or remove, whatever) the extension of a filename (if it has one)?
Example:
print replace_extension('/home/user/somefile.txt', '.jpg')
In my ...
252
votes
7
answers
167k
views
Git copy file preserving history [duplicate]
I have a somewhat confusing question in Git.
Lets say, I have a file dir1/A.txt committed and git preserves a history of commits
Now I need to copy the file into dir2/A.txt (not move, but copy).
I ...
240
votes
10
answers
233k
views
Get file name from URI string in C#
I have this method for grabbing the file name from a string URI. What can I do to make it more robust?
private string GetFileName(string hrefLink)
{
string[] parts = hrefLink.Split('/');
...
240
votes
8
answers
320k
views
Allowed characters in filename
Where can I find a list of allowed characters in filenames, depending on the operating system?
(e.g., on Linux, the character : is allowed in filenames, but not on Windows)
177
votes
29
answers
254k
views
Get file name from URL
In Java, given a java.net.URL or a String in the form of http://www.example.com/some/path/to/a/file.xml , what is the easiest way to get the file name, minus the extension? So, in this example, I'm ...
170
votes
14
answers
429k
views
Use jQuery to get the file input's selected filename without the path
I used this:
$('input[type=file]').val()
to get the file name selected, but it returned the full path, as in "C:\fakepath\filename.doc". The "fakepath" part was actually there - not sure if it's ...
165
votes
8
answers
120k
views
Is it possible to use "/" in a filename?
I know that this is not something that should ever be done, but is there a way to use the slash character that normally separates directories within a filename in Linux?
161
votes
3
answers
170k
views
How to get folder name, in which given file resides, from pathlib.path?
Is there something similar to os.path.dirname(path), but in pathlib?
152
votes
8
answers
184k
views
DateTime.ToString() format that can be used in a filename or extension?
I want to add a timestamp to filenames as files are created but most of the DateTime methods I've tried output something with spaces and slashes. For instance:
Debug.WriteLine(DateTime.Now.ToString())...
147
votes
6
answers
175k
views
A html space is showing as %2520 instead of %20
Passing a filename to the firefox browser causes it to replace spaces with %2520 instead of %20.
I have the following HTML in a file called myhtml.html:
<img src="C:\Documents and Settings\...
147
votes
5
answers
84k
views
How to apply a Git patch to a file with a different name and path?
I have two repositories. In one, I make changes to file ./hello.test. I commit the changes and create a patch from that commit with git format-patch -1 HEAD. Now, I have a second repository that ...
144
votes
1
answer
65k
views
Preferred (or most common) file extension for a Python pickle
At times, I've seen .pickle, .pck, .pcl, and .db for files that contain Python pickles, but I am unsure what is the most common or best practice. I know that the latter three extensions are also used ...
142
votes
23
answers
202k
views
Sanitizing strings to make them URL and filename safe?
I am trying to come up with a function that does a good job of sanitizing certain strings so that they are safe to use in the URL (like a post slug) and also safe to use as file names. For example, ...
139
votes
11
answers
181k
views
How to rename with prefix/suffix?
How do I do mv original.filename new.original.filename without retyping the original filename?
I would imagine being able to do something like mv -p=new. original.filename or perhaps mv original....
136
votes
11
answers
169k
views
Log4net rolling daily filename with date in the file name
I would like to have files named for example:
dd.mm.yyyy.log
How is this possible with log4net?
135
votes
10
answers
195k
views
How to get file name from content-disposition
I downloaded a file as response of ajax. How to get the file name and file type from content-disposition and display thumbnail for it. I got many search results but couldn't find right way.
$("....
135
votes
6
answers
103k
views
What is your favorite date and time format in a file name? [closed]
This is a somewhat subjective question, and not very important in the big scheme of things, but something that yet annoys me regularly. There seems to be no self-evident way to put a timestamp in a ...
129
votes
6
answers
137k
views
Should Python class filenames also be camelCased?
I know that classes in Python are typically cased using camelCase.
Is it also the normal convention to have the file that contains the class also be camelCase'd especially if the file only contains ...
128
votes
11
answers
105k
views
Linux - Replacing spaces in the file names
I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?
116
votes
6
answers
139k
views
Renaming part of a filename [duplicate]
I have loads of files which look like this:
DET01-ABC-5_50-001.dat
...
DET01-ABC-5_50-0025.dat
and I want them to look like this:
DET01-XYZ-5_50-001.dat
...
DET01-XYZ-5_50-0025.dat
How can I do ...
115
votes
17
answers
52k
views
How to make a valid Windows filename from an arbitrary string?
I've got a string like "Foo: Bar" that I want to use as a filename, but on Windows the ":" char isn't allowed in a filename.
Is there a method that will turn "Foo: Bar" into something like "Foo- Bar"?...
113
votes
5
answers
252k
views
Why do I get a SyntaxError for a Unicode escape in my file path? [duplicate]
The folder I want to get to is called python and is on my desktop.
I get the following error when I try to get to it
>>> os.chdir('C:\Users\expoperialed\Desktop\Python')
SyntaxError: (...
106
votes
9
answers
338k
views
Rename multiple files in a folder, add a prefix (Windows)
I'd like to batch rename files in a folder, prefixing the folder's name into the new names. i.e. files in C:\house chores\ will all be renamed house chores - $old_name.
105
votes
16
answers
168k
views
Getting the names of all files in a directory with PHP
For some reason, I keep getting a '1' for the file names with this code:
if (is_dir($log_directory))
{
if ($handle = opendir($log_directory))
{
while($file = readdir($handle) !== ...
103
votes
3
answers
116k
views
What is the best way to combine a path and a filename in C#/.NET?
What is the best way to combine a path with a filename?
That is, given c:\foo and bar.txt, I want c:\foo\bar.txt.
Given c:\foo and ..\bar.txt, I want either an error or c:\foo\bar.txt (so I cannot ...
100
votes
26
answers
223k
views
How to get file extension from string in C++
Given a string "filename.conf", how to I verify the extension part?
I need a cross platform solution.
91
votes
8
answers
27k
views
How to deploy Node.js application with deep node_modules structure on Windows?
I've run into a curious issue - apparently some Node.js module have so deep folder hierarchies that Windows copy command (or PowerShell's Copy-Item which is what we're actually using) hits the ...
89
votes
6
answers
97k
views
Flutter: Get the filename of a File
I thought this would be pretty straight-forward, but can't seem to get this. I have a File file and it has a path file.path which spits out something like /storage/emulated/0/Android/data/my_app/files/...
85
votes
5
answers
74k
views
javascript url-safe filename-safe string
Looking for a regex/replace function to take a user inputted string say, "John Smith's Cool Page" and return a filename/url safe string like "john_smith_s_cool_page.html", or something to that extent.
81
votes
8
answers
99k
views
Mac OS X doesn't allow to name files starting with a dot. How do I name the .htaccess file?
As mentioned in the title, Mac OS X doesn't allow me to name files starting with a dot ( . ).
You can’t use a name that begins with a dot “.”, because these names are reserved for the system. Please ...