View PlGroup Filehandling
ID | 21 |
Text | Filehandling |
Title | Not set |
PlItems
- procedure TForm1.MenuDummy1Click(Sender: TObject);
var
ExeName : array[0..255] of char;
myFile : String;
begin
myFile:='winword.exe';
Showmessage('Starte: ' + myFile);
StrPCopy(ExeName, myFile);
ShellExecute(MainForm.Handle, 'open', ExeName, nil, nil, SW_SHOWNORMAL);
// Drucken geht dann so (siehe auch Dateitypen im Windows Explorer):
ShellExecute(MainForm.Handle, 'print', ExeName, nil, nil, SW_SHOWNORMAL);
end;
- import os
searchPath = '/home/patrick/Desktop/'
# os.walk will go through all subdirectories
for pathentry in os.walk(searchPath, False):
for dir in pathentry[1]:
path = os.path.join(pathentry[0], dir)
if os.path.islink(path):
print path + "... is a link"
# os.unlink(path)
else:
print path + "... is a dir"
# os.rmdir(path)
for file in pathentry[2]:
path = os.path.join(pathentry[0], file)
# os.unlink(path)
print path + "... is a file"
# os.listdir only the given path
for file in os.listdir(searchPath):
fullfilename = os.path.join(searchPath, file)
if os.path.isfile(fullfilename):
print fullfilename + "... is a file"
- AppPath:=ExtractFilePath(Application.Exename);
- private void ReadAllFilesInDir(string path)
{
DirectoryInfo dirInfo = new DirectoryInfo(path);
FileInfo[] files = dirInfo.GetFiles();
foreach (FileInfo fiOutput in files)
{
Console.WriteLine(fiOutput.Name);
}
}
- import commands
output=commands.getoutput("dir")
print output
# home tmp myFile.sh
- # http://wiki.ubuntuusers.de/Shell/Bash-Skripting-Guide_f%C3%BCr_Anf%C3%A4nger#Variablen-Teil-2
#!/bin/bash
for file in /home/myUser/*/*.png ; do
echo "Diese Datei: $file"
fname=$(basename "$file")
echo "hat den Namen: $fname"
fdir=$(dirname "$file")
echo "und steht im Verzeichnis: $fdir"
# Diese Datei: /home/myUser/galaxy.png
# hat den Namen: galaxy.png
# und steht im Verzeichnis: /home/myUser
- # http://wiki.ubuntuusers.de/Shell/Bash-Skripting-Guide_f%C3%BCr_Anf%C3%A4nger#If-Else-Anweisung
#!/bin/bash
filePath="/home/myUser/test.txt"
if [ -f ${filePath} ]
then
echo "There it is! ;-)"
else
echo "Whow, file is missing at this place :-("
fi
- procedure TForm1.CheckIfFileExists();
var
filePath : String;
begin
filePath:='C:\User\myUser\text.txt';
if FileExists(filePath) then
begin
ShowMessage('There it is! ;-)');
end
else
begin
ShowMessage('Whow, file is missing at this place :-(');
end;
end;
- String filePath = "C:\\User\\myUser\\text.txt";
if (System.IO.File.Exists(filePath))
{
MessageBox.Show("There it is! ;-)");
}
else
{
MessageBox.Show("Whow, file is missing at this place :-(");
}
- $filePath = "/home/myUser/test.txt";
if (file_exists($filePath))
{
echo "There it is! ;-)"
}
else
{
"Whow, file is missing at this place :-("
}
- @echo off
SET checkFileExists="C:\Temp\myFile.txt"
IF exist %checkFileExists% GOTO :EXISTS
GOTO :EXISTSNOT
:EXISTS
echo There it is :-)
GOTO :END
:EXISTSNOT
echo Sorry, the file isn't there :-(
GOTO :END
:END
echo End of program
- import os
filePath="/home/myUser/test.txt"
if (os.path.isfile(filePath)):
print "There it is! ;-)"
else:
print "Whow, file is missing at this place :-("
- import os
myFilename = "/tmp/Test/myFile.txt"
# Get only the path
path_only = os.path.dirname(myFilename)
# /tmp/Test
# Filename only
filename_only = os.path.basename(myFilename)
# myFile.txt
# get only filename without extention
filename_without_ext = os.path.basename(os.path.splitext(myFilename)[0])
# myFile
# get only the extension
filename_extention_only = os.path.splitext(myFilename)[1]
# .txt
# put them together again
myFilename_together = os.path.join(path_only, filename_without_ext + filename_extention_only)
# /tmp/Test/myFile.txt
- String thisVar = "OK";
boolean resultVar;
resultVar = thisVar.equals("OK") ? true : false;
// same as if-else construction
if (thisVar.equals("OK"))
{
resultVar = true;
}
else
{
resultVar = false;
}
- #!/bin/sh
myFilename="$1"
# or
myFilename="/tmp/Test/myFile.txt"
# Get only the path
path_only=$(dirname "$myFilename")
# /tmp/Test
# if it is a local file in the path, then . will be prompted e.g. myFilename="myFile.txt"
# Filename only
filename_only=$(basename "$myFilename")
# myFile.txt
# get only filename without extention
filename_without_ext="${filename_only%.*}"
# myFile
# get only the extension
filename_extention_only="${myFilename##*.}"
# txt
# put them together again
myFilename_together="${path_only}/${filename_without_ext}.${filename_extention_only}"
# /tmp/Test/myFile.txt
- $CSVFullPath = "C:\Temp"
$FileFilter = "*.csv"
$ListOfFiles = (Get-ChildItem -Path $CSVFullPath -Filter $FileFilter) | Select Name, BaseName, DirectoryName, FullName, Extension
$ListOfFiles.Name
- $CSVFullPath = "C:\Temp"
$FileFilter = "*.*"
$ListOfFiles = (Get-ChildItem -Path $CSVFullPath -Filter $FileFilter -Recurse) | Select Name, BaseName, DirectoryName, FullName, Extension | Where {$_.DirectoryName -like "*not_processed*" }
$ListOfFiles.FullName
- $myFolder = "C:\TEMP"
if ((Test-Path $myFolder) -eq $true)
{
write-host "Folder exists!"
}
else
{
write-host "Folder doesn't exist!"
}
- import os.path
from os import path
myFolder = "/tmp/test"
if path.exists(myFolder) & path.isdir(myFolder):
print "Folder exists!"
else:
print "Folder doesn't exist!"
- import os
myFolder = "/tmp/test"
try:
os.mkdir(myFolder)
except OSError:
print ("Error creating directory: %s" % myFolder)
else:
print ("Directory created: %s " % myFolder)
- #!/bin/sh
# The script is located in /tmp/abc/myscript.sh
export AppPath=$(dirname $0)
echo $0
# /tmp/abc/myscript.sh
echo $AppPath
# /tmp/abc